People associate prompts with short task descriptions you’d give an LLM in your day-to-day use. When in every industrial-strength LLM app, context engineering is the delicate art and science of filling the context window with just the right information for the next step.
The principles are older: retrieval augmented generation (RAG), chat memory, and tool calling were early forms of it, and the 12-factor agents guide lists "own your context window" as one of its principles.
On any single call, the context window holds several kinds of information:
- System instructions that set behavior and rules
- Memory: conversation history and durable facts about the user
- Retrieved knowledge from documents, databases, or the web
- Tool definitions and the results of earlier tool calls
- The user question itself

Everything else the application knows stays outside the window until a step needs it. Context engineering is the logic that makes that choice on every call.
Why Is Context Engineering Important?
A model can only work with what is in its context window, so the quality of that window sets the ceiling on the quality of the answer. Four benefits follow from managing it on purpose.
Bigger context windows don't solve it
Models get worse as their input grows, long before the window is full. Chroma's context rot report tested 18 models and found that "model performance consistently degrades with increasing input length," even on simple tasks, and that a single distractor passage lowers accuracy. Anthropic's current API documentation gives the same reason for its compaction feature: it keeps the active context small "because response quality degrades as a conversation grows."
Answers grounded in your own data
A model's training data stops at a cutoff and never includes your internal documents. Selecting the right passages, records, and tool results for each question lets it answer from current, proprietary information, which reduces hallucinations in production assistants.
Agents that stay on task
Agents call a model dozens of times, and each call adds tool output to the history, so early mistakes pile up. In LLMs Get Lost in Multi-Turn Conversation, published at ICLR 2026, Microsoft and Salesforce researchers gave 15 models the same tasks either in one message or spread across turns: average performance fell from 90% to 65%, and every model they tested dropped.
Lower cost and latency
Every token in the window is paid for and processed on every call. In the RAG-MCP paper, retrieving only the relevant tool descriptions cut prompt tokens by more than 50%. Putting stable content, such as instructions and tool definitions, at the start also lets prompt caching reuse it.
How Does Context Engineering Work?
Context engineering works in 5 steps that repeat on every model call. The first 4 follow LangChain's context engineering for agents post.
1. Write context outside the window
The system saves what it may need later to files or memory stores instead of keeping it in the window. Anthropic calls this structured note-taking, where "the agent regularly writes notes persisted to memory outside of the context window."
2. Select what this step needs
Before each call, the system pulls in only the relevant pieces: the top-ranked document chunks, the memories that match the current question, and the tools this task can use. Anthropic recommends just-in-time retrieval, where the agent keeps "lightweight identifiers (file paths, stored queries, web links, etc.)" and loads the data when a step needs it.
3. Compress what has grown
Long histories and large tool outputs get summarized, trimmed, or cleared, keeping decisions, constraints, and open questions.
4. Isolate separate jobs
Subtasks run in their own context windows so they don't contaminate each other. A research subagent can read tens of thousands of tokens and return a 1,000- to 2,000-token summary to the main agent, according to Anthropic's guide.
5. Assemble and order the call
The selected pieces go into a predictable structure, with instructions first and the current question last. The model's answer or tool call feeds the next round.

