Context engineering is the work of deciding exactly what an AI agent sees at each step: its instructions, the documents it retrieves, tool results, memory and the conversation so far. When agents fail in production, the model is rarely the cause. The agent usually had the wrong information, stale information or far too much of it, and fixing that is integration work across your CRM, warehouse, docs and permission systems.
What context engineering means
Prompt engineering was about writing one good instruction. Agents run for many turns, call tools and pull in data, so the question has changed to what should be in the model's window right now. Anthropic's engineering team defines context engineering as "the set of strategies for curating and maintaining the optimal set of tokens (information) during LLM inference" (Anthropic).
For a production agent, the context window at any step usually holds five things:
- Instructions: the system prompt, policies and tool descriptions.
- Retrieved documents: passages from your wiki, contracts, tickets or code.
- Tool results: API responses, SQL output, search results, file contents.
- Memory: facts saved from earlier sessions, such as a customer's preferences or a project's decisions.
- Conversation state: the user's messages and the agent's own earlier steps.
Each of these is a design choice with a cost. Your engineers decide what goes in, in what form, how fresh it is and who is allowed to see it.
Next question: How big is a context window, and what fills it?
Why more context makes agents worse and more expensive
The tempting fix for a confused agent is to give it everything. Models now accept very long inputs, so teams paste in whole knowledge bases and full chat histories. The research says this backfires.
The "Lost in the Middle" paper by Liu and colleagues, published in TACL, found that model performance "is often highest when relevant information occurs at the beginning or end of the input context" and "significantly degrades" when the answer sits in the middle (Liu et al.). A long, stuffed window buries the one fact that matters.
Chroma's 2025 "Context Rot" study tested 18 models and reported that "model performance consistently degrades with increasing input length." It also found that "even a single distractor reduces performance," and that the damage from distractors grew as inputs got longer (Chroma). In an enterprise, distractors are everywhere: last year's pricing sheet, a superseded policy, a near-duplicate ticket.
Anthropic describes the same effect as an "attention budget" that the model draws down as context grows. Its advice is to find the smallest set of high-signal tokens that gets the job done (Anthropic).
Cost follows the same curve. Every input token is billed on every step, and an agent that runs 20 steps resends its context 20 times. Prompt caching helps with the stable parts: Anthropic bills cache reads at a fraction of the base input price, but the default cache lives for only 5 minutes, and anything that changes each step is not cached (Claude Docs). Trimming context is usually the cheapest performance fix you have.
Next question: How much will our agent's tokens cost each month?
Context sources, failure modes and fixes
Most context failures in production map to one row of this table. Use it in design reviews and postmortems.
Next question: What does a production retrieval pipeline involve?
Seven patterns that hold up in production
1. Retrieval over stuffing
Do not load the knowledge base up front. Anthropic describes "just-in-time" retrieval, where the agent keeps lightweight identifiers such as file paths, stored queries and links, then loads data at runtime using tools (Anthropic). A well-built retrieval pipeline returns the three passages that matter, not thirty.
2. Compact the history
Long sessions need compaction: summarizing the window and starting fresh with the summary. Decide what the summary must keep (open tasks, decisions, constraints) and test it, because a bad summary silently drops the one instruction the user cared about.
3. Structured notes and memory files
For work that spans hours or days, have the agent write notes outside the window and read them back when needed. Anthropic calls this structured note-taking. Treat agent memory like any other data store: a schema, an owner, timestamps and a way to correct or delete entries.
4. Trim tool results
Design tools to return what the agent needs to decide, not everything the API can produce. Anthropic calls tool result clearing one of the safest, lightest forms of compaction. A CRM lookup should return the account, owner, stage and last activity, not the full object with 200 fields.
5. Sub-agents with clean context
When a task needs a lot of reading, hand it to a sub-agent with its own window. In Anthropic's description, a sub-agent may use tens of thousands of tokens but returns a condensed summary, often 1,000 to 2,000 tokens. The parent stays focused. Use this sparingly; every extra agent adds cost and coordination.
6. Put the important things where models look
Given the "Lost in the Middle" results, place the task and the most relevant evidence at the start or end of the window, not between pages of background.
7. Cache what is stable
Keep instructions and tool definitions identical across steps and put them first, so they can be cached. Put changing data after them.
Next question: How should agent memory be stored and scoped?
Permission-aware retrieval and the source of truth
This is where enterprise context engineering differs from a demo. Your agent pulls from Salesforce, Snowflake, Confluence, SharePoint and ticketing systems, and each has its own access rules. If the retrieval layer runs with a service account that can read everything, the agent will eventually show a sales rep a board deck or an HR file.
The fix is retrieval that respects the requesting user's permissions at query time, either by calling source systems as that user or by syncing access control lists into the index and filtering on them. It is unglamorous work: identity mapping, group sync, handling deleted records and testing edge cases like a colleague whose access ended yesterday.
Freshness is the second half. For every fact an agent might use, name the system of record and how quickly changes reach the index. A pricing agent that reads a cached PDF while finance has updated the warehouse table will be confidently wrong. Index the source of truth, store when each item was last updated, and drop superseded versions rather than letting retrieval rank them.
Next question: Who builds permission-aware retrieval over our systems?
Evals for context quality
Most agent evals grade the final answer. Context engineering needs evals one layer down, so you can tell whether a bad answer came from bad context or bad reasoning. Useful checks:
- Retrieval recall: for a set of real questions, did the right document make it into the window?
- Distractor rate: how many retrieved passages were stale, superseded or irrelevant?
- Permission tests: do users with different roles get different, correct context for the same question?
- Compaction tests: after a summary, does the agent still honor constraints set early in the session?
- Token budget: average and worst-case input tokens per step, tracked per release.
Build the eval set from production traces, not invented prompts, and rerun it whenever you change a retriever, chunking strategy, tool output format or model.
Next question: Who builds the eval suite for context quality?
A context engineering checklist
Use this before an agent goes to production, and again in Q1 planning for agents already live:
- Every context source is listed with its owner and system of record.
- Instructions fit on one screen and contain no contradictions.
- Retrieval returns a small, ranked set of passages with dates and sources attached.
- Superseded and duplicate documents are filtered out of the index.
- Tools return compact, decision-ready output; old tool results are cleared.
- Conversation history is compacted at a defined threshold, and the summary is tested.
- Memory has a schema, timestamps, an expiry rule and a way to correct entries.
- Retrieval runs with the end user's permissions, and access changes propagate.
- Sub-agents, if used, return short structured summaries.
- Stable content is placed first and cached; per-step token use is tracked.
- Evals cover retrieval recall, distractors, permissions and compaction.
Next question: What does it take to build agents on our own data?
How Ryz can help
Context engineering is integration work: connecting agents to your CRM, warehouse, documents and identity systems, then measuring what reaches the model. Ryz AI pod teams are senior engineers who build these systems inside your cloud and repos, alongside your team, and our pods have shipped production agents such as a driver-support agent covering about 218,000 calls a year in three languages. If you need one specialist instead, we can add senior RAG engineers to your team, with senior engineers at $10,000 to $15,000 per engineer per month.
FAQ
What is the difference between context engineering and prompt engineering?
Prompt engineering focuses on writing the instruction. Context engineering covers everything the model sees at each step, including retrieved documents, tool results, memory and history, and it matters most for agents that run over many turns.
Does a bigger context window remove the need for context engineering?
No. Research such as Chroma's context rot study and the "Lost in the Middle" paper shows quality drops as inputs grow and as distractors are added. A larger window gives you room, not accuracy, and every extra token is billed on every step.
Is context engineering just RAG?
Retrieval is one part. Context engineering also covers instructions, tool output design, memory, history compaction, sub-agents, permissions and evals. Many failing agents have decent retrieval and poor handling of everything else.
Who should own context engineering on an engineering team?
Usually the team that owns the agent, with help from data engineering for pipelines and freshness and from security for permissions. It works best when one senior engineer is accountable for what reaches the model and how it is measured.

