Skip to the page

What is context engineering?

Choosing what goes into a model's context at each step of a task, what to fetch only when needed, and what to clear, note down or summarise as the context grows.

intermediate

A model answers from what is in its context windowContext WindowEverything a model can see at once while it writes an answer, counted in tokens. When the window is full, something has to be left out., and from nothing else it can see at that moment. Context engineering is the work of deciding what is in there at every step of a task. For a single question it is mostly writing the prompt. For an agentAgentA model that uses tools in a loop, deciding each next step from what the last one returned, until the task is done. that runs for 50 laps, it is the main job.

Why more is not better

Current Claude models can hold up to 1M tokens, so it is tempting to load everything. It does not work well. Anthropic's documentation names the effect context rot: as the token count grows, the model's accuracy and recall fall. Its engineers describe an attention budget that every token draws on, so each token you add competes with the ones that matter.

An agent fills its own context. Each tool result stays in the conversation and is sent again on every lap. A few large files or long command outputs can take up most of the context with text the agent has already used.

The main moves

  • Keep the fixed part small and clear. The system prompt and the tool definitions go out with every request, and each tool card costs tokens before any work starts.
  • Fetch just in time. Instead of loading every document up front, give the agent references, such as a file path, a stored query or a link, and tools to open them when needed. RetrievalRAGA way to answer from your own documents. A search finds the passages that match the question, and they are added to the prompt before the model writes. is one way to do it.
  • Clear what is spent. Once the agent has used a tool result, the full text can go. Context editing in the Claude API swaps the oldest tool results for a short placeholder after a threshold.
  • Write notes outside the context. The agent keeps a progress file, for example with a memory tool, and reads it back later. Anthropic's engineers call this structured note-taking.
  • Summarise when it gets long. CompactionCompactionReplacing the older part of a long conversation with a summary, so an agent can keep working when its context fills up. replaces the older turns with a summary and carries on from it.
  • Split the work. A subagentSubagentAn agent started by another agent for one part of a task. It works in its own fresh context and returns only its findings. does one focused part in its own fresh context and returns only what it found.

The trade-offs

Each move gives something up. A cleared result has to be fetched again if it turns out to matter. A summary keeps what seemed important when it was written, and an instruction from early in the conversation can be lost. Rules that must last belong in the system prompt or a project file that is sent with every request.

Clearing also changes the start of the conversation, which breaks prompt cachingKV CacheWork a model saves from the text it has already read, so it is not redone for every new token. Prompt caching keeps that work between requests and charges less for it. from that point. Clear in large pieces, not a little at a time.

Where to start

Look at what your agent actually sends. Count the tokens in a request on lap 1 and on lap 30, and see what share is old tool output. That share tells you which of the moves above to make first.

Sources

Where each fact comes from

  1. 1Effective context engineering for AI agents
  2. 2Context windows
  3. 3Context editing
  4. 4How the agent loop works