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

- Page: https://tecyfy.com/concepts/context-engineering
- Subject: AI agents (https://tecyfy.com/concepts)
- Level: intermediate

A model answers from what is in its [context window](https://tecyfy.com/concepts/context-window), 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 [agent](https://tecyfy.com/concepts/agent) 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. [Retrieval](https://tecyfy.com/concepts/rag) 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.** [Compaction](https://tecyfy.com/concepts#compaction) replaces the older turns with a summary and carries on from it.
- **Split the work.** A [subagent](https://tecyfy.com/concepts#subagent) 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 caching](https://tecyfy.com/concepts/kv-cache) 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.

## Related

- [Context Window](https://tecyfy.com/concepts/context-window): Everything 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.
- [Agent](https://tecyfy.com/concepts/agent): A model that uses tools in a loop, deciding each next step from what the last one returned, until the task is done.
- [Agent Memory](https://tecyfy.com/concepts/agent-memory): The ways an agent keeps what it needs beyond a single context window, such as summaries, notes in files, and saved facts it reads back later. The model itself remembers nothing.
- [KV Cache](https://tecyfy.com/concepts/kv-cache): Work 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.
- [Compaction](https://tecyfy.com/concepts#compaction): Replacing the older part of a long conversation with a summary, so an agent can keep working when its context fills up.
- [Subagent](https://tecyfy.com/concepts#subagent): An agent started by another agent for one part of a task. It works in its own fresh context and returns only its findings.
- [RAG](https://tecyfy.com/concepts/rag): A 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.

## Sources

- [Effective context engineering for AI agents](https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents)
- [Context windows](https://platform.claude.com/docs/en/build-with-claude/context-windows)
- [Context editing](https://platform.claude.com/docs/en/build-with-claude/context-editing)
- [How the agent loop works](https://code.claude.com/docs/en/agent-sdk/agent-loop)
