Engineering Guide

Context Engineering for AI Agents (2026 Field Guide)

Prompt engineering was about the words you write. Context engineering is about everything the model sees when it runs: the system prompt, the tool definitions, retrieved documents, prior turns, and scratchpad state. Anthropic named the discipline directly in its 2025 piece Effective context engineering for AI agents, and for anyone building agents it has become the higher-leverage skill. This guide gives you a working mental model, a token-budget you can compute, a comparison of the main tactics, and a way to run it consistently across many models on one API.

Diagram of an AI agent context window split into system prompt, tools, retrieval, and history

What context engineering is

Context engineering is the discipline of curating what an LLM sees at inference time. Anthropic put a name to it in its 2025 engineering piece Effective context engineering for AI agents, framing the context window as a scarce resource to be managed deliberately rather than stuffed to the limit.

The core idea: a model does not reason over your intentions. It reasons over the exact tokens present in its context. Every design decision that changes those tokens, from which documents you retrieve to how many past turns you keep, is context engineering. For an agent that runs dozens of steps, those decisions compound, and they decide whether the run stays coherent or drifts.

If you want the underlying constraint spelled out, our explainer on the Claude context window covers how token limits and attention interact. This guide sits one level up: given a window, how do you decide what earns a place in it.

Context engineering vs prompt engineering

Prompt engineering optimizes the static instruction you author. Context engineering manages the entire, largely dynamic, set of tokens the model sees during a run. The distinction matters because in an agent most of the context is not written by you at all, it is produced at runtime by tools and retrieval.

DimensionPrompt engineeringContext engineering
ScopeA single authored instructionThe full context window across a run
Authored byYou, by handYou plus tools, retrieval, and history
Time dimensionStatic, set onceDynamic, changes every agent step
Main failure modeAmbiguous wordingWindow bloat and stale context
Primary leverRephrasingSelection, compaction, offloading
RelationshipA subset of the belowThe superset

Prompt engineering never went away, it became one tool inside a larger kit. A crisp system prompt still matters, it just is not sufficient once the agent starts appending tool output on every loop.

Anatomy of an agent context

Before you can budget a context, you need to see its parts. A typical agent context breaks into a handful of buckets, some fixed for the whole run and some that grow with every step. The chart below shows an illustrative split for a mid-run coding agent, so you can see where tokens actually go.

Illustrative token allocation in a mid-run agent context System prompt 3k Tool definitions 5k Retrieved docs 12k Conversation history 16k Working scratchpad 4k Output reserve 8k Blue bars are the growth-prone buckets that context engineering targets first.
Illustrative, not measured: a modeled 48k-token context for a mid-run coding agent. Retrieval and history are the buckets that balloon. Chart: DataLLM Lab

The lesson from the shape: the two growth-prone buckets, retrieval and history, are where a run goes wrong. Fixed costs like the system prompt and tool schemas are easy to reason about because they do not move. The variable buckets need active management, which is what the tactics below address. Tool definitions in particular deserve care, since verbose schemas tax every single call. If you are choosing tools to expose at all, our comparison of MCP vs API is a useful companion.

The core tactics compared

There is no single trick. Production agents combine several tactics, each trading something. Here is how the main ones stack up.

TacticWhat it doesBest whenCost
RetrievalPulls only relevant docs into contextLarge knowledge base, narrow queryRetrieval quality is the ceiling
CompactionSummarizes old turns into a digestLong conversationsLossy, can drop detail you needed
OffloadingStores state in files or memory, not contextLong-horizon tasksExtra tool calls to read it back
Tool trimmingExposes fewer, tighter tool schemasAgents with many toolsLess capability per step
Sub-agentsEach carries its own focused contextDecomposable multi-part tasksOrchestration overhead

A practical decision rule: if your problem is too much irrelevant data, reach for retrieval and tool trimming first; if your problem is a run that goes on too long, reach for compaction, offloading, and sub-agents. Most real agents need one from each column. For agent architecture more broadly, our roundup of the best LLM for AI agents in 2026 pairs model choice with these tactics, and if you drive tool-use through Claude Code, the patterns in Claude Code skills and AGENTS.md show how to keep instructions themselves lean.

