Context Engineering
In short
Context engineering is the practice of choosing what an LLM sees on each call (instructions, documents, tool results, history) so it can do the task reliably.
What is context engineering?
A language model only knows what is in its context window at the moment it answers: the system prompt, the conversation so far, and whatever else the application puts in. Context engineering is the work of deciding what goes into that window, in what form and in what order, on every single call.
Prompt engineering is about phrasing a good instruction; context engineering is about everything around it. For an AI agent that means retrieving the right documents, summarizing long histories, passing in tool definitions and their results, keeping notes it can return to, and leaving out what would only distract it. The term spread in 2025 as agents began running long, many-step tasks.
The window is limited and every token costs money and attention, so more context is not always better. Models tend to miss details buried in very long inputs, and stale or contradictory information leads them astray. Good context engineering keeps what the model needs for this step, close to where it will use it, and drops the rest.
Key takeaways
- Context engineering decides what an LLM sees on each call: instructions, data, tool results and history.
- It is broader than prompt engineering, which focuses on the instruction itself.
- Retrieval, summarization and memory are its main tools in AI agents.
- More context is not better context: irrelevant or stale text makes answers worse.
Example
// Fit the most useful pieces into a fixed token budget, most important first
function buildContext(task: string, history: Message[], docs: Doc[], budget: number): Message[] {
const context: Message[] = [{ role: "system", content: INSTRUCTIONS }];
let used = countTokens(INSTRUCTIONS) + countTokens(task);
for (const doc of docs.slice(0, 3)) {
// the three best-matching documents, if they fit
if (used + countTokens(doc.text) > budget) break;
context.push({ role: "user", content: `Source: ${doc.text}` });
used += countTokens(doc.text);
}
// older turns are replaced by a short summary
context.push({ role: "user", content: `Earlier: ${summarize(history)}` });
context.push({ role: "user", content: task });
return context;
}Readers ask
Is context engineering the same as prompt engineering?
No, it is wider. Prompt engineering is about how you word the instruction; context engineering is about all the information the model receives with it, including retrieved documents, tool results, memory and conversation history.
Why not just put everything into a model with a huge context window?
Because cost and quality both suffer. Long inputs are slower and more expensive, and models tend to overlook details buried in the middle of them, so a short, relevant context usually gives better answers than a long, complete one.
See also
- Prompt EngineeringAI & Machine Learning, p. 36Prompt engineering is the practice of designing, testing, and refining the instructions given to an AI model so it produces accurate, consistent, useful output.
- Context WindowAI & Machine Learning, p. 12A context window is the maximum amount of text, measured in tokens, that an LLM can consider at once, including the prompt, conversation history, and its reply.
- RAGAI & Machine Learning, p. 38RAG is a technique that makes an LLM answer using relevant documents retrieved at question time, so its responses are grounded in current, specific data.
- AI AgentAI & Machine Learning, p. 2An AI agent is a system that uses an LLM to plan and carry out multi-step tasks by deciding which tools to call, observing the results, and acting again.
- System PromptAI & Machine Learning, p. 44A system prompt is the instructions an app gives a language model before the conversation starts, setting its role, rules, tone and what it should know.
- Tool CallingAI & Machine Learning, p. 47Tool calling is an LLM feature in which the model asks the application to run a specific function with structured arguments, then uses the result in its answer.
- TokenAI & Machine Learning, p. 46A token is the basic unit of text that an LLM reads and generates, usually a whole word, part of a word, or a punctuation mark, mapped to a numeric ID.
- Model Context ProtocolAI & Machine Learning, p. 29The Model Context Protocol is an open standard that defines how AI applications connect to external tools, data sources, and prompts through a shared interface.
Spotted a mistake or something missing on this page?Suggest an edit