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.
share
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
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.
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.
Threshold or on demand, both beta. pause_after_compaction lets you put recent turns back word for word.
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.
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.
- 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.