Agents and Workflows
Agent Pattern Diagnosis
Exam objective: Analyzing a trace or description of a misbehaving agent to identify which design pattern is missing or misapplied, such as tool-use loop termination, subagent isolation, memory, or context-window management, and selecting the pattern that corrects the behavior.
What the exam tests
You are given a trace or a description of a misbehaving agent and must name the missing or misapplied pattern, then pick the fix that treats the cause. Work in this order: how did the loop end (or fail to end), what was in context at that moment, and what should have been carried across a boundary (turn, compaction, subagent or session).
Key rules
Loop termination
- A client-tool loop runs while
stop_reason == "tool_use". Every other value leaves the loop. A response can hold text and tool calls together. - Return one
tool_resultpertool_useblock, in one user message, with the results before any text. pause_turn(server-tool loop hit its iteration limit): append the assistant content unchanged and resend with the same tools, with no new user message.max_tokenswith an incomplete trailingtool_use: do not execute it; retry with a highermax_tokens.- Empty
end_turnafter tool results: look for text appended aftertool_resultblocks. - In the Agent SDK, bound open-ended runs with
maxTurns/maxBudgetUsdand check theResultMessagesubtype before readingresult.
while response.stop_reason == "tool_use":
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": run_tools(response.content)})
response = client.messages.create(messages=messages, **params)
handle_final(response) # end_turn, max_tokens, refusal, pause_turn, ...
Subagent isolation
- A non-fork subagent starts without the parent's history, tool results or system prompt. Put paths, errors and decisions in the delegation prompt.
- Only its final message returns, so delegation is the cure for a parent flooded by exploration.
- Restrict its
toolsto what the subtask needs. Cap depth, concurrency and spend if the agent tree runs away.
Context-window management
- Quality degrades as context grows, so problems appear before any hard limit.
- Stale tool results: tool result clearing (
clear_tool_uses_20250919). It invalidates the cached prefix when it clears;clear_at_leastmakes that worthwhile, andexclude_toolsprotects important results. - Long conversation near the limit: compaction (summary of older turns). Detail can be lost, so tune what the summary preserves.
- In the Agent SDK, standing rules go in CLAUDE.md, not only in the first prompt.
- Bloated or overlapping tool sets waste context and cause wrong tool choices.
- Load data just in time through file paths, queries or links instead of preloading everything.
Memory and multi-session work
- Nothing carries into a new conversation unless it is stored outside the context window, for example with the memory tool (client-side: your handler runs the file operations and must block path traversal).
- For long builds: a first session writes a feature list, progress log and startup script; later sessions read them, test the baseline, do one feature, verify end to end and record the result.
- Compaction and memory complement each other: one keeps the active context small, the other preserves what must survive the summary.
Traps
- Deciding the loop is over because the response contains text.
- Looping
while stop_reason != "end_turn": a refusal or truncation never satisfies the condition, so the loop does not exit on its own. - Treating an iteration cap as the fix. It is a backstop; the exit condition is still wrong.
- Raising
max_tokensto cure input growth. It limits output. - Picking a bigger context window when the problem is relevance, lost instructions or cross-session state.
- Expecting compaction to give a brand-new session any history.
- Writing "fix the bug we discussed" as a subagent prompt.
- Sending
tool_resultblocks for server tools.
Decide quickly
| Situation | Choice | Why |
|---|---|---|
| Agent announces a step, then stops | Key the loop on stop_reason | Text may precede a tool call |
Research answer cut off with pause_turn | Resend with the paused turn appended | The turn is unfinished |
| Old tool outputs dominate input | Tool result clearing | Removes what is no longer needed |
| Long dialogue nears the limit | Compaction | Keeps the conversation going |
Rule forgotten after compact_boundary | Move it to CLAUDE.md | Re-injected on every request |
| Exploration floods the main agent | Subagent | Only the summary returns |
| Subagent solves the wrong problem | Richer delegation prompt | It cannot see the parent history |
| New session repeats old work | Memory or progress files | State must live outside context |
| Agent declares victory early | Feature list plus end-to-end checks | Gives an explicit definition of done |