Context engineering is the practice of deciding everything a language model sees on each call: the instructions, retrieved documents, conversation history, tool definitions and tool results, and how much room each gets. Prompt engineering is one part of it, focused on how the instructions are worded. The difference matters most in agents and multi-step workflows, where a well-written prompt still fails if the model is fed stale documents, a bloated history, or twenty tools it does not need.
Featured image: “Artificial Neural Network with Chip” by mikemacmarketing, licensed under CC BY 2.0.
Below you will find a side-by-side comparison of the two disciplines, the components of a context window, a worked example that shows the same request with poor and with engineered context, and a checklist for auditing your own workflows.
Where the term comes from
The phrase gained traction in 2025 as teams moved from single chatbot turns to agents that run for many steps. Anthropic’s engineering team set out a working definition in Effective context engineering for AI agents (September 2025): curating and maintaining the best set of tokens during inference, including information beyond the prompt itself. The post frames the core question as which configuration of context is most likely to produce the behavior you want.
Two terms from that post are worth knowing:
- Attention budget. Context is a finite resource with diminishing returns. Every token you add uses some of the model’s capacity to attend to the rest.
- Context rot. As the number of tokens grows, the model’s ability to recall information from its context accurately goes down. Anthropic’s context window documentation says the same, and adds that curating what is in context matters as much as how much space is available.
Independent research points the same way. The 2023 paper Lost in the Middle found that models used relevant information best when it sat near the start or end of a long input, and noticeably worse when it was in the middle. More context is not automatically better context.
Context engineering vs prompt engineering, side by side
Prompt engineering asks how to phrase an instruction; context engineering asks what the model should be looking at when it reads that instruction. The table summarizes the practical differences.
| Prompt engineering | Context engineering | |
|---|---|---|
| Main question | How should the instruction be worded? | What should be in the window for this call? |
| Unit of work | One prompt or template | Every call in a workflow, plus what carries over between them |
| When it is decided | Mostly written once, reused | Assembled at runtime, often per call |
| Inputs it controls | Instructions, examples, output format | Instructions plus retrieved data, history, memory, tools and tool results |
| Typical failure | Ambiguous or contradictory wording | Missing, stale, irrelevant or excessive information |
| Typical fix | Rewrite, add an example, clarify format | Change retrieval, trim or summarize history, remove tools, restructure tool output |
| Who usually owns it | Anyone who writes prompts | Whoever builds the application or agent |
Neither replaces the other. A clean, relevant context with a vague instruction still gives vague answers. If you are new to the instruction side, start with prompt engineering from scratch.
What goes into a context window
Every model call is built from the same set of parts, and each is a separate lever. Anthropic’s documentation lists what counts toward the window: the system prompt, all messages (including tool results, images and documents), tool definitions, and the output of the current turn, including any extended thinking.
Instructions
The system prompt, formatting rules and examples. Anthropic’s post recommends what it calls the right altitude: specific enough to guide behavior, but not a brittle list of hard-coded if-then rules. It suggests starting minimal, adding instructions and examples in response to observed failures, and using a few diverse, canonical examples instead of trying to list every edge case.
Knowledge
Documents, database rows and search results relevant to the request, usually brought in through retrieval-augmented generation (RAG). The engineering decisions are what to retrieve, how many results, in what order, and with what labels (source, date, ID) so the model can tell them apart.
History and memory
Earlier turns of the conversation, plus anything stored outside the window and loaded back in, such as notes or user preferences. In a chat, each turn resends the whole history, so it grows with every exchange. Deciding what to keep verbatim, what to summarize and what to drop is a core context engineering task; our guide to keeping long AI conversations on track covers the chat-user side of this.
Tools and tool results
Tool definitions take space even when no tool is called, and tool results can be large. Standards such as the Model Context Protocol (MCP), which its documentation describes as an open-source standard for connecting AI applications to external systems, make it easy to connect many tools at once. That convenience is also a context risk: every connected tool adds definitions the model must read and choose between. Clear, non-overlapping tool descriptions help; see our checklist for writing tool descriptions.
Worked example: the same question, two contexts
The scenario below is illustrative. An internal IT helpdesk assistant receives this message from an employee on turn 14 of a chat: “The VPN still drops every few minutes, what else can I try?”
Before: everything goes in
[system prompt: 2,500 words, written over a year, including
rules for 30 different request types]
[tool definitions: 22 tools, including payroll, HR and calendar tools]
[retrieved docs: top 10 search hits for "VPN", including the
2022 guide for a VPN client the company no longer uses]
[full chat history: 13 turns, including 6 turns about a printer
problem that was solved earlier]
[a 4,000-line raw log from the "get_device_logs" tool, pasted as-is]
User: The VPN still drops every few minutes, what else can I try?
Likely problems: the answer cites steps for the retired VPN client because that document was retrieved; it repeats a fix the user already tried in turn 9, which is now buried in the middle; it may call an irrelevant tool; and the raw log consumes most of the attention budget while the one relevant error line is lost in it. None of these are wording problems, so rewriting the system prompt would not fix them.
After: assembled for this call
<instructions>
You are the IT helpdesk assistant. Use only the documents below for
troubleshooting steps. If the documents do not cover the problem,
say so and offer to open a ticket. Do not repeat steps listed under
already_tried.
</instructions>
<case_state>
topic: VPN disconnects every few minutes
device: company laptop, current VPN client
already_tried: restarted client; switched Wi-Fi network; reinstalled client
</case_state>
<documents>
<document source="kb-vpn-current" updated="2026-08">...</document>
<document source="kb-vpn-timeouts" updated="2026-05">...</document>
</documents>
<log_summary source="get_device_logs">
Last 24h: 11 disconnects. Repeated error: "keepalive timeout".
No authentication errors.
</log_summary>
[tools: search_kb, get_device_logs, create_ticket]
User: The VPN still drops every few minutes, what else can I try?
What changed, and which context engineering technique each change represents:
- Topic-scoped instructions. Only the rules relevant to troubleshooting are included, rather than all 30 request types.
- Filtered retrieval. Documents for the retired client are excluded by metadata, and each remaining document carries its source and date.
- Structured state instead of raw history. The printer conversation is gone; what matters from earlier turns is condensed into case_state, placed where it is easy to see.
- Summarized tool output. The raw log is reduced to the facts the model needs. Anthropic’s post recommends this kind of pruning, noting that clearing old tool results is one of the lightest forms of compaction.
- Fewer tools. Three relevant tools instead of twenty-two, which lowers both token use and the chance of a wrong tool call.
Techniques for long-running agents
When a task runs longer than one context window can comfortably hold, three patterns from Anthropic’s post do most of the work:
- Compaction. Summarize the conversation as it approaches the limit and continue in a fresh window with the summary. The post advises tuning the summary for recall first (do not lose anything important), then trimming for precision. Anthropic’s API documentation also describes server-side compaction, currently in beta for recent Claude models.
- Structured note-taking. The agent writes progress notes, a to-do list or decisions to a file or memory store outside the window, and reads them back later. This keeps key state available without carrying the full history.
- Sub-agents. A coordinating agent hands focused sub-tasks to sub-agents that each work in a clean context and return only a condensed result. The detailed exploration never enters the main agent’s window.
The post also describes just-in-time retrieval: instead of loading everything up front, the agent keeps lightweight references such as file paths or saved queries and fetches content with tools when it needs it. Anthropic notes that Claude Code combines both approaches, loading a small project instructions file at the start and searching for other files as needed.
A context audit checklist
Before rewriting a prompt that is underperforming, log the full context of a few failing calls and go through this list. Each item notes why it matters.
- Can you see the exact context that was sent? If you only log the user message, you cannot diagnose context problems. Log the full request for at least a sample of calls.
- Is anything stale? Old documents, superseded policies, outdated product data. Add dates to retrieved content and filter on them.
- Is anything irrelevant? Earlier topics in the history, off-topic search hits, instructions for unrelated tasks. Each one competes for attention.
- Is anything missing? Facts the model needed but had to guess, such as what the user already tried, or the user’s plan or region.
- Are static and dynamic parts separated? Keep stable instructions at the start and changing data after them. This also makes prompt caching more effective, though cached tokens still count toward the window.
- Is the important information in a visible position? Put the current task state and the actual question near the end, not buried mid-context.
- Are tool results raw? Summarize, filter or truncate large outputs before passing them back.
- Are there more tools than the task needs? Expose only the tools relevant to the current step, and make sure their descriptions do not overlap.
- Is the history managed? Decide in code what gets summarized and when, rather than letting it grow until it hits the limit.
- Is the content clearly labeled? Tags or headings for instructions, documents, state and user input help the model tell them apart, and help defend against instructions hidden inside retrieved content.
Common questions
Does a bigger context window make context engineering unnecessary?
No. Several current models accept around a million tokens, but a larger window raises the ceiling without changing the context rot and positional effects described above. It also raises cost and latency per call. The goal remains the smallest set of high-signal information that does the job. Our explainer on context windows covers sizes and limits.
Is context engineering only relevant for agents?
It matters most for agents because they make many calls and accumulate tool results. But the same principles apply to a single RAG query or a long chat: give the model relevant, current, well-labeled information and leave out the rest.



