Rubric
Contents — domains, guide and mocks

Loops, subagents, memory and context

CCDV-F 1.314 min read · checked 21 September 2026

Task statementAgent Patterns and Frameworks (4.9%) — tool-use loops, sub-agents, memory, context-window management, and agentic abstraction frameworks

The tool-use loop

  1. Send contextsystem prompt, tools, history
  2. Model respondstext, tool calls, or both
  3. Execute toolsyour code runs each one
  4. Append resultsone result per call, matched by id

tool calls → round again · no tool calls → final answer

One pass through this cycle is a turn. The loop ends when the model answers with no tool calls at all — never when it merely sounds finished.

The loop, and the two details that break it

Every agent is this cycle. Claude receives the prompt, the system prompt, the tool definitions and the history; it responds with text, one or more tool calls, or both; your harness executes them and feeds the results back; and the cycle repeats. Each full cycle is one turn. The Agent SDK does all of this for you and ends the loop when Claude produces a response with no tool calls.

Two details cause most hand-written loops to misbehave. The first is the stopping rule: a turn that contains friendly text can also contain a tool call, so the presence of text proves nothing — the API's stop_reason does. The second is bookkeeping: one response can request several tools, and every tool_use block needs its own tool_result carrying the same tool_use_id, appended after the assistant turn that asked for it. Handling stop reasons is the subject of 4.1, and the error side of tool results is covered there too.

Concurrency is a property of the tool, not of the loop. When Claude requests several calls in one turn, read-only tools such as Read, Glob and Grep can run at the same time, while tools that change state — Edit, Write, Bash — run one after another so they cannot collide. A custom tool is sequential by default and opts into parallel execution with the readOnlyHint annotation.

Subagents: a second desk in another room

A subagent is a separate agent instance with its own conversation. What matters for design is the boundary. A non-fork subagent starts with no parent history at all: it receives its own system prompt, the prompt string the Agent tool passed it, its tool definitions, and project memory files unless told to omit them. It does not receive the parent's conversation, tool results or system prompt. When it finishes, only its final message returns to the parent as the tool result.

What crosses the boundary

Goes in

  • The subagent's own system prompt
  • The prompt string from the Agent tool
  • Its tool definitions, or the subset in tools
  • Project CLAUDE.md, unless omitted

Stays behind

  • The parent's conversation and tool results
  • The parent's system prompt
  • Everything the subagent read while working
  • Skill content it was not given
The narrow channel in both directions is the point. It is also the trap: anything the subagent needs must be written into the prompt you send it.

That asymmetry is what makes subagents a context-management technique. A subagent that reads forty files costs the parent one summary. It is also why vague delegation fails: a brief like “look into the outage” leaves the subagent without the file paths, error messages and decisions the parent already has. State the objective, the output format, which tools to use and where the task ends. The architecture side of this — managers, supervisors, when to delegate at all — is 1.1.

Memory: three different things with one name

Ask three developers what agent memory means and you get three answers, and all three appear on this exam.

KindWhat it isLives
Session continuityThe conversation transcript, resumed or forked by session_idOn disk with the harness, or in a session store you supply
Memory filesNotes the agent writes and reads back — the API memory tool, or CLAUDE.md-style instruction filesFiles, outside the conversation
External statePlans, findings and artefacts written to storage, referenced by pointerYour database, object store or filesystem

Sessions are the cheapest of the three. The SDK writes the conversation to disk automatically; capture session_id from the result message and pass it back as resume to carry on with everything the agent already read and decided. fork_session / forkSession copies the history into a new session so you can try a second approach without losing the first, and the original keeps its own id. Be clear on the limit: a session persists the conversation, not the filesystem.

Fork a session to try a different approachpython
# session_id came from ResultMessage.session_id on an earlier run
async for message in query(
    prompt="Instead of JWT, outline how OAuth2 would work here",
    options=ClaudeAgentOptions(
        resume=session_id,      # start from that conversation
        fork_session=True,      # ...but branch: the original is untouched
        max_turns=5,
    ),
):
    if isinstance(message, ResultMessage):
        forked_id = message.session_id   # a new id, not the original

Memory files are for what must survive the window. Anthropic's research system writes the lead agent's plan to external storage precisely because context can be exceeded mid-run, and subagents store large outputs externally and pass lightweight references back rather than copying everything through the conversation. The API's memory tool formalises the same idea: Claude is warned as the clearing threshold approaches, writes what matters to memory files, and can read it back on demand.

Managing the window

The context window does not reset between turns. The system prompt, tool definitions, memory files, every prompt, every response, every tool input and every tool output accumulate until the session ends. Large tool outputs dominate: one verbose command or one big file can cost thousands of tokens in a single turn.

What is taking up the window

fixed at the top, growing at the bottom

  1. System promptsmall, every request
  2. Tool definitionsper tool; MCP schemas can be deferred
  3. Memory filesre-sent each request, prompt-cached
  4. Conversation historyprompts and responses
  5. Tool inputs and outputsgrows fastest by far
Only the bottom layer grows without bound. That is why the fixes target tool output rather than the prompt.

There are four levers, and a good design uses more than one. Compaction: when the window approaches its limit the SDK summarises older history automatically and emits a compact_boundary message. Context editing: on the API, the clear_tool_uses_20250919 strategy clears old tool results server-side once a trigger is passed, keeping the most recent few. Isolation: push wide reading into subagents. Restraint: fewer tools in the definition list, lower-cost tool outputs, and deferred MCP schemas.