To build this loop yourself, our LangGraph agents tutorial walks through a working agent step by step.
If you'd rather watch, our video lesson covers the same ground with examples:
Key Components of Context Engineering
Each component is stored, selected, and kept fresh in its own way.
System instructions
The system prompt sets the model's role, rules, and output format. It is the most stable part of the context, and good instructions leave out anything a tool or document already supplies.
Memory
Short-term memory is the recent conversation and the state of the current task. Long-term memory holds facts that outlive a session, such as a user's plan or past decisions, recalled when relevant. Our Mem0 tutorial shows one way to build that layer.
Retrieved knowledge
Retrieval brings in documents, database rows, or search results at runtime. The quality of this component depends on chunking, ranking, and metadata filters, because the model can only use the passages that make it into the window.
Tools and tool results
Tool definitions tell the model what actions it can take, and tool results bring back fresh data. Protocols like the Model Context Protocol (MCP) and A2A make it easy to connect many tools, and just as easy to overload the context with them.
What Can You Do With Context Engineering?
These are the most common places context engineering shows up.
RAG systems
RAG was one of the first context engineering techniques: it lets a model answer from documents that were never in its training data, without fine-tuning. Our agentic RAG tutorial shows the next step, where an agent decides what to retrieve.
AI agents
Agents decide which tool to call, read the result, and decide again, so their context changes on every step. An agent can notice it needs current data, call an API, and continue with the fresh result in its window. Our agent harness guide covers the software that manages this loop.
AI coding assistants
Coding assistants such as Claude Code and Cursor combine retrieval and agent loops over a codebase. They need the project's structure, conventions, and recent changes, which is why instruction files like CLAUDE.md and AGENTS.md exist.
How Is Context Engineering Different From Prompt Engineering and RAG?
Context engineering is the broader discipline: prompt engineering shapes the instructions inside the context, and RAG is one technique for filling it.
Context engineering vs prompt engineering
If you ask ChatGPT to "write a professional email," that's prompt engineering: you're writing instructions for a single task. If you build a support bot that remembers previous tickets, reads account details, and keeps a conversation going across sessions, that's context engineering.
| Dimension | Context Engineering | Prompt Engineering |
|---|---|---|
| Scope | Everything the model sees on a call | The instructions and examples |
| Unit of work | A system that runs on every call | A single prompt |
| Typical tools | Retrieval, memory stores, tool selection, summarizers | Wording, examples, output format |
| Best for | Agents, assistants, and apps that run over many turns | One-off tasks and content generation |
Most applications need both. You still write good prompts, but they now sit inside a managed context instead of starting fresh each time.
Context engineering vs RAG
RAG retrieves external knowledge and inserts it into a model request. Context engineering includes RAG and adds memory, tool selection, compression, isolation, and ordering around it.
| Dimension | Context Engineering | RAG |
|---|---|---|
| What it manages | Instructions, memory, tools, retrieved data, and history | Retrieved documents |
| Main question | What should the model see on this call? | Which passages answer this question? |
| Techniques | Selection, compression, isolation, and ordering | Chunking, embedding, and ranking |
| Relationship | The whole discipline | One technique inside it |
Our comparisons of RAG vs CAG and agentic search vs RAG cover the retrieval side in more depth.
How We Use Context Engineering to Keep a Coding Agent on Track
With Claude Code, most of our context engineering happens in the files it loads and how we split work into sessions, as we describe in Writing the Best CLAUDE.md and Claude Code Best Practices.
- We keep a short CLAUDE.md. It holds what the agent can't infer from the code: an overview, a directory map, our commands, and known traps. Anthropic's Claude Code docs recommend under 200 lines per file; we start from about 60. We test each line by asking whether removing it would cause a mistake.
- We leave out what belongs elsewhere. Style rules go to the linter, API docs stay as links, and task-specific instructions become skills or a subdirectory CLAUDE.md, which load only when a task needs them.
- We plan in a file before any code. We ask for a plan.md, annotate what is wrong or vague, and send it back with "address all notes, don't implement yet" until it is clear.
- We clear instead of letting the history grow. When a task is done or the context gets heavy, we write progress to a markdown file, run /clear, and start a fresh session that reads it, so we decide what carries over.
- We send side quests to subagents. Searching a large codebase or reading logs happens in a subagent with its own window, so the main session only gets the summary.
The main window holds the current task and the rules the agent can't forget; everything else stays one file read away.
Challenges and Limitations of Context Engineering
Context engineering exists because long contexts fail in predictable ways. Practitioners name 4 failures, each with a fix in the next section, and our essay Context Engineering Is the New Systems Design for AI maps where they enter an agent's loop, from noisy ingestion to memory writes that poison the store.
Context poisoning
Context poisoning is when a hallucination or error enters the context and keeps getting referenced. Google DeepMind's Gemini 2.5 technical report describes a Pokémon-playing agent whose goals and summary were "poisoned" with misinformation about the game state. The fix is quarantine and validation.
Context distraction
Context distraction is when the context grows so long that the model over-relies on it. The same Gemini agent began "repeating actions from its vast history" once its context passed 100,000 tokens. The fix is summarization that keeps constraints.
Context confusion
Context confusion is when irrelevant content, often too many tools, leads the model to a worse answer. In the RAG-MCP paper, a model shown every tool description picked the right tool 13.62% of the time, and retrieving a shortlist first raised that to 43.13%. The fix is a smaller tool loadout.
Context clash
Context clash is when new information or tools conflict with what is already in the context. In the multi-turn study above, models made early, wrong attempts while a task was still arriving in pieces, and those attempts stayed in the context: "when LLMs take a wrong turn in a conversation, they get lost and do not recover." The fix is pruning and offloading.
Limits of the fixes
Every fix trades something away. Summaries lose detail: an October 2026 study, Benchmarks Are Surprisingly Compaction-Friendly, found that on ProgramBench, the first compaction summary "already drops about one third of the task's anticheating rules on average." Retrieved documents and tool outputs can also carry prompt injection, and context assembled across users can leak data a user shouldn't see.
Best Practices for Context Engineering
These practices answer the failures above. For systems already in production, the anti-patterns in our context engineering essay, from dumping the full transcript on every turn to memory that never forgets, make a useful checklist.
Quarantine and validate before you remember
Run separate subtasks in separate threads, and check facts before they go into long-term memory. If an agent starts acting on something false, start a fresh thread instead of arguing with the poisoned one.
Summarize, but keep the constraints verbatim
Compress old turns into a summary when the context grows, but carry the rules, decisions, and open questions over word for word. The compaction study above found that instructions about how an agent should work are dropped about 6 times more often than descriptions of what to build.
Give each task a small tool loadout
Index your tool descriptions for retrieval, for example in a vector database, and give each task only the tools it needs, as the RAG-MCP paper does.
Prune and offload
Remove tool results and failed attempts that no longer matter, and keep working notes in a scratchpad or file outside the main context, as in step 1 of the loop above.
Treat retrieved text as data, not instructions
Keep system instructions separate from documents, web pages, and tool output, and filter by the user's permissions before anything enters the context. Test with conflicting sources and injected instructions, not only clean questions.
What's Next for Context Engineering?
Context management is moving from application code into model APIs and shared standards, while research shows its blind spots.
- Compaction is becoming an API feature. The Claude API's compaction writes the summary on the server, either when input reaches a token threshold or on request, with both modes in beta and the on-demand mode under a September 2026 beta header. Context editing clears old tool results by rule, and a memory tool keeps files across sessions.
- Context formats are converging. The Linux Foundation's Agentic AI Foundation launched in December 2025 with MCP and AGENTS.md among its founding projects, and Agent Skills joined it in October 2026. Claude Code already reads AGENTS.md files in place of CLAUDE.md.
- Research is measuring what compaction loses. The compaction study above shows that the constraints an agent must follow are the first thing summaries drop, which points to summaries that keep rules apart from progress.
For predictions on where the memory layer is heading, see the last section of our context engineering essay.
Final Thoughts
Context engineering is the practice of deciding what an LLM sees on each call, and it decides whether an AI application feels reliable or forgetful. Bigger context windows haven't removed the need for it: they've made the choice of what to leave out more important.
Start small: write down what your application should always know, retrieve the rest per question, and measure what goes into the window before adding more machinery.
To learn more, we recommend these resources:
- Introduction to AI Agents course
- Retrieval Augmented Generation (RAG) with LangChain course
- Multi-Agent Systems With LangGraph course
- Writing the Best CLAUDE.md
- Best AI Coding Assistants
If you want to build these systems in a structured way, we recommend our Developing AI Applications track.
FAQs
What is context engineering?
Context engineering is the practice of deciding what information a large language model sees on each call, and building the systems that supply it: instructions, memory, retrieved documents, tool definitions, and tool results. It matters most for AI agents and assistants that run over many turns, where what the model sees decides the quality of every answer.
When should I start using context engineering instead of just prompts?
Start when your application has to remember things between turns or sessions, draw on several information sources, call tools, or run long multi-step tasks. A single well-written prompt is enough for one-off tasks like drafting an email. Anything that runs over many turns or large data needs context engineering to stay accurate and affordable.
What's the main difference between context engineering and prompt engineering?
Prompt engineering focuses on the wording of instructions for a single task. Context engineering designs the whole information flow a model sees on every call, including memory, retrieved documents, tools, and history. Prompt engineering is one part of context engineering: well-written prompts still matter, but they sit inside a context the system assembles and manages.
Can I use larger context windows instead of context engineering?
No. Models get less accurate as their input grows, long before the window is full. Chroma's context rot report found performance degrading with input length across all 18 models it tested, and Anthropic's API documentation notes that response quality degrades as a conversation grows. Selecting, compressing, and ordering context still matters at any window size.
Why do AI models perform worse when I give them more tools or information?
Irrelevant content distracts the model, a failure called context confusion: tool descriptions start to overlap, and the model may call the wrong tool. The fix is a small tool loadout. In the RAG-MCP paper, retrieving only the relevant tool descriptions more than tripled tool selection accuracy and cut prompt tokens by more than half.
Is RAG the same as context engineering?
No. Retrieval augmented generation (RAG) is one context engineering technique: it retrieves documents and adds them to a model request. Context engineering is the broader discipline that also covers memory, tool selection, compressing long histories, isolating subtasks in separate agents, and the order in which everything appears in the context window.

Bex Tuychiev builds voice agents and LLM systems, and most of his work goes into making them reliable enough to run in production, preferably in healthcare.





Your chatbot forgets the instructions you gave it 20 turns ago, your coding agent loses track of the project's architecture, and your RAG tool can't connect facts spread across documents. A better prompt fixes none of this, because the problem is what the model sees on each call, not how you phrase the request.
In this article, we'll explain what context engineering is, how it works, how it differs from prompt engineering and RAG, and how we use it to keep a coding agent on track. You can also see our guides to context windows and LLM memory.
What Is Context Engineering?
Context engineering is the practice of deciding what information a large language model sees on each call, and building the systems that gather, select, compress, and order that information. Anthropic's engineers define it as "the set of strategies for curating and maintaining the optimal set of tokens (information) during LLM inference".
Andrej Karpathy drew the line in June 2025: prompting is what you do in a chat, while context engineering is what serious LLM apps do at every step: