Skip to content

Detection

chimera.detection identifies when an agent falls into a repetitive loop by analysing tool-call history. Two strategies detect different repetition patterns and can be combined via a composite detector.

A frozen dataclass returned when a strategy fires:

FieldTypeDescription
detectedboolWhether a loop was found
strategystrName of the triggering strategy (e.g. "exact_repeat")
patternstrHuman-readable description of the detected pattern
confidencefloatValue in [0, 1] expressing certainty (default 1.0)

Every detector implements three methods:

MethodDescription
record(tool_name, args)Record a tool invocation for later analysis
check()Return a DetectionResult if a loop is found, else None
reset()Clear all internal state

Detects when the last N tool calls share the same MD5 signature. Each tool call is hashed as md5(json.dumps({"name": ..., "args": ...}, sort_keys=True)).

ParameterDefaultDescription
window10Max recent signatures to retain in the sliding window
threshold3How many consecutive identical signatures trigger detection
from chimera.detection import ExactRepeatDetector
detector = ExactRepeatDetector(window=10, threshold=3)
detector.record("bash", {"command": "ls"})
detector.record("bash", {"command": "ls"})
detector.record("bash", {"command": "ls"})
result = detector.check() # DetectionResult(detected=True, ...)

Detects repeating A-B-A-B (or longer period) cycles. The algorithm checks every candidate period from 2 up to len(history) // 2. A cycle is confirmed when the same sub-sequence repeats threshold times consecutively at the tail.

ParameterDefaultDescription
window10Max recent signatures to retain
threshold2How many consecutive repetitions of the cycle constitute a match
from chimera.detection import PatternCycleDetector
detector = PatternCycleDetector(window=10, threshold=2)
detector.record("read_file", {"path": "a.py"})
detector.record("edit_file", {"path": "a.py"})
detector.record("read_file", {"path": "a.py"})
detector.record("edit_file", {"path": "a.py"})
result = detector.check() # Cycle of period 2 repeated 2 times

Fans out to a list of strategies. record() is forwarded to all of them; check() returns the first non-None result.

from chimera.detection import CompositeDetector, ExactRepeatDetector, PatternCycleDetector
detector = CompositeDetector([
ExactRepeatDetector(threshold=3),
PatternCycleDetector(threshold=2),
])

The LoopDetector is a convenience wrapper that creates a default CompositeDetector (with both ExactRepeatDetector and PatternCycleDetector) and pairs it with an OnDetect action policy.

from chimera.detection import LoopDetector, OnDetect
ld = LoopDetector(on_detect=OnDetect.BREAK, window=10, threshold=3)
result = ld.record_and_check("bash", {"command": "ls"})
if result:
print(f"Loop: {result.pattern}")

Controls what the agent loop does when a loop is detected:

ValueBehaviour
OnDetect.ASKPrompt the user for confirmation before continuing
OnDetect.BREAKImmediately stop the agent loop
OnDetect.WARNLog a warning but continue execution

Implement DetectionStrategy to add domain-specific detection:

from chimera.detection import DetectionStrategy, DetectionResult
class TokenBudgetDetector(DetectionStrategy):
def record(self, tool_name, args):
...
def check(self):
if self._over_budget():
return DetectionResult(
detected=True,
strategy="token_budget",
pattern="Token budget exceeded",
)
return None
def reset(self):
...