Clear old tool results, keep a memory filepython
response = client.beta.messages.create(
    model=MODEL, max_tokens=4096, messages=messages,
    tools=[{"type": "memory_20250818", "name": "memory"}],
    betas=["context-management-2025-06-27"],
    context_management={"edits": [{
        "type": "clear_tool_uses_20250919",
        "trigger": {"type": "input_tokens", "value": 30000},  # start clearing here
        "keep": {"type": "tool_uses", "value": 3},            # keep the latest 3
        "exclude_tools": ["web_search"],                      # never clear these
    }]},
)

Abstraction frameworks: how much should be hidden

An agentic framework packages the loop, the tool plumbing and often the prompts. The Agent SDK is one; several third-party libraries are others. The caution in “Building effective agents” is specific: frameworks add layers that obscure the underlying prompts and responses, which makes debugging harder, and they tempt you into complexity when a simpler pattern would do. The advice is to start with the API directly, understand what is being sent, and adopt a framework once you can predict what it will produce.

Is this abstraction earning its place?

  • Passes: You can see the exact prompt and tools that reach the APIRequired for debugging and for cost work
  • Passes: You can read the termination state of a runSuccess, limit hit, or error
  • Passes: Guardrails run as code, not as instructionsHooks or permission rules
  • Check: You know what it does when the window fillsCompaction, clearing, or silent truncation?
  • Fails: It hides retries and errors behind a single callYou cannot tell a model failure from a transport failure
  • Fails: Adopted because it was familiar, not because of a needThe named anti-pattern
Run a candidate framework — or your own wrapper — past these. Two or more failures and you are buying debugging difficulty rather than leverage.

Traps the wrong answers are built from

Tempting but wrongDo this instead
Ending the loop when the model's reply contains textEnd when the response carries no tool calls — read the stop reason, not the prose.
Delegating with a one-line prompt and assuming the subagent can see the parent's workWrite the file paths, errors and decisions into the Agent tool prompt; nothing else crosses.
Putting a permanent rule in the opening user messagePut it in a memory file that is re-injected each request, and enforce it with a hook.
Letting raw tool output accumulate until the window fillsClear old tool results at a trigger, isolate wide reading in subagents, and keep outputs small.
Adopting a framework for the loop before you have written oneBuild against the API first so you can see the prompts the framework will later hide.

You should now be able to

  • Describe a tool-use loop in terms of turns, tool results and the condition that ends it.
  • State exactly what a subagent inherits and what it does not, and brief one accordingly.
  • Distinguish session continuity, memory files and external state, and pick the right one.
  • Name what consumes the context window and apply compaction, context editing and isolation to control it.
  • Judge whether an agentic framework is hiding detail you will need when something breaks.

Practice questions

Original questions written for this lesson, in the exam’s style. Answer first, then open the reasoning — every option is explained, including why the wrong ones are tempting.

  1. Question 1

    A research agent delegates to a subagent with the prompt “continue the analysis we discussed”. The subagent returns a generic summary that ignores the three files the parent had already read.

    What explains the behaviour?

    1. AThe subagent ran out of turns before it could read the files.
    2. BSubagent tool permissions defaulted to read-only, blocking file access.
    3. CA subagent starts with no parent history, so “we discussed” refers to nothing it can see.
    4. DThe parent summarised the subagent's output before returning it.
    Show answer and reasoning
    1. AIncorrect. A turn limit produces partial output on work attempted, not a generic answer to a task it never understood.
    2. BIncorrect. Tool restriction would surface as a missing tool, not as a misunderstood brief.
    3. CCorrect. Only the prompt string crosses the boundary; the parent's conversation and tool results stay behind.
    4. DIncorrect. Parent summarising affects what the user sees, not what the subagent produced.
  2. Question 2

    An agent processes long support transcripts. By turn 60 requests are slow and expensive, and the biggest contributors in the trace are repeated document-retrieval results from early in the run.

    Which change addresses the cause most directly?

    1. AClear old tool results at a token trigger, keeping the latest few.
    2. BShorten the system prompt and remove examples from it.
    3. CLower max_tokens so every response the model writes is shorter.
    4. DSwitch to a model with a larger context window and change nothing else.
    Show answer and reasoning
    1. ACorrect. It removes exactly the content identified in the trace — stale tool output — while leaving recent results intact.
    2. BIncorrect. The system prompt is a small fixed cost and is prompt-cached; it is not what is growing.
    3. CIncorrect. That caps output length and risks truncation; the input side is the problem.
    4. DIncorrect. It postpones the wall without reducing cost per request, and the stale results still crowd the window.
  3. Question 3

    A team wants an agent to answer a follow-up question a day later without re-reading the twelve files it analysed, and also wants to explore a second approach without losing the first thread.

    Which combination does this correctly?

    1. AStore the final answer in a database and prepend it to a fresh prompt each time.
    2. BResume the saved session_id, and fork it when exploring the alternative.
    3. CRun both approaches as subagents from a new session each morning.
    4. DDisable compaction so nothing is ever summarised away.
    Show answer and reasoning
    1. AIncorrect. It preserves a conclusion but not the analysis, and it does not give two independent threads.
    2. BCorrect. Resuming restores the full prior context; forking copies that history into a new session and leaves the original id intact.
    3. CIncorrect. Subagents start fresh with no history, so the twelve files would be read again.
    4. DIncorrect. Compaction concerns one long run, not returning to a conversation tomorrow, and cannot be relied on as storage.

Sources

Drafted with AI assistance and checked against the sources above; expert review is in progress. Spotted an error? Tell us and it gets fixed, dated and listed on how this is written.