What is context engineering?
Context engineering is the practice of deciding everything an AI model sees in its context window for each step: instructions, conversation history, long-term memory, retrieved knowledge, and tool definitions and results. The aim is simple to state and hard to do: give the model what it needs, and nothing that distracts it.
A language model only knows what's in front of it at that moment. It has no other view of your project, your preferences or what happened yesterday. So the quality of an AI agent is, to a large degree, the quality of the context it's given at each step.
Context engineering vs prompt engineering: what's the difference?
Prompt engineering is about the wording of the request. Context engineering is about everything around it.
| Prompt engineering | Context engineering | |
|---|---|---|
| Focus | How the instruction is phrased | What information the model receives |
| Scope | One prompt | Every step of a session, and across sessions |
| Main levers | Wording, examples, format | Selection, memory, retrieval, ordering, token budget |
| Matters most for | Single questions | Agents running long, multi-step work |
The two work together. A perfect prompt can't help if the fact the model needs isn't in its context.
What goes into an AI agent's context window?
Usually six kinds of information, all competing for the same limited space.
Instructions
The role, rules and constraints the agent should always follow.
Current task
What the user is asking for right now.
Recent history
The last turns of this conversation: short-term, working memory.
Long-term memory
Decisions, preferences and lessons from earlier sessions that still apply.
Retrieved knowledge
Relevant documents, code or data fetched for this step.
Tools
Tool definitions the agent can call, and the results it gets back.
Don't bigger context windows make this unnecessary?
No. A bigger window raises the limit; it doesn't decide what belongs inside it.
- Cost: every token in the window is paid for on every call.
- Speed: more context means slower responses.
- Accuracy: long, noisy context often makes models worse at finding what matters, a problem often called context rot.
- Continuity: nothing in a bigger window survives to the next session on its own.
A small, well-chosen context can come close to a huge one at a fraction of the cost. On a public long-conversation benchmark, our memory layer reaches 97% of the accuracy of giving the same model the whole conversation, while reading about 82% fewer tokens. And when the history is far too long to fit at all (about 1.27 million tokens per question), retrieval is the only option: it still answers 83% correctly.
What are the core context engineering techniques?
Six moves cover most of it.
- Select. Retrieve only what's relevant to this step, ranked by relevance and by what has actually proved useful.
- Compress. Summarise long history and tool output into the facts that matter, instead of passing them through raw.
- Write. Save decisions, preferences and lessons to memory outside the window, so they survive the session.
- Isolate. Keep separate tasks, projects or sub-agents in separate contexts so they don't bleed into each other.
- Order. Put stable instructions first and the most relevant facts close to the task; models don't weigh every position equally.
- Budget. Give each kind of context a token allowance and enforce it, so nothing quietly crowds out the rest.
What are the most common context failures?
Most agent failures that look like "the model got dumber" are really context failures.
Missing context
The decision or fact the agent needed was never in the window, so it guessed.
Stale context
An old fact that has since changed is still being fed in, and the agent follows it.
Context poisoning
A wrong statement or hallucination gets saved and then repeated in every later step.
Distraction
So much loosely related material that the relevant part gets lost.
Clash
Two sources in the window contradict each other, and the agent picks the wrong one.
Duplication
The same fact repeated many times, wasting budget and skewing attention.
Where does long-term memory fit in context engineering?
Memory is the long-term half of context engineering. It writes what matters outside the window so it survives the session, then brings the relevant part back when a later task needs it.
Without memory, every session's context has to be rebuilt from scratch, usually by the user, by hand. With it, the agent starts each task already knowing the decisions, preferences and fixes that apply. A good memory layer also handles the failure modes above: it updates facts that changed, removes what's wrong, merges duplicates, and keeps what it injects small. For a deeper look, read our guide to AI memory layers and long-term memory for AI agents.
How do you measure context engineering?
By outcomes, not by how clever the setup looks.
Compare against a simple baseline, such as sending the whole history. If your context engineering doesn't beat it on accuracy while reading fewer tokens, it isn't doing its job. On LongMemEval, a public benchmark of 500 questions over long chat histories, our approach answers 94.2% correctly while reading about 75% fewer tokens than the full history.
A context engineering checklist
Ten questions to ask of any AI agent's context.
- Is the task stated clearly, close to where the model will answer?
- Are the instructions stable and short, rather than growing every week?
- Is earlier knowledge available, or does every session start from zero?
- Is retrieval ranked, with only the top few results included?
- Is long output compressed before it's passed back in?
- Are outdated facts replaced, not just added to?
- Are separate projects kept separate, without walling off what's shared?
- Is there a token budget per kind of context?
- Can you see what the model saw when an answer goes wrong?
- Is every change measured against a baseline?
Frequently asked questions
What is context engineering?
The practice of deciding everything an AI model sees in its context window for each step: instructions, history, long-term memory, retrieved knowledge, and tool definitions and results. The goal is to include what the model needs and leave out what distracts it.
What's the difference between context engineering and prompt engineering?
Prompt engineering is about wording the instruction. Context engineering is about everything around it: what history, memory, documents and tool output the model gets, in what order, and within what budget.
What is context rot?
The drop in answer quality that often happens as context grows long and noisy: relevant facts get buried and older or contradictory information competes with the current task.
Doesn't a bigger context window solve this?
No. A larger window raises the limit, but filling it costs more, takes longer and can lower accuracy. A small, well-chosen context comes close at a fraction of the cost, and keeps working once everything no longer fits.
How does memory relate to context engineering?
Memory is its long-term half: it writes what matters outside the window so it survives the session, and brings the relevant part back when a later task needs it.
How do you measure good context engineering?
Measure answer accuracy on realistic tasks or long-memory benchmarks, tokens per answer, latency, and whether the evidence needed for each answer reached the context.
The long-term half of context, handled.
Open Agentic Brain and Memory is an open-source, self-hosted memory layer for AI coding tools. It remembers what matters and brings back only what's relevant, on your machine and under your key.
curl -fsSL https://raw.githubusercontent.com/rohithhn/Open-Agentic-Brain-and-Memory/main/install.sh | bash