Skip to content
The AI Agent Factory

27.6 Diagnosing a misbehaving agent

Approved by
Panaversity ·
23 min read

In everyday life. A family comes home and finds the kitchen floor under water. The eldest daughter does three things. She says what she sees: the bath upstairs has overflowed. She finds where it began: the tap was left on. Then she asks the question that stops it from happening again: whose job was it to turn the tap off? Each person thought another would do it. Nobody had decided. Mopping the floor fixes today's problem. Naming whose job the tap is fixes next week's. In this section, the water is the wrong run, and the tap is where the failure began. The job nobody was given is a row of your table with no owner.

What you will learn. You practice this in Lab 27, Task 3.

  • What are the three steps of a diagnosis?
  • Which row does each of the four patterns belong to?
  • When is a pattern missing, and when is it misapplied?
  • Which symptoms are not one of the four patterns?
  • What does a diagnosis cost?

Why it matters. A builder who sees only "the agent misbehaved" changes the prompt. If nobody chose who stops the run, no prompt will stop it.

At work. On the same Wednesday, both builds have run, and you open the first of three broken traces. The worker calls search_policy, reads the result, and calls search_policy again with the same input, many times. The trace ends with no stop from a limit. You do not open the code. You open docs/architecture.md and read the four rows under the steps. The row "when the run stops" has an empty owner.

What are the three steps of a diagnosis?

Name the failure, say where it began, then find the row nobody decided. 24.7 gave the first step and 24.8 the second. This section adds the third. The exam guide describes the whole task as reading "a trace or description of a misbehaving agent to identify which design pattern is missing or misapplied".1

  1. Its name, where 24.7 has one. Of the four traces in the next part, the third is a hallucination. Its answer states a fact that no tool result or document of Brightline's holds. The fourth, which is Lab 26's, is context loss. The first two have no name among the six, and that is allowed: 24.7's list is open. Say what happened in your own words, and invent no new name.
  2. Where it began. Read the status, then the request as sent, then the output (24.8). Do this for each trace before you look for a row.
  3. The row. Under the steps, your table has four rows for the whole run. Find the row that has no owner, or the wrong value.

Which row does each of the four patterns belong to?

Each pattern is the owner's decision for one of the four run rows. A design pattern here is a rule about the whole run that the builder's code holds. The guide names four: "tool-use loop termination, subagent isolation, memory, or context-window management".1 Where nobody decided the row, the model or a product default decided, and no code limited it.

  • When the run stops: tool-use loop termination. Loop termination is the rule that ends the loop when the model does not end it. The trace shows the same tool call repeating, and no stop from a limit. Nobody decided, so the Claude Agent SDK's default did, and the default is no limit on turns or on budget (27.3). The correction is 21.8's turn limit and budget, set by you.
  • What a helper sees: subagent isolation. The trace shows a subagent whose first request already holds the parent's conversation. On the Claude Agent SDK you can find a subagent's steps in a trace. Each of its messages has a parent_tool_use_id, the id of the parent's call that started it.2 The product form of this fault is a fork, which "copies the conversation so far instead of starting fresh."2 The correction is a subagent that starts fresh from its brief (26.5). To stop forks, add a deny rule for Agent(fork). A deny rule is a permission rule that always blocks one tool.2
  • Whose notes the run may read: memory. The trace shows Brightline's review stating a fact that no tool result of Brightline's holds. The fact came from a note saved for Cedar Ridge, the other company on the portal. Nobody decided whose store the run opens, so both companies used one store. The correction is one store for each person and company, chosen by your code (26.6).
  • What stays in the window: context-window management. The trace shows a rule followed on the early invoices and not on the late ones, with a summary between them. Nobody told the summary what to keep. This is Lab 26's trace, traces/month-end-drift.jsonl, which 26.2 reads. Its correction is a summary that is told what to keep (26.4).

Figure 27.4. Each sign in a trace points to one run row, and each row to the pattern that corrects it.

When is a pattern missing, and when is it misapplied?

It is missing when the row has no owner. It is misapplied when the row has an owner and the wrong value. The four cases above are all missing patterns. Two cases show the other kind:

  • A turn limit set too low. A review that is going well ends with the result error_max_turns3 before its last invoices are done. Loop termination is there. Its value is wrong.
  • A helper kept apart from details the parent needed. The subagent is isolated as designed, and its one short line leaves out a value the parent must decide on. Isolation is there, applied to work that needed one window (26.5).

The correction differs. For a missing pattern, give the row an owner. For a misapplied one, change the value, and read a passing run first to learn what the value should be. The guide calls this last step "selecting the pattern that corrects the behavior".1

A pattern also does only its own job. Loop termination stops the cost. "A limit only stops a run. It never says the answer was right." (22.6) Whether the answer may be used is the check's decision.

Which symptoms are not one of the four patterns?

Three are answered by the second step of the diagnosis, where it began. One is a check that nobody set. Not every bad run points to a run row.

In Claude. For three symptoms, 24.8's question, where it began, is the whole answer. The vendor's documentation says what to do for each:4

What the trace showsWhat to do
A tool call cut off because the response reached max_tokens"retry the request with a higher max_tokens value to get the full tool use"
An empty response with the stop reason end_turn, right after tool resultsLook for a text block that your code added after the tool results
The stop reason refusal: the model declines"Read stop_details and retry on a fallback model", which means another model

The fourth symptom does have a place in the table. A worker, or a checker, says "done" before the work is done. The vendor names this as the main way a checking subagent fails: "marking outputs as passing without thorough testing."5 In the table it is a step's check, in the sixth column, that nobody set. The correction is a check by code on what that step returned (23.16).

What does a diagnosis cost?

It costs a trace that holds content, a table to read it against, and the patience to read before changing anything. Some traces record only which calls ran, and not what was said. Such a trace cannot show a subagent's first request or a note from the wrong store (24.7). And the third step works only if the table was written, which is why Lab 27 writes it first.

One trace also shows one run. It can show that a row had no owner. It cannot show how often the fault appears.

When you have only a description, such as "the agent sometimes never finishes", the steps are the same. Ask which of the four rows the words point to, and then ask for the trace.

The operating rule: name the failure, and find where it began. Then find the run row with no owner or the wrong value, and correct that row in code.

Do this now. Ask your coding agent to fill the four run rows for your own project. Copy this line:

Read this project's code and answer four questions, each with the file and the line that decides it. What stops a run when the model does not stop? What does a subagent receive when it starts? Whose notes may a run read? What stays in the context window when the history is cut? Where no line decides one, write "nobody decided". Change no file.

For each "nobody decided", say which of the four patterns is missing, and what a trace of that run would show.

Check yourself

Question 1 / 8

0 answered

In what order does a diagnosis of a misbehaving agent go?

  1. Claude Certified Developer - Foundations Exam Guide, version 1.1, Anthropic.↑↑2↑3
  2. Subagents in the SDK, Claude Agent SDK, Anthropic.↑↑2↑3
  3. How the agent loop works, Claude Agent SDK, Anthropic.↑
  4. Stop reasons and fallback, Anthropic.↑
  5. Building multi-agent systems: when and how to use them, Anthropic.↑
Owner
Panaversity