AI Agent Binary Search Debug 2026

🤖 Agents 2026-07-17 · Updated 2026-11-09 3 min read

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

  1. Find the first step whose output is clearly wrong.
  2. Check whether its input was already wrong; move forward or backward.
  3. Input right, output wrong: was the tool called, with the right arguments?
  4. Arguments right, result wrong: truncated or mis-parsed response?
  5. 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

Related Articles
2026-07-12
Self-Hosted AI Agent Guide 2026: Run Your Own Private AI Assistant on a VPS
2026-08-13
MCP Server Marketplace 2026: Where to Find 10,000+ Model Context Protocol Servers
2026-08-14
AI PDF Chatbot With LangChain 2026: Build One in 30 Minutes
2026-07-16
Choosing an Open Source RAG Stack in 2026: Three Categories, Eight Projects
2026-08-05
AI Quiz Generator in 2026: Build a Free Self-Hosted Quiz App with Anki (30k Stars) + LangChain - 5 Working Patterns
2026-08-06
MCP TypeScript SDK (13,080 Stars) 2026: Build Your First Model Context Protocol Server in TypeScript

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.

💬 Comments (0)

No comments yet. Be the first!

Login to comment