Task statement 6.1 · Prompt and context engineering

6.1 · Context Engineering

Shelfwise, the course example, answers a stock question in about three requests of about 7,000 input tokens each, far below a Haiku-family model's 200,000-token context window. Context engineering still decides what fills those tokens: which tool output, which history and which instructions reach Claude.

Why it's tested

The tempting fix for an agent that slows down or loses track is more room: a larger context window, or compaction everywhere. A larger window only delays the problem, because recall degrades as the window fills, and compaction summarises away exact values and early instructions. The cheaper fix usually sits earlier, in what the tools return and which steps share one window.

Start hereThree terms this section adds

context rot
The docs' name for accuracy and recall degrading as the number of tokens in the context window grows.
context drift
Stale or conflicting instructions and facts building up in the window, so Claude works from an outdated version.
context bloat
Tool output and history that the current step does not need, crowding the window.

KnowledgeContext window management

Everything in a request counts toward the context window: the system prompt, tool definitions, every message including tool results, and Claude's output, thinking included. Each turn resends the whole history (2.3), which grows with every tool result (5.4 covers what that costs). The context-windows docs warn that more context is not automatically better, because accuracy and recall degrade as the token count grows, which they call context rot. Anthropic's context-engineering post therefore treats context as an attention budget, to be spent on the smallest set of high-signal tokens. Context window management is deciding, at each step, what earns a place in that budget, so a window with room to spare can still be badly managed.

KnowledgeContext drift and bloat

Two failures follow when the window fills without management. Bloat is volume: raw tool output and old turns the current step does not need, which cost tokens on every request and dilute recall. Drift is staleness: an early instruction lost in a summary, a corrected value beside the original, or an old tool result read as current. Claude Code's docs warn of the lost-instruction case: early instructions may be lost when context fills up. The fixes differ, because bloat needs less content while drift needs the current version to win.

More precise than the guide. "Context drift" and "bloat" are the guide's words, not docs terms (as of 2026-10-05): the docs say context rot, and the context-engineering post speaks of context pollution and bloated tool sets, so the definitions here are this course's framing. Exam answer: drift is stale or conflicting content in the window, and bloat is content the current step does not need.

Interactive · what each strategy leaves in the main context window

Four bars show the main context window of one agent task at the same late step, each read from left to right in tokens, with a dotted cache breakpoint after the cached prefix and a dashed window limit at the right. No management: the prefix, then a long run of old tool output and turns, then recent turns, reaching the window limit. Prune at the tool: the prefix, short trimmed tool output and recent turns. Clear or compact: the prefix, a short summary or placeholders, and recent turns, with the part after the breakpoint written to the cache again. Isolate in a subagent: the prefix, recent turns and a short summary returned from a subagent whose own context window, drawn outside the bar, holds all the reading. CONTEXT USED (TOKENS) → cache breakpoint window limit No management bloat near the limit prefix old tool output and turns recent Prune at the tool less enters the window prefix trimmed output recent Clear or compact removed after the fact prefix summary recent after the breakpoint: written to the cache again Isolate in a subagent reading stays outside prefix recent summary subagent's own window ALL THE READING returns SAME TASK, SAME STEP · BAR LENGTH = TOKENS IN THE MAIN WINDOW

Choose a strategy to see what stays in the main context window.

No management: every raw tool result and old turn stays and is resent on each request, so the window nears its limit and recall degrades well before it gets there.

Prune at the tool: the tool returns only the fields and matches the next step needs, so the history grows slowly and the cached prefix never changes.

Clear or compact: context editing swaps old tool results for placeholders, or compaction swaps older turns for a summary. The window shrinks, but the part after the breakpoint is cached again and exact detail can be lost.

Isolate in a subagent: the subagent reads in its own context window and only its summary enters the main window, so the reading can neither crowd it nor go stale there.

Blue is the cached prefix (system prompt and tools), grey old history, green tool or subagent output, amber recent turns, dashed grey a summary or placeholders, and dashed ember a context window.

KnowledgeTool-output pruning

Pruning starts at the tool, before anything enters the window. A tool can return only the fields the next step needs, filter or page its results, and truncate with a note on how to narrow the search (8.1 covers tool design). Claude Code caps MCP tool output at 25,000 tokens by default. Results that were useful once can be cleared later with context editing (1.3), whose clear_tool_uses_20250919 edit can spare any tool named in exclude_tools. Clearing changes the conversation after the cache breakpoint, so the docs advise setting clear_at_least so that each clear removes enough tokens to pay for the cache write it causes.

KnowledgeCompaction, and what it loses

The API's threshold compaction, a beta compact_20260112 edit, summarises once input reaches a trigger (150,000 tokens by default, at least 50,000). It returns a compaction block, and on later requests the API drops everything before it. Its instructions field replaces the default summary prompt entirely, so a custom prompt must name everything the summary has to keep. On-demand compaction (beta header compact-2026-09-04) lets your code ask for the summary instead. Both run only on Claude 4.6 and later models. Whatever the summary omits is gone: exact values, instructions given in earlier turns, images and documents. So compaction keeps the task but loses the wording. A cache_control breakpoint at the end of the system prompt keeps that prompt cached, so only the summary is written to the cache. The grid compares the three surfaces.

Claude API

Threshold or on demand, both beta. pause_after_compaction lets you put recent turns back word for word.

Claude Code

