Open Agentic Brain and Memory

Home / Blog / Guide

Guide · 6 October 2026 · 8 min read

Context engineering, explained: how to give AI agents the right context

Prompt engineering is what you ask. Context engineering is everything else the model sees when it answers. For AI agents that run for many steps, it's the difference between an assistant that stays sharp and one that drifts, forgets and burns tokens.

Jump to the checklist
One step's context window example budget
  • Instructions
  • Long-term memory
  • Retrieved knowledge
  • Tool definitions and results
  • Recent history
  • Current task

Everything the model knows for this step has to fit here. Context engineering decides what earns a place.

In short

What it is
Deciding everything an AI model sees for each step, not just how the prompt is worded.
Why it matters
Too little context and the agent guesses. Too much and it drifts, slows down and costs more.
Core moves
Select, compress, write to memory, isolate, order, and budget tokens.
Where memory fits
Memory is the long-term half: what survives the session and comes back when needed.

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 engineeringContext engineering
FocusHow the instruction is phrasedWhat information the model receives
ScopeOne promptEvery step of a session, and across sessions
Main leversWording, examples, formatSelection, memory, retrieval, ordering, token budget
Matters most forSingle questionsAgents 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.

  1. Select. Retrieve only what's relevant to this step, ranked by relevance and by what has actually proved useful.
  2. Compress. Summarise long history and tool output into the facts that matter, instead of passing them through raw.
  3. Write. Save decisions, preferences and lessons to memory outside the window, so they survive the session.
  4. Isolate. Keep separate tasks, projects or sub-agents in separate contexts so they don't bleed into each other.
  5. Order. Put stable instructions first and the most relevant facts close to the task; models don't weigh every position equally.
  6. 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.

Accuracycorrect answers on realistic tasks or long-memory benchmarks
Tokenshow much the model reads per answer
Latencyhow long retrieval and assembly add
Evidencedid the needed fact reach the window at all?

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.

  1. Is the task stated clearly, close to where the model will answer?
  2. Are the instructions stable and short, rather than growing every week?
  3. Is earlier knowledge available, or does every session start from zero?
  4. Is retrieval ranked, with only the top few results included?
  5. Is long output compressed before it's passed back in?
  6. Are outdated facts replaced, not just added to?
  7. Are separate projects kept separate, without walling off what's shared?
  8. Is there a token budget per kind of context?
  9. Can you see what the model saw when an answer goes wrong?
  10. 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.

Open source · Apache-2.0

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