الانتقال إلى المحتوى
أساسيات الـ AIAI Agents· 7 دقائق للقراءة

Context Engineering: Why Preloading Everything Makes Agents Worse

An agent's context is a working budget, not a warehouse. Give it an index, fetch details on demand and clear what's stale.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

An on-call assistant gets everything "so it never lacks anything": the full runbook wiki pasted into the system prompt and sixty tools from five MCP servers. Turn one already sends a small book. By turn thirty, the context also holds every log dump the agent has pulled. It quotes a runbook page that was replaced last year, calls a restart tool from the wrong server, and each turn costs more than the last. Nothing was missing. There was too much.

The common misconception

"An agent works best when every document and instruction it might need is loaded into the prompt up front." It's a reasonable instinct:

  • 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.

The half-truth: an agent can't use what it never sees. But fitting in the window isn't the same as helping, and an agent keeps adding to its own context with every step.

What actually happens

Context is a budget the loop keeps spending

Anthropic describes as curating and maintaining the best set of tokens during inference: the system prompt, tools, external data and message history, not just the prompt. Unlike a prompt written once, it's iterative. An agent in a loop generates more data every turn, and something has to decide what goes into the next request.

As the long-context article showed, recall declines as inputs grow, well before the window is full. Anthropic calls this and describes an attention budget that every new token draws down. In an agent, the growth is built in: every file read, search result and log dump stays in the history and is sent, and billed, again on each turn. Anthropic's guiding principle is to find the smallest possible set of high-signal tokens that maximizes the chance of the outcome you want.

Tool definitions are context too

Anthropic's tool search docs say a typical multi-server setup (GitHub, Slack, Sentry, Grafana and Splunk) can consume about 55,000 tokens in definitions before Claude does any work, and that Claude's ability to pick the right tool degrades past 30 to 50 tools. The context engineering post names bloated, overlapping tool sets as one of the most common failures: if a human engineer can't say which tool fits a situation, the agent can't be expected to do better. When you really need hundreds of tools, the tool search tool lets Claude load only the 3 to 5 it needs, with defer_loading: true on the rest; the docs say it typically cuts definition tokens by over 85%.

Just in time, not just in case

Instead of loading data up front, a just-in-time agent keeps lightweight references, such as file paths, stored queries or links, and loads the content with tools when it needs it. Claude Code works this way: its 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.

The trade-off is real. Exploring at runtime is slower than precomputed retrieval, and without good tools and guidance an agent can waste context on dead ends. For less dynamic content, such as legal or finance work, retrieving more up front can be the better balance.

Clear what's stale, keep notes for what lasts

Long tasks need the context to shrink as well as grow. The Claude API offers three ways to do that:

  • 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.md file 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.

Sub-agents with their own clean context are a fourth option, with trade-offs of their own; the next article in this path covers them.

Anthropic reported the effect when it launched context editing and the memory tool alongside Claude Sonnet 4.5 in September 2025. On an internal agentic-search evaluation, context editing improved performance by 29% over baseline, and by 39% combined with the memory tool. In a 100-turn web search evaluation, it let agents finish workflows that would otherwise fail from context exhaustion while cutting token consumption by 84%.

Here is the on-call agent's first request both ways. The second sends an index of page titles and three tools, and turns on tool result clearing for the loop that follows.

Python oncall.py
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 turn

The second agent opens the one page it needs with read_page. Once a later request passes 30,000 input tokens, old log dumps give way to placeholders while the latest three tool results stay.

Preload everythingEngineer the context
DocumentsThe whole corpus in the system promptAn index, pages read on demand
ToolsEvery tool from every serverA minimal set, or tool search for large catalogs
Old tool resultsResent on every turnCleared once they're stale
Facts needed laterHoped to survive in the historyWritten to notes or memory
Cost per turnLarge from turn one, then growingSmall, and kept in check by clearing

How to apply it in your project

  1. Measure a long runLog input tokens per turn on a realistic session and see what fills them: system prompt, tool definitions, history or tool results.
  2. Swap documents for an indexKeep only the rules every request needs up front. List everything else by path and title, and give the agent a tool to open it.
  3. Trim the tool setMerge overlapping tools, return only the fields the agent needs, and use tool search when the catalog passes a few dozen tools.
  4. Clear, compact and take notesTurn on tool result clearing for tool-heavy loops, compaction for long sessions, and notes or memory for facts that must outlive both.
  5. Evaluate at full lengthCompare 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%.

📚 المصادر

  1. Effective context engineering for AI agents — Anthropic
  2. Context editing — Claude API Docs
  3. Compaction overview — Claude API Docs
  4. Tool search tool — Claude API Docs
  5. Managing context on the Claude Developer Platform — Anthropic

التعليقات (0)