AI Agent Binary Search Debug 2026
A ten-step task fails at step seven. Reading logs from either end takes half an hour and the failure may not even reproduce. Binary debugging halves the search space each round; with checkpoints and fixed replay, locating the fault takes minutes.
📜 Table of Contents
📖 Keep reading
Binary Debugging for Agents: Find the Broken Step Fast
The user says the result is wrong. You open the logs: fifteen calls, every one returning 200. Where did it break? Binary search beats careful reading, but it needs inspectable intermediate state.
1. Record a checkpoint per step
import json, time, uuid
class Checkpoint:
def __init__(self, run_id=None):
self.run_id = run_id or uuid.uuid4().hex[:12]
self.steps = []
def record(self, name, inputs, output, error=None):
self.steps.append({
"run_id": self.run_id, "name": name, "seq": len(self.steps),
"input": json.dumps(inputs, ensure_ascii=False)[:2000],
"output": json.dumps(output, ensure_ascii=False)[:2000],
"error": error, "ts": time.time(),
})
Truncate payloads or a single record grows to megabytes.
2. Reproduce before locating
Set temperature to 0, pin tool responses, pin the prompt version, replay the same input. Non-determinism comes from sampling, tool returns, and clock or randomness. If it still will not reproduce, suspect concurrency or external state before reaching for bisection.
3. Inject known-good results at the midpoint
def run_with_injection(agent, steps, inject_at=None, payload=None):
for i, step in enumerate(steps):
out = payload if i == inject_at else agent.execute(step)
if out is None or out.get("error"):
return i
return None
Ten steps need at most four injections to isolate one. Start near the middle.
4. Diff two traces
Compare a good run and a bad run on four fields: prompt version, tool schema, model version, retrieved context. Regressions usually differ by one variable.
5. Keep a minimal reproduction set
| Case type | Store | Purpose |
|---|---|---|
| Single-step failure | step input and tool output | tool-layer bugs |
| Multi-step drift | full message sequence | planning bugs |
| Format failure | raw model output | parsing bugs |
| Long context | full retrieval dump | truncation bugs |
Twenty cases covering the main failure modes are enough to make bisection repeatable.
Combined effect
| Practice | Solves | Effort |
|---|---|---|
| Checkpoints | unknown step | small, one-off |
| Fixed replay | no reproduction | small, needs tool changes |
| Injection | slow narrowing | small |
| Trace diff | regressions from changes | small |
| Repro set | recurring regressions | medium |
A practical order
- Find the first step whose output is clearly wrong.
- Check whether its input was already wrong; move forward or backward.
- Input right, output wrong: was the tool called, with the right arguments?
- Arguments right, result wrong: truncated or mis-parsed response?
- All clean: did the model or prompt version change?
FAQ
Do checkpoints slow the main path? Synchronous DB writes do. Use an async queue; the added latency is usually milliseconds.
Full checkpointing in production? Yes, with sampling and truncation: all failures, a fraction of successes.
How do I judge a step is wrong when output is prose? Require structured output for key steps and validate against a schema.
Does bisection work for planning errors? It does, with a different test: every step looks fine but the whole run drifts. Inject a known-good plan at the midpoint and see if the rest completes.
Trace field names differ by framework; standardise your own checkpoint schema rather than relying on defaults.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.