Key takeaways
- Context engineering is deciding what information an AI model sees at each step, including instructions, documents, tool results and memory.
- Prompt engineering is one part of it. For agents that take many steps, what you feed the model matters more than clever wording.
- More context is not always better. Irrelevant or conflicting information makes answers worse and costs more.
- Keep context lean, current and well-structured, and test changes against real questions.
- Most agent failures that look like model mistakes are actually context mistakes.
From prompts to context
A prompt is the instruction you give a model. An agent, though, works through many steps: it reads a request, looks up information, calls tools, reads the results and decides what to do next. At each step the model sees a different bundle of text. Context engineering is the work of designing that bundle so the model has what it needs and nothing that gets in the way.
The term has become popular as teams move from simple chatbots to multi-step agents. Most commentators treat it as a broader discipline that includes prompt writing rather than a replacement for it.
The five layers of context
- Instructions. The agent's role, rules, tone and limits. Keep them clear and short.
- Retrieved knowledge. Documents and records fetched for this specific question, as in retrieval-augmented generation.
- Tool results. Live data returned from systems, such as an order status or a calendar.
- Memory. Facts worth keeping between steps or sessions, such as a customer's preferences or a decision made earlier.
- The conversation. The current request and recent turns.
Problems usually come from one of these layers being missing, stale, bloated or contradicting another.
Why more context can make things worse
It is tempting to paste in everything just in case. In practice, too much context causes trouble: the model can overlook important details buried in a long input, conflicting instructions confuse it, and every extra token adds cost and delay. A smaller set of relevant, well-ordered information usually beats a large dump. See our guide to reducing LLM API costs for the cost side of this.
Practical techniques
- Retrieve selectively. Fetch only the few passages that answer the question, ranked by relevance.
- Summarise history. Replace long conversation logs with a short summary plus the most recent turns.
- Structure the input. Label sections clearly, such as policy, customer record and question, so the model knows what each part is.
- Return compact tool results. Send back the fields the agent needs rather than whole database rows or web pages.
- Write down what matters. Store key decisions in memory instead of hoping they stay in a long thread.
- Separate untrusted content. Mark text from emails and websites as data, not instructions, to reduce the risk of prompt injection described in our guardrails guide.
Debugging an agent through its context
When an agent gives a wrong answer, look at exactly what it saw. Was the right document retrieved? Was a stale value in memory? Did a tool return something unexpected? Logging the full context of each step turns vague complaints, such as "the AI got it wrong", into specific, fixable causes. Then add the failing case to your evaluation set so it stays fixed.
Further reading
Several engineering teams have written about this shift, including Sourcegraph's explanation of context engineering and Shelf's guide to governed knowledge for agents. Many of these sources are vendors, so treat their statistics with care. If you would like help designing the context behind your own agent, our AI agent development team can review it with you.