What is context engineering? How it differs from prompt engineering

Context engineering is choosing what enters an LLM's context window each step: Anthropic's 29 September 2025 definition, techniques and a tested example.

Context engineering, beyond the prompt: cover for What is context engineering? How it differs from prompt engineering

Context engineering is the practice of deciding what information goes into a language model's context window at each step: instructions, retrieved documents, tool definitions, memory and message history. Anthropic's Applied AI team defined it on 29 September 2025 as curating and maintaining the optimal set of tokens during inference, and called it the natural progression of prompt engineering. Prompt engineering is one part of it.

Last updated: 11 November 2026.

What is context engineering?

Context engineering is the discipline of curating the tokens a large language model sees when it generates a response. Anthropic's engineering article defines context as the set of tokens included when sampling from the model, and context engineering as the set of strategies for curating and maintaining the optimal set of those tokens, including information that arrives from outside the prompt.

Two posts on X in June 2025 helped the term spread, and Simon Willison quoted both on 27 June 2025 as examples of it gaining traction. Tobi Lutke, chief executive of Shopify, wrote on 19 June 2025 (UTC) that he preferred "context engineering" because it describes the core skill better: providing all the context for the task to be plausibly solvable by the model. Andrej Karpathy added his support on 25 June 2025 and called it the delicate art and science of filling the context window with just the right information for the next step.

Karpathy's list of what goes into that window is a useful checklist: task descriptions and explanations, few-shot examples, retrieval-augmented generation (RAG), related data, tools, state and history, and compacting. He added that too little or wrongly formed context leaves the model without what it needs, while too much or irrelevant context can raise cost and lower performance.

How does context engineering differ from prompt engineering?

Prompt engineering is about writing instructions well, while context engineering is about managing everything the model sees across many steps. Anthropic defines prompt engineering as methods for writing and organising model instructions, particularly system prompts, and says it views context engineering as the natural progression of that work as teams build agents that run over many turns.

The table sets the two side by side using the definitions in Anthropic's article of 29 September 2025.

AspectPrompt engineeringContext engineering
ScopeWriting and organising instructions, mainly the system promptCurating all tokens at inference: instructions, tools, MCP, external data, message history
RhythmA discrete task: write a promptIterative: curation happens each time you decide what to pass to the model
Guiding aimInstructions at the right altitude: specific enough to guide behaviour, flexible enough to give the model strong heuristicsThe smallest set of high-signal tokens that makes the desired outcome most likely
Typical failureBrittle hardcoded logic or vague guidanceBloated tool sets and long, noisy context (context rot)

The terms overlap rather than compete. A system prompt is still part of the context, so prompt writing remains a skill inside the larger job.

Simon Willison, who writes a blog on language models, wrote the same week that the new term might have sticking power because people's inferred meaning of "prompt engineering" is typing into a chatbot, while the inferred meaning of context engineering is closer to the intended one. For the prompt side in depth, see what prompt engineering is and how prompting, RAG and fine-tuning compare.

What is a context window, and what fills it?

A context window is all the text a language model can reference when generating a response, including the response itself. Anthropic's Claude documentation describes it as the model's working memory, distinct from the data the model was trained on.

Everything in a request counts toward the window: the system prompt, every message including tool results, images and documents, and the tool definitions. For Claude models the documentation says the window is up to 1M tokens depending on the model, with other models at 200k tokens, and that more context is not automatically better. Window sizes differ by vendor and model and change over time, so a team should read the current figure from the vendor's page.

Google's Gemini API documentation makes a related point: a very large window invites providing all relevant information up front, yet retrieval techniques remain valuable in specific scenarios. A larger window raises the ceiling without removing the need to choose what goes in.

Which techniques does context engineering use?

Context engineering uses a short list of techniques: retrieval, memory, compaction, tool selection and sub-agents. LangChain's post of 2 July 2025 groups them as write, select, compress and isolate, and Anthropic's article of 29 September 2025 names compaction, structured note-taking and sub-agents for long tasks. The table maps the techniques to what each does.