Auto-compacts near the limit, clearing older tool outputs first; /compact focus on … steers the summary. The system prompt and root CLAUDE.md survive; path-scoped rules do not.

Agent SDK

Compacts automatically, emitting a compact_boundary message after a PreCompact hook. Sending /compact as a prompt compacts on demand.

KnowledgeContext isolation

Context isolation keeps work out of the main thread instead of cleaning up afterwards. A subagent reads in its own context window and returns only its final response, the condensed summary that 1.3 describes, so the main window grows by that summary, not the transcript. A multi-step workflow does the same in code: in prompt chaining, each call receives only the previous step's output, which your code can check before passing it on. Isolation prevents bloat because the reading never enters the main window, and it prevents drift because each step starts from the inputs you chose, not from everything said before. The price is a fresh start: a subagent sees none of the parent's history and cannot reuse its cached prefix.

SkillWorked example: bloat in a window far from full

Shelfwise already isolates each staff chat as its own session, resumed by session_id (2.5), with auto memory off (1.3). Its context engineering is mostly in what stock_lookup returns, as a branch trial showed:

# branch: fuzzy search returns every match's full feed record
"any drill bits at Leeds?" → 38 matches, full records
requests ≈19,000 tokens (was ≈7,000); eval 146/150, 4 wrong-size counts; cost over $0.01
# fix: at most 5 records in the usual shape, plus the total
{"matches": 38, "records": [{"source": "stock_feed", "sku": "DB-4MM", "count": 12, ...}]}
eval 150/150, requests back to ≈7,000; Claude asks which size

This was bloat in a window nowhere near full, so neither compaction nor context editing fits. A chat of about three requests never reaches a trigger, server-side compaction does not run on Haiku-family models, and each clear would rewrite the cache after the ≈6,000-token prefix. Pruning at the tool shrinks every request with no beta feature, keeping answers under $0.01.

When the window holds the wrong things, ask: what is filling it, and does the next step need it? Tool output the next step does not need → prune at the tool, or clear old results with context editing. A long history the task still needs in outline → compact, after writing exact values where no summary reaches, such as a memory-tool file (1.3). Reading whose detail the main thread never uses → isolate it in a subagent or a chained call that returns only the result.
Takeaway
  • Context rot: recall falls as the window fills, so aim for the fewest high-signal tokens, not the largest window.
  • Clearing or compacting rewrites the cache after the breakpoint, so end the system prompt with a breakpoint and clear in large steps.
  • Compaction keeps the system prompt but summarises instructions given in turns.

check yourself

0 / 3 answered

A contract-review agent caches its 30,000-token system prompt and its conversation. The team turns on tool-result clearing at 60,000 input tokens, keeping three results, and cost per task rises 40% with the same tool calls. From turn 12, requests seconds apart each show about 30,000 cache-read tokens, 25,000 cache-write tokens and 2,000 tokens cleared.What is the most likely root cause?

Select one.

A foundation's grant agent reads 12 applications in one conversation, scoring each, then writes the panel's ranking memo. The chair replaced the rubric after application 7, and the memo still cites two scores from the old rubric. The steps never change, and the foundation wants each step's output checked by code before the next step uses it.Which design most directly keeps old rubric scores and earlier applications out of the memo step, with checks between steps?

Select one.

A data engineer works with a Messages API assistant through a day-long warehouse migration, with threshold compaction at the default trigger. After compactions the assistant went back to a column mapping the engineer corrected hours earlier, and the 0.5% row-count tolerance she set now appears in the summary only as "a small tolerance".Which TWO changes most directly keep the mapping fix and the 0.5% tolerance across compactions?

Select two.

sources

  1. Claude API Docs, "Context windows": everything in the request and the output counts toward the window; accuracy and recall degrade as token count grows, called context rot; compaction in beta for Claude 4.6 and later models. Checked 2026-10-05.
  2. Anthropic Engineering, "Effective context engineering for AI agents": the attention budget; the smallest set of high-signal tokens; context pollution; bloated tool sets; tool result clearing as light-touch compaction. Checked 2026-10-05.
  3. Claude API Docs, "Context editing": clear_tool_uses_20250919, exclude_tools and clear_at_least; tool result clearing invalidates cached prefixes. Checked 2026-10-05.
  4. Claude API Docs, "Compaction at a token threshold" and "Compaction on demand": compact_20260112, the 150,000 default and 50,000 minimum trigger, the compaction block, content before it dropped, instructions replacing the default prompt, pause_after_compaction, a breakpoint at the end of the system prompt; compact-2026-09-04; images and documents lost; supported models. Checked 2026-10-05.
  5. Claude Code Docs, "How Claude Code works" and "Explore the context window": older tool outputs cleared first, then a summary; early instructions may be lost; /compact with a focus; what survives compaction. Checked 2026-10-05.
  6. Claude Code Docs, "How the agent loop works": automatic compaction, the compact_boundary message, the PreCompact hook, /compact sent as a prompt; a subagent returns only its final response. Checked 2026-10-05.
  7. Anthropic Engineering, "Writing effective tools for agents": pagination, filtering and truncation for token efficiency; Claude Code's 25,000-token default limit on tool responses. Checked 2026-10-05.
  8. Anthropic Engineering, "Building effective agents": prompt chaining, where each call processes the previous one's output, with programmatic checks between steps. Checked 2026-10-05.
  9. The fuzzy-search branch and the five-record stock_lookup result are this course's additions to Shelfwise.