Prompt and Context Engineering
Context Management for Long-Running Sessions
Exam objective: Analyzing context bloat or drift in a long-running session or agent and selecting the management technique that restores intended behavior, such as compaction, tool-output pruning, subagent isolation, or criteria for starting a fresh session.
What the exam tests
You get a long-running session or agent that has become bloated, forgetful, or off course, and you pick the technique that restores the intended behavior. First identify what is filling or distorting the context: tool definitions, old tool results, conversation history, one oversized read, stale unrelated work, or state that never left the conversation.
Key rules
- More context is not automatically better. Accuracy and recall degrade as tokens grow (context rot). Aim for the smallest set of high-signal tokens.
- Compaction replaces older turns with a summary. On the API, server-side compaction (beta) is the primary strategy for long conversations; it can run on demand or at a token threshold, and recent turns can be kept word for word. A custom summarization prompt replaces the default, so tune it on real traces: recall first, then precision.
- Context editing (beta) clears content by rule, server-side, while your client keeps the full history.
clear_tool_uses_20250919removes the oldest tool results. Tunetrigger,keep,clear_at_least,exclude_tools, andclear_tool_inputs.clear_thinking_20251015manages earlier thinking blocks. When combined, list it first.- Clearing invalidates the cached prefix, so make each clear worth it.
- Tool context: tool search defers definitions; programmatic tool calling keeps intermediate results out of history; prompt caching only lowers price.
- Memory and notes keep state outside the window. The memory tool is client-side: your application runs the file operations. It pairs with both compaction and context editing.
- Subagents isolate verbose work. A non-fork subagent starts without the parent conversation, so put paths, errors, and decisions in its prompt. Only its final message returns, so ask for a distilled summary.
- Fresh session: use it for unrelated work (
/clearin Claude Code), or when state can be rebuilt from progress files, tests, and git. Tell the new session exactly how to get its bearings. - Claude Code compaction:
/compact <focus>steers the summary. Project-root CLAUDE.md is re-injected afterwards; path-scoped rules and nested CLAUDE.md files return only when a matching file is touched; instructions given only in chat can be lost.
{"context_management": {"edits": [{
"type": "clear_tool_uses_20250919",
"trigger": {"type": "input_tokens", "value": 30000},
"keep": {"type": "tool_uses", "value": 3},
"clear_at_least": {"type": "input_tokens", "value": 5000},
"exclude_tools": ["web_search"]
}]}}
This request needs the beta header context-management-2025-06-27. Check context_management.applied_edits in the response to see what was cleared.
Traps
- "Cache it" as a fix for a full window. Cached tokens still occupy context.
- "Use a bigger window" as a fix for drift. It delays overflow and does not remove noise.
- Compaction when one output refills the context. Read targeted ranges or delegate the read.
- Assuming compaction carries project state across many sessions. Write a feature list and progress log, commit, and verify before marking work done.
- Clearing results that are needed later without first saving the facts, or protecting every tool so nothing can be cleared.
- Delegating tightly coupled work. Frequent back-and-forth, shared context between phases, and latency-sensitive quick edits belong in the main conversation.
- Resuming on another host without the transcript. Session files are local; pass the results into a fresh session or use a session store.
Decide quickly
| Situation | Choice | Why |
|---|---|---|
| Long dialogue nearing the limit, decisions must carry on | Compaction | Keeps continuity in fewer tokens |
| Old file or search results dominate | Tool result clearing | Removes stale bulk, leaves the rest |
| Cleared results hold facts needed later | Add the memory tool | Facts persist outside the window |
| Many tool schemas, few used | Tool search | Definitions load on demand |
| Chain of calls with bulky intermediates | Programmatic tool calling | Intermediates never enter history |
| Verbose, self-contained investigation | Subagent | Only the summary returns |
| Next task is unrelated | Fresh session / /clear | Old history is pure noise |
| Work spans many context windows | Progress file, checklist, git | A new context recovers state reliably |
| Rule vanished after compaction | Project-root CLAUDE.md | Re-read from disk |