Skip to content

loop_guard

diapason.agents.loop_guard

Agent loop guard — detect and prevent degenerate tool-calling loops.

LoopGuard

Detect and prevent degenerate agent loops.

Features: 1. Hash tracking: SHA-256 of (tool_name, args) blocks after max_identical_calls 2. Ping-pong detection: Sliding window detects A-B-A-B or A-B-C-A-B-C patterns 3. Poll-tool awareness: Tools with spec.metadata["polling"] = True get relaxed budget 4. Context overflow recovery: 4-stage compression of message history

check_call(tool_name, arguments)

Check whether a tool call should proceed or be blocked.

check_response(content)

Check whether an agent response indicates a loop. Reserved for future use.

compress_context(messages)

Apply 4-stage context overflow recovery to message list.

Stages: 1. Summarize old tool results (replace content with "[Tool result truncated]") 2. Sliding window — keep only recent messages 3. Drop tool call/result pairs from the middle 4. Truncate to system + last 2 exchanges

reset()

Reset all tracking state — always via Rust backend.

LoopGuardConfig dataclass

Configuration for the loop guard.

LoopVerdict dataclass

Result of a loop guard check.