
Context Engineering: Why Preloading Everything Makes Agents Worse
- كاتب موثّق
محتويات المقال
The common misconception
Context windows are huge: current Claude models accept 1M tokens. Missing context really does cause failures; the retrieval articles in this path were about exactly that. Preloading is simple and predictable, and prompt caching makes a repeated prefix cheaper. Every fetch is another tool call and another round trip, so loading up front feels faster.
What actually happens
Context is a budget the loop keeps spending
Tool definitions are context too
defer_loading: true on the rest; the docs say it typically cuts definition tokens by over 85%.
Just in time, not just in case
CLAUDE.md files go into context up front, while glob and grep let it find files on demand. The references carry signal too: a folder name, file size or timestamp hints at what's inside. Anthropic calls this mix of a few always-needed rules up front and everything else on demand a hybrid strategy.
Clear what's stale, keep notes for what lasts
Tool result clearing (context editing, beta) replaces the oldest tool results with a placeholder once the prompt passes a threshold, by default 100,000 input tokens, keeping the 3 most recent tool uses. It runs on the server before the prompt reaches Claude, and your own history stays complete. (beta) replaces older turns with a summary Claude writes on the server. The docs give the reason: response quality degrades as a conversation grows. Notes outside the window, such as a NOTES.mdfile or the memory tool, hold facts the agent needs later. With context editing and the memory tool together, Claude gets a warning to save important information before results are cleared.
import anthropicclient = anthropic.Anthropic()pages = load_wiki() # every runbook page, "just in case"docs = "\n\n".join(f"## {p.title}\n{p.text}" for p in pages)reply = client.messages.create( model="claude-opus-5-5", max_tokens=16000, system=f"You are the on-call assistant.\n<runbooks>\n{docs}\n</runbooks>", tools=ALL_TOOLS, # every tool from five MCP servers messages=[{"role": "user", "content": "Why does checkout return 502?"}],)print(reply.usage.input_tokens) # resent and billed on every turnimport anthropicclient = anthropic.Anthropic()index = "\n".join(f"- {p.path}: {p.title}" for p in load_wiki())reply = client.beta.messages.create( model="claude-opus-5-5", max_tokens=16000, system=f"You are the on-call assistant. Runbook index:\n{index}", tools=[READ_PAGE, SEARCH_LOGS, LIST_DEPLOYS], # what on-call needs messages=[{"role": "user", "content": "Why does checkout return 502?"}], betas=["context-management-2025-06-27"], context_management={"edits": [{ "type": "clear_tool_uses_20250919", # drop stale tool results "trigger": {"type": "input_tokens", "value": 30000}, "keep": {"type": "tool_uses", "value": 3}, }]},)print(reply.usage.input_tokens) # the index, not the whole wikiread_page. Once a later request passes 30,000 input tokens, old log dumps give way to placeholders while the latest three tool results stay.
How to apply it in your project
Measure a long run Log input tokens per turn on a realistic session and see what fills them: system prompt, tool definitions, history or tool results. Swap documents for an index Keep only the rules every request needs up front. List everything else by path and title, and give the agent a tool to open it. Trim the tool set Merge overlapping tools, return only the fields the agent needs, and use tool search when the catalog passes a few dozen tools. Clear, compact and take notes Turn on tool result clearing for tool-heavy loops, compaction for long sessions, and notes or memory for facts that must outlive both. Evaluate at full length Compare success rate and tokens over complete runs, not just the first turn, before and after each change.
Key takeaways
An agent's context is an attention budget spent on every turn, not storage that's free to fill. Preloaded documents, unused tool definitions and stale tool results compete with what matters and are billed every turn. Load just in time: give the agent an index and tools to fetch details, and keep the tool set small. Clear stale results, compact long histories and keep lasting facts in notes outside the window. Measure over full runs: in Anthropic's evaluation, context editing alone improved agentic search by 29%.
التعليقات (0)