What is context engineering?
Context engineering is designing which information a model receives at each step, in what order and how much: instructions, retrieved documents, tool results, conversation history and memory.
It differs from prompt engineering in scope: prompt engineering is about writing a single prompt, while context engineering manages selection, ordering, compression and retention of information across all steps. Order and source reliability matter as much as length.
- Topic
- Prompts, context, reasoning and generation
- Also known as
- Context management, context design
How context engineering works
- Before each request, context pieces are chosen: system instructions, passages retrieved through Retrieval-Augmented Generation, tool outputs and relevant history.
- Content that does not fit the context window is summarised, compressed or left out (context compression).
- Critical information goes at the start or end of the context to reduce the lost-in-the-middle effect.
- In multi-step agent loops the context is updated after each step; rules decide what is kept in long-term memory and what is dropped.
In more detail
Four core operations: write, select, compress and isolate
In current agent architectures, context engineering is commonly organised into four core operations: writing (persisting intermediate notes and plans outside the context window in scratchpads or memory), selecting (pulling only relevant memories, RAG chunks and tool definitions just-in-time), compressing (summarising or trimming verbose tool outputs) and isolating (running sub-tasks in clean sub-agent contexts so intermediate noise does not pollute the main agent).
Context rot and agent failure modes
Poorly engineered agent loops suffer from distinct failure modes: context poisoning (an early hallucination or injected instruction persists in history and skews later steps), context distraction (irrelevant history causes the model to repeat old patterns instead of reasoning anew) and context clash (contradictory retrieved snippets or tool outputs confuse decisions). Mitigating these requires explicit state pruning, versioning and conflict resolution rules.
Tool schema bloat, MCP and just-in-time loading
In tool-using agents and Model Context Protocol (MCP) integrations, dozens of verbose tool schemas and raw API payloads can silently consume tens of thousands of tokens before the user even asks a question. Effective context engineering applies progressive disclosure, loading detailed tool schemas or file contents on demand via lightweight handles rather than injecting every asset upfront.
Commonly confused terms
| Term | How it differs |
|---|---|
| Retrieval-augmented generation (RAG) | RAG is one specific retrieval mechanism for fetching relevant external documents, whereas context engineering is the broader discipline coordinating RAG outputs together with system instructions, tool schemas, conversation history, scratchpads and agent memory. |
| Fine-tuning | Fine-tuning updates a model's weights offline to instil persistent skills or style, whereas context engineering optimises what runtime information is placed into the model's working window at inference time without changing weights. |
Why context engineering matters for on-premises AI
Because the context window is limited, agents and RAG systems succeed largely by giving the right information in the right order. Unneeded content raises GPU memory use and latency, which is a direct cost on in-house hardware. Only documents the user is permitted to see should enter the context, so selection is designed together with permission checks. Tool results and outside documents can carry prompt injection, so untrusted content is marked separately.
A public body's in-house assistant answers citizen applications by selecting the relevant regulations, earlier correspondence and knowledge base entries in priority order before passing them to the model.
Common questions
How does context engineering differ from prompt engineering?
Prompt engineering focuses on how to write one prompt. Context engineering designs, at system level, what information the model gets at each step, in what order and how much.
Do larger context windows make it unnecessary?
No. Models can still miss information in the middle of a large window and every extra token costs; selection and ordering still decide results.
Related terms
- Context window
- Prompt engineering
- Context compression
- Lost in the middle
- Retrieval-Augmented Generation
- Context
- Context extension
- Agent memory
- Conversation history
Sources
- Mei et al., A Survey of Context Engineering for Large Language Models, 2025
- Liu et al., Lost in the Middle: How Language Models Use Long Contexts, 2023
In the glossary: Glossary, A to C