Fundamentals
Context Engineering: The Discipline Behind Reliable Agents
Most agent failures aren't model failures. They're context failures. Here's what context engineering is and how to get it right.
Updated · 5 min read

For a couple of years, "prompt engineering" was the catch-all term for getting good output from a language model. It still matters, but it solves a narrower problem than most people building agents actually have. Context engineering is the broader discipline: not just how you phrase one instruction, but what information, memory, retrieved documents, tool outputs, conversation history, the model has access to at the moment it responds.
The distinction isn't academic. When an AI agent goes off the rails in production, tracing the failure back to a badly worded prompt is rare. Far more often the model simply didn't have the right information in front of it: a stale fact, a missing tool result, or a context window so cluttered with irrelevant history that the one fact that mattered got buried. That's a context engineering failure, and it needs a different fix than rewriting a prompt.
Context engineering vs prompt engineering
Prompt engineering asks: how should I phrase this instruction? It's concerned with wording, examples, and format, applied to a single, mostly self-contained request. It's still useful, especially for one-off tasks, but it assumes the model already has what it needs to answer and just needs to be asked well.
Context engineering asks a different question: what does the model need access to right now? That covers memory retrieved from prior sessions, results from tool calls, relevant documents, and the instructions themselves. Prompt engineering is one input to context engineering, not a replacement for it. An agent with a perfectly worded prompt and no relevant memory will still fail; an agent with a mediocre prompt and the right facts in front of it usually won't.
Why context engineering matters more for agents than chatbots
A single-turn chatbot interaction is naturally bounded: one question, one answer, done. Production agents break that assumption. They run multi-step loops, call tools, accumulate conversation history, and often need facts from sessions that happened days or weeks earlier. Every one of those steps adds tokens to the context, and none of them are curated by default.
Left unmanaged, that accumulation causes two distinct failure modes. First, the context window eventually fills up and the model starts losing access to early information. Second, and more insidious, research on long-context models has repeatedly found that models attend unevenly across a long context, a pattern documented in the "Lost in the Middle" study, so even information that technically fits can get effectively ignored. Careful context engineering exists specifically to prevent both.
What good context engineering looks like
In practice, context engineering is a set of concrete decisions repeated on every request: what to retrieve from memory, how much of the conversation history to keep verbatim versus summarize, which tool results are worth keeping around, and in what order to present everything to the model. None of this is one-size-fits-all; the right answer depends on the task.
A well-engineered context is also compact, not just correct. Every irrelevant token in the window is a token competing for the model's attention and a token you're paying to process. Teams that get this right tend to treat context as a budget: retrieve the smallest set of facts that answers the current question, rank them by relevance, and drop the rest rather than hoping the model will ignore noise on its own.
Context engineering depends on good memory
You can't engineer context you don't have access to. If an agent's only source of information is the current conversation, there's nothing to curate beyond that one thread. Context engineering only becomes a real discipline once there's a durable memory layer behind it, AI agent memory that persists facts across sessions, so there's something worth selecting from.
That's also why the two problems get solved together in practice. A memory system that returns everything it has on a topic just shifts the clutter problem downstream; a memory system built for context engineering ranks and filters at retrieval time, so what reaches the model is already close to what it needs, not a dump that still requires trimming.
Where the Model Context Protocol fits in
Standardizing how a client discovers and calls tools and memory has a direct effect on the practice, even though the Model Context Protocol doesn't itself decide what's relevant. Before a common protocol, every integration was bespoke, which meant every team solved discovery and formatting separately, on top of whatever curation logic they'd built. A shared protocol removes that layer of accidental variation, so the actual discipline, deciding what's relevant, is what teams spend their effort on instead of plumbing.
This separation matters practically: the protocol defines how a client learns what a server can offer, but a server still has to decide what to return when it's asked. A well-built memory server does that filtering itself, ranking and trimming before results ever leave the server, rather than handing back everything and leaving the client to sort it out. That's the practice living inside the infrastructure, not bolted on afterward.
How to tell if it's actually working
Because the discipline is invisible when done well, it helps to have a concrete signal for whether it's working. A useful proxy is the ratio of tokens included in a request to tokens the model actually needed to answer correctly; a low ratio (little padding, high relevance) tends to correlate with fewer hallucinations and lower cost per request, while a high ratio is a sign that retrieval or curation logic is dumping too much in and letting the model sort it out.
Teams that track this over time typically find the gains compound: tightening what reaches the model on the highest-traffic request types pays off disproportionately, since even a small reduction in irrelevant tokens per request adds up fast at scale. It's a discipline best treated as ongoing tuning, revisited as usage patterns shift, not a one-time setup step you configure once and leave alone.
How stored supports context engineering
stored's hybrid retrieval is built for exactly this: a single query spans graph relationships, vector similarity, and full-text search, and returns one ranked, compact set of results rather than everything that loosely matches. That's the retrieval half of context engineering handled for you, so your prompt only has to work with what's actually relevant.
Because every memory carries provenance, source agent, timestamp, and its links to other entities, you can also see exactly what context an agent had access to when it produced a given answer. That visibility turns context engineering from a guess into something you can inspect and correct, closing the loop between a bad output and the context that caused it.
Frequently asked questions
Is context engineering just a rebrand of prompt engineering?
No. Prompt engineering focuses on how a single instruction is worded. Context engineering is broader: it covers everything in the model's context window besides the prompt, including memory, retrieved documents, and tool outputs.
Why do most AI agent failures come from context, not the model?
Agents run multi-step loops that accumulate tool outputs and conversation history fast. Most failures trace back to the model missing a relevant fact or being distracted by irrelevant context, not the underlying model being incapable of the task.
Does a bigger context window remove the need for context engineering?
No. Larger windows raise the ceiling on how much you can include, but research shows models attend unevenly across long contexts, so dumping in everything still degrades quality and costs more per request than retrieving only what's relevant.
How do I start practicing context engineering on an existing agent?
Start by auditing what actually reaches the model on a typical request: memory, tool results, and history. Most teams find they're including far more than the task needs, and trimming to what's relevant is the fastest improvement available.
Related reading

AI Agent Memory: What It Is and How to Build It
Context windows reset. AI agent memory doesn't. Here's what agent memory actually is, why it matters, and how teams build it in production.

Context Window: What It Is and Why It Isn't Enough
A million-token context window sounds like enough. Here's why it still isn't a substitute for real AI agent memory.

Context Compression: Fitting More Into a Finite Window
A January 2026 paper cut token usage 22.7% with no accuracy loss. Here's how context compression actually works.
See what your agents remember.
Connect an agent and stored starts building the live memory graph this post describes, free to try.
No credit card required · Free plan available · Bring your own OpenAI key