TechniqueWhat it doesDescribed by
Retrieval (RAG or just-in-time)Pulls relevant data into context when needed, either before inference or through tool calls at run timeKarpathy; Anthropic
Memory and note-takingWrites notes outside the window and reads them back laterAnthropic; LangChain (write, select)
CompactionSummarises a conversation nearing the window limit and restarts with the summaryAnthropic; LangChain (compress)
Tool result clearingRemoves old raw tool outputs from the historyAnthropic
Tool selectionKeeps the tool set minimal so choices are unambiguous, or retrieves only the relevant toolsAnthropic; LangChain (select)
Sub-agentsGives a focused agent a clean window and takes back a condensed summaryAnthropic; LangChain (isolate)
Structured outputsConstrains responses to a schema so output is parseableClaude documentation

Anthropic's article adds details worth knowing. Its sub-agents return condensed summaries of often 1,000 to 2,000 tokens, and it describes Claude Code as a hybrid: CLAUDE.md files are loaded into context up front, while glob and grep let the agent retrieve files just in time. The trade-off it names is that runtime exploration is slower than retrieving pre-computed data.

On tools, Anthropic's wording is that if a human engineer cannot say which tool should be used in a situation, an agent cannot be expected to do better. The Model Context Protocol guide covers how tools reach the model, and multi-agent systems explained covers the sub-agent pattern.

Vendors now ship some of these as features. Anthropic's documentation lists server-side compaction and a client-side memory tool that stores files under a /memories directory, and its prompt caching feature lets the API resume from specific prefixes of a prompt to cut processing time and cost. Those pages mark several of the features as beta, so details can change.

How do you assemble context under a token budget?

A token budget caps what goes into one request: reserve room for the instructions and the question, then add memory, the best-scoring retrieved chunks and the newest chat turns until the budget runs out. The sample below is hypothetical, a support assistant for a food-delivery app, and it counts tokens with the tiktoken library, which implements OpenAI's tokeniser.

import tiktoken

enc = tiktoken.get_encoding("o200k_base")

def tokens(text):
    return len(enc.encode(text))

def build_context(system, question, memory, chunks, history, budget):
    used = tokens(system) + tokens(question)
    kept, dropped = [], []
    ranked = [("memory", m) for m in memory]
    ranked += [("chunk", t) for _, t in sorted(chunks, reverse=True)]
    for kind, text in ranked:
        if used + tokens(text) <= budget:
            kept.append((kind, text)); used += tokens(text)
        else:
            dropped.append((kind, text))
    for i, turn in enumerate(reversed(history)):
        if used + tokens(turn) > budget:
            dropped += [("turn", t) for t in history[: len(history) - i]]
            break
        kept.append(("turn", turn)); used += tokens(turn)
    return kept, dropped, used

A second file calls the function with a 100-token budget, a system prompt, a question, two memory notes, three retrieved chunks with relevance scores and four chat turns:

from context_budget import build_context, tokens

system = "You are a support assistant for a food-delivery app. Answer only from the context."
question = "Can I get a refund for a late order?"
memory = ["User prefers short answers.", "User is on the Pune plan."]
chunks = [(0.91, "Refund policy: orders more than 30 minutes late qualify for a full refund to the wallet."),
          (0.74, "Refunds to the original payment method take 5 to 7 working days."),
          (0.22, "The company was founded in a rented flat. " * 6)]
history = ["user: hi", "assistant: Hello, how can I help?",
           "user: my order was late by 45 minutes", "assistant: Sorry about that. Let me check."]

everything = sum(tokens(t) for t in [system, question, *memory, *[c[1] for c in chunks], *history])
kept, dropped, used = build_context(system, question, memory, chunks, history, budget=100)
print("everything:", everything, "tokens | budget: 100 | used:", used)
for kind, text in dropped:
    print("dropped", kind, "|", tokens(text), "tokens |", text[:36])

Output from running both files with Python 3.9 and tiktoken 0.14.0:

everything: 162 tokens | budget: 100 | used: 95
dropped chunk | 55 tokens | The company was founded in a rented
dropped turn | 3 tokens | user: hi
dropped turn | 9 tokens | assistant: Hello, how can I help?

