Debugging and Evaluating Agent Behavior

Claude Code Agents: Advanced Orchestration

Chapter 8 · Debugging and Evaluating Agent Behavior

Fundamentals' Chapter 3 established "trust but verify" — an agent's report describes intent, not a guarantee. This chapter covers what happens once verification actually turns up something wrong: isolating where in a chain the mistake originated, telling a bad brief apart from a genuinely unreliable agent, and evaluating behavior systematically rather than reacting to one incident.

Isolating Where in a Chain Something Went Wrong

For a single agent, "trust but verify" means checking that one agent's own work. In a chain, a wrong final result could have originated at any step — the research agent found the wrong thing, the planning agent proposed a flawed approach despite correct research, or the coding agent implemented a good plan incorrectly. Debugging a chain means checking each step's own actual output against what it should have produced, rather than assuming the last agent in the chain is automatically the one at fault simply because its output is what surfaced the problem.

Distinguishing a Bad Input From a Bad Agent

An agent — even a genuinely well-designed one — given a poor brief, or working from an earlier step's own already-wrong output, will produce a wrong result. That's not the agent's own fault; it's a symptom of a problem that originated earlier in the chain and simply propagated forward, exactly as Chapter 7 described. Before concluding an agent itself is unreliable, confirm what it was actually working from was correct in the first place.

Evaluating Agent Behavior Systematically

Beyond debugging one failure, genuinely evaluating whether an agent — or a whole pipeline — is reliable over time benefits from a small set of representative test cases with known-correct expected outcomes, run repeatedly, rather than judging reliability from a handful of anecdotal results noticed in passing. This applies Fundamentals' Chapter 7's testing discipline to the agents themselves, not just to code.

When to Revise a Brief vs. When to Revise the Agent's Own Definition

If the same agent definition, given a new and better brief, now produces a correct result, the original problem was the brief — a per-task fix, not a sign anything is wrong with the agent itself. If the same agent, given an already-good brief, still produces the wrong kind of result repeatedly, the problem more likely lives in the agent's own definition — its system prompt, tool access, or model choice — and needs revising at that level, rather than re-briefed differently every single time it's used.

AspectDebugging a Single AgentDebugging a Chain
What to checkThat one agent's own actual outputEvery step's own output, working through the chain
Where the fault could beThe agent, or its briefAny step, or the handoffs between them (Ch.7)
First moveVerify against the task's own requirementsCheck the earliest step first, not just the last
Check the earliest step first, not just the last one
A wrong final result naturally draws attention to the last agent in the chain, since that's where the problem became visible — but the actual mistake may have happened several steps earlier and simply gone unnoticed until it reached the end. Working through the chain from the earliest step forward (or backward from the failure, methodically) finds the real origin faster than assuming the most visible step is automatically the culpable one.
One incident, in either direction, doesn't prove reliability or unreliability
A single anecdotal failure doesn't necessarily mean an agent's own definition is broken — it could simply be one unusual edge case its brief or design never anticipated. Just as importantly, a single anecdotal success doesn't prove an agent is reliable either. Judging based on one incident, in either direction, risks the wrong conclusion — this is exactly why systematic evaluation over multiple representative cases matters more than a gut reaction to any one result.

Hands-On Exercises

Exercise 1

A three-agent chain (research, plan, implement) produces an incorrect implementation. Describe how you would go about finding which step actually caused the problem, using this chapter's own guidance.

📄 View solution
Exercise 2

A coding agent produces incorrect code once, and a developer immediately concludes the agent's definition needs to be rewritten. Using this chapter's own material, explain what should be checked first before reaching that conclusion.

📄 View solution
Exercise 3

Explain the difference between fixing a problem by rewriting a single brief and fixing a problem by revising an agent's own definition, and how you would tell which one a given failure actually calls for.

📄 View solution

Chapter 8 Quick Reference

  • A wrong result from a chain could originate at any step, not necessarily the last one — check the earliest step first
  • An agent given a poor brief or bad earlier output isn't itself broken — confirm its actual input was correct before blaming the agent
  • Evaluate reliability over a set of representative test cases, not a handful of anecdotal results
  • A new brief fixing the result → the brief was the problem. The same good brief still failing → revise the agent's own definition
  • One incident, in either direction, doesn't prove an agent is reliable or unreliable