Test your context budget on 300+ models

The same context behaves differently on a 200k-window model than a 32k one, and pricing changes the calculus. DataLLM Lab gives you one OpenAI-compatible key for Claude, Qwen, DeepSeek, GLM, Mistral and more, so you can route the same request and compare.

A token budget you can compute

Context engineering becomes concrete the moment you write the budget as arithmetic. Take a model with a 128k window and a task that needs about 8k tokens of output. Your usable input budget is 128k minus an output reserve, minus fixed costs, and what remains is what you can spend on the variable buckets.

Worked example. Reserve 8k for output. Fixed costs: 3k system prompt plus 5k tool definitions equals 8k. That leaves 128k minus 8k minus 8k, which is 112k for variable context. If you cap history at 16k and scratchpad at 4k, you have 92k for retrieval. That is generous, but the lesson runs the other way on a small model: on a 32k window with the same 8k output reserve and 8k fixed costs, only 16k remains for everything variable, so history and retrieval are now in direct competition and something has to give.

The decision rule that falls out: fixed costs plus output reserve plus history should leave headroom for retrieval, or you are on the wrong model for the task. When they do not, either move to a larger window, offload history to external memory, or split the work across sub-agents. Computing this per model, rather than guessing, is the single highest-leverage habit in context engineering.

Running it across many models

The tactics above are model-agnostic, but the numbers are not. Window sizes range from tens of thousands to hundreds of thousands of tokens, and per-token pricing swings the cost of a fat context by an order of magnitude. That means the same context strategy needs re-checking against each model you consider.

This is where a gateway earns its place. Rather than integrate each provider separately, you point one OpenAI-compatible endpoint at Claude Opus 4.8 for a long-context planning step, Qwen3 Coder Next for tool-heavy execution, and DeepSeek V4 Flash or GLM 5.2 for cheap high-volume sub-agent calls, all with one key. If the gateway pattern is new to you, start with what is an LLM gateway. The payoff for context engineering specifically: you can run the same budgeted request across models and measure which one holds your context best, without rewriting a line of integration code.

FAQ

What is context engineering?

Context engineering is the practice of deciding what information enters an LLM context window at each step of an agent run, and in what form. It spans the system prompt, tool definitions, retrieved data, conversation history, and working memory, treating the finite context as a budget to be curated rather than filled to the limit.

How is context engineering different from prompt engineering?

Prompt engineering optimizes the wording of a single instruction you author. Context engineering manages the full set of tokens the model sees during a multi-step run, including dynamic material like tool outputs and retrieved documents you do not write by hand. Prompt engineering is a subset of context engineering.

Why does context engineering matter for agents specifically?

Agents loop: each tool call appends output back into the context, so a long run can bloat the window with stale results, redundant history, and noise. Because attention quality degrades as the window fills, curating what stays in context directly controls both cost and accuracy over a multi-step task.

What are the main context engineering tactics?

The common tactics are retrieval to pull in only relevant documents, compaction or summarization of old turns, offloading state to external memory or files, tight tool design so definitions cost few tokens, and sub-agents that each carry their own focused context. Most production agents combine several.

How do I budget tokens in a context window?

Start from the model context limit, subtract a reserve for the output you need, then allocate the remainder across fixed costs like the system prompt and tool schemas and variable costs like retrieval and history. Track each bucket so you can see which one is crowding out room for reasoning.

Can I apply the same context engineering across different models?

Mostly yes. The tactics are model-agnostic, but window sizes and pricing differ, so the same budget behaves differently on each model. A single OpenAI-compatible gateway lets you route the same request to many models and compare how your context holds up without rewriting integration code.

Written by
Kevin Fan

Founder of DataLLM Lab, the unified LLM gateway. Kevin tests models the boring way — same prompts, real costs, unedited outputs — and writes up what the runs actually show.

One API for every model

One API, every model.

Get a single API key for Claude Opus 4.7, GPT-5.4, and 300+ more — with automatic price comparison and routing to the best model for every request.