The arithmetic explains the result. The system prompt (18 tokens) and the question (10) take 28. The two memory notes (5 and 7) bring it to 40, and the two relevant chunks (19 and 16) to 75. The 55-token chunk scored 0.22 would push the total to 130, so it is dropped. The two newest turns (10 and 10) reach 95, the next needs 9 and would reach 104, so the two oldest turns go. A real system would summarise those turns instead of discarding them.

Two caveats apply. The count comes from an OpenAI tokeniser, and Anthropic's token counting documentation says counts depend on the model, so a production budget should use the target model's own counter. The relevance scores are given here; in practice they come from a retriever and are only as good as its ranking.

What are the limits and costs of context engineering?

The main limit is context rot: as the number of tokens in the window grows, a model's ability to recall information from it falls. Anthropic describes context as a finite resource with diminishing marginal returns and says this shows up as a gradient rather than a hard cliff. Chroma's technical report of July 2025 found that performance degrades as input length increases even on deliberately simple tasks, often in non-uniform ways.

Drew Breunig, a writer on AI systems, named four ways long context fails, which LangChain's post repeats: poisoning (a hallucination enters the context), distraction (the context overwhelms what the model learned in training), confusion (superfluous content shapes the answer) and clash (parts of the context disagree). Each is a reason to remove content as well as to add it.

Curation has costs of its own. Anthropic warns that overly aggressive compaction can lose subtle details whose importance appears only later, and that retrieving at run time is slower than using pre-computed data. Karpathy notes that excess context can raise cost, and LangChain's post cites Anthropic's report that multi-agent setups can use up to 15 times more tokens than chat. Anthropic also says that for the foreseeable future windows of all sizes are likely to suffer context pollution where the strongest performance is wanted, so waiting for a bigger window is not a plan.

Where does context engineering fit in an AI programme?

The AI Officer Programme (Certificate in AI Officer Programme) is a live online programme by Vishlesan i-Hub Foundation, IIT Patna, delivered with Masai, for working managers and senior professionals, covering generative AI, agentic AI, AI governance and responsible AI without requiring coding. Its listed topics include LLM context and prompting, Retrieval-Augmented Generation, tool use and function calling, and multi-agent orchestration. Readers who want the build path can read the agentic AI roadmap.

Frequently asked questions

What is context engineering?

Context engineering is the practice of curating and maintaining the set of tokens a language model sees during inference, including instructions, retrieved data, tool definitions, memory and message history. Anthropic's engineering article of 29 September 2025 defines it this way and describes it as the natural progression of prompt engineering for agents that run across many turns.

Is context engineering replacing prompt engineering?

Context engineering widens prompt engineering instead of removing it. Anthropic calls it the natural progression of prompt engineering, and a system prompt remains part of the context that gets curated. Tobi Lutke and Andrej Karpathy preferred the newer term in June 2025, and Karpathy noted that people associate prompts with short task descriptions.

What is a context window?

A context window is all the text a language model can reference when generating a response, including the response itself. Anthropic's documentation says the system prompt, every message, tool results, images, documents and tool definitions all count toward it. Window sizes differ by model, up to 1M tokens on some Claude models according to that documentation.

What are the main context engineering techniques?

The main context engineering techniques are retrieval, memory or note-taking, compaction or summarisation, clearing old tool results, keeping the tool set minimal, sub-agents with clean windows and structured outputs. LangChain groups them as write, select, compress and isolate. Anthropic's article names compaction, structured note-taking and sub-agent architectures for tasks that outgrow one window.

What is context rot?

Context rot is the decline in a model's ability to recall information accurately as the number of tokens in its context window grows. Anthropic's engineering article uses the term and describes it as a gradient rather than a cliff. Chroma's July 2025 technical report found performance falling with longer input even on simple tasks.

Do you need to code to do context engineering?

Building context assembly into an application needs code, such as a Python function that counts tokens and trims chat history to a budget. The ideas do not: deciding what an assistant should see, what it should remember and which tools it may use is a design decision that does not itself require code. The AI Officer Programme page states that no coding is required.

Next step

Run the budget sample with your own chunks and chat turns, then change the budget and watch which items drop first. For a structured route into LLM context, retrieval and agents without writing code, see the AI Officer Programme page.

×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.