# 27.6 Diagnosing a misbehaving agent (/building-ai-workers/agent-architecture/diagnosing-a-misbehaving-agent)

---
type: Document
title: "27.6 Diagnosing a misbehaving agent"
description: "How to read a trace for the step nobody decided, name the pattern that is missing or misapplied, and choose the fix."
status: stable
order: 327.06
ksor:
  owner: team:panaversity
  audience:
    - public
  approval:
    by: process:panaversity
    at: 2026-11-30T08:16:30Z
chapter: '27'
part: III
expert_status: manager
concepts: [ "27.6" ]
last_verified: '2026-11-30'

sources:
  - id: anthropic-ccdv-f-exam-guide
    title: Claude Certified Developer - Foundations Exam Guide, version 1.1, effective December 7, 2026 (read 2026-11-30)
    resource: https://cc.sj-cdn.net/instructor/6nizmqk8tpzpfjvt6qmmav7rh/public/1794946771/Claude_Certified_Developer_-_Foundations_Exam_Guide_Updated_Dec_7.pdf
  - id: claude-agent-sdk-subagents
    title: Subagents in the SDK (Claude Agent SDK, verified 2026-11-30)
    resource: https://code.claude.com/docs/en/agent-sdk/subagents
  - id: anthropic-sdk-agent-loop
    title: How the agent loop works (Claude Agent SDK, verified 2026-11-30)
    resource: https://code.claude.com/docs/en/agent-sdk/agent-loop
  - id: anthropic-multi-agent-when
    title: 'Building multi-agent systems: when and how to use them (Claude, January 23, 2026, verified 2026-11-30)'
    resource: https://claude.com/blog/building-multi-agent-systems-when-and-how-to-use-them
  - id: anthropic-stop-reasons
    title: Stop reasons and fallback (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons
generated:
  at: 2026-11-30T08:16:30Z
  by: esl-rewrite/1.2.0+ksor.1
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

**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.

<!-- OPEN: recorded output for the first broken trace. No broken trace is recorded yet (spec, raised point 3f). The number of repeated `search_policy` calls, what the tool answered each time, the trace's last line, and its cost all come from the recorded trace. Until then the scene above gives no count. -->

### What are the three steps of a diagnosis?

**Name the failure, say where it began, then find the row nobody decided.** [24.7](../evaluating-ai-workers/naming-the-failure.md) gave the first step and [24.8](../evaluating-ai-workers/integration-layer-or-model-output.md) 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".[^anthropic-ccdv-f-exam-guide]

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.

<!-- EDITOR: whether step 2 may say where the faults of the three broken traces began. An earlier draft said that in every trace of this section the model answered the request it was given, and the fault began in the code around it. No broken trace is recorded yet, and Task 3 asks the reader that same question, so this draft says only "Do this for each trace before you look for a row." The other option: once the traces are recorded, and if all of them do begin in the code around the model, the sentence comes back with its evidence. -->

<!-- EDITOR: is the third trace a hallucination? The spec's change line for 27.6 says "The third trace is a hallucination", and this draft follows it here and in Task 3's "What good looks like". But 24.7 defines a hallucination as an answer that "states something that no tool result or document in the run supports", and in the third trace a note inside the run, saved for Cedar Ridge, holds the fact. So the name fits only if "in the run" means Brightline's own sources. The other option, closer to 24.7: the third trace has no name among the six either, step 1 says that the first three have none, and Task 3 says "None of the three has a name in 24.7's list". It also depends on how the Cedar Ridge note reaches the run, which is not recorded yet. -->

### 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".[^anthropic-ccdv-f-exam-guide] 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](an-orchestrator-and-its-subagents.md)). The correction is [21.8](../agent-loop-by-hand/the-stops-you-add-yourself.md)'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.[^claude-agent-sdk-subagents] The product form of this fault is a fork, which "copies the conversation so far instead of starting fresh."[^claude-agent-sdk-subagents] The correction is a subagent that starts fresh from its brief ([26.5](../context-engineering/isolation-with-subagents.md)). To stop forks, add a deny rule for `Agent(fork)`. A deny rule is a permission rule that always blocks one tool.[^claude-agent-sdk-subagents]
- *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](../context-engineering/memory-outside-the-window.md)).
- *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](../context-engineering/reading-context-use-in-a-trace.md) reads. Its correction is a summary that is told what to keep ([26.4](../context-engineering/compaction.md)).

<!-- OPEN: recorded output for the second and third broken traces. Neither is recorded. The first request of the subagent in the second, and the words of the Cedar Ridge note and of the answer that repeats it in the third, come from the recorded traces (spec, raised point 3f). Until then the list above quotes no line of either trace. -->

<!-- EDITOR: two choices in the list above. First, Lab 26's drift trace stands as the fourth case, for context-window management, as the spec proposes (raised point 17), and Lab 27 adds no fourth trace. The other option: Lab 27's kit gets a fourth broken trace of its own. Second, plan item C10's sentence for 23.17 about a note "saved for Cedar Ridge" is not in the book yet, so this page cites 26.6 for the rule and says nothing of 23.17 (raised point 14). The other option: C10 lands first, and the third item cites 23.17. -->

<!-- FIGURE 27.4: A table drawn as three columns and four rows, read left to right, titled "From the trace to the pattern". Column heads, left to right: "What the trace shows", "The row nobody decided", "The pattern". An arrow points right from each cell of the first column to the cell beside it in the second, and from the second to the third. No arrow points left, up or down. Row 1: "The same tool call repeats, and no limit stops it" -> "When the run stops" -> "Tool-use loop termination". Row 2: "A subagent's first request holds the parent's conversation" -> "What a helper sees" -> "Subagent isolation". Row 3: "A fact that no tool result of this company holds" -> "Whose notes the run may read" -> "Memory". Row 4: "A rule followed early and not late" -> "What stays in the window" -> "Context-window management". The middle column is shaded and carries a small label above it: "the four run rows of docs/architecture.md". The alt text must give all four rows, each read left to right. -->

*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_turns`[^anthropic-sdk-agent-loop] 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".[^anthropic-ccdv-f-exam-guide]

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](../agent-sdks/what-counts-as-done.md)) 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:[^anthropic-stop-reasons]

| What the trace shows | What 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 results | Look 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."[^anthropic-multi-agent-when] 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.

<!-- EDITOR: which later chapters does 27.6 point to after "one trace shows one run"? The plan gives this section a "going further" pointer. The book plan names Chapters 32 and 36, and the CCDV-P plan's item A3 names Chapters 31 and 35. Neither pair is in the book yet, so this draft names no chapter. The options: add one line with the book plan's two chapters, add one with the CCDV-P plan's two, or keep none until the chapters exist. -->

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.

[^anthropic-ccdv-f-exam-guide]: Claude Certified Developer - Foundations Exam Guide, version 1.1, Anthropic.
[^claude-agent-sdk-subagents]: Subagents in the SDK, Claude Agent SDK, Anthropic.
[^anthropic-sdk-agent-loop]: How the agent loop works, Claude Agent SDK, Anthropic.
[^anthropic-stop-reasons]: Stop reasons and fallback, Anthropic.
[^anthropic-multi-agent-when]: Building multi-agent systems: when and how to use them, Anthropic.
