explainx / blog
Context engineering is the full-stack discipline of designing everything that enters a model's context window—system prompts, RAG documents, tool definitions, conversation history, and constraints. This guide covers the anatomy of a well-engineered context package, token budget management, agentic context design, common mistakes with before/after examples, and a practical checklist.
Jun 28, 2026
Prompt engineering fixes your wording. Context engineering fixes what the model sees. This guide draws the precise line, shows concrete examples of each in action, and maps out when to reach for which tool.
Jun 28, 2026
RAG is not just a retrieval problem — it's a context engineering problem. What you retrieve, how you inject it, and where it lives in the context window determines whether the model can actually use it. This guide covers the full pipeline from chunking to injection.
Jun 28, 2026
Token cost isn't just a billing concern — it's a context quality signal. This guide covers how to plan token budgets before deployment, allocate them across context components, and monitor per-task cost to find and fix context inefficiencies.
Context engineering is what you are already doing when you care about what the model sees, not only how you phrase a single message. As AI systems move from single-turn chatbots to multi-step agents with tool access, retrieval pipelines, and long conversation histories, what you put in the context window becomes the most important engineering decision you make.
This guide covers the full stack: the anatomy of a context package, how to manage token budgets, how to design context for agentic systems, the most common mistakes teams make (with before/after examples), a copy-ready checklist, and how to measure whether your context engineering is actually working.
Prompt engineering focuses on the wording of a single message—usually the user turn, sometimes the system prompt. You iterate on phrasing, try different framings, and see which produces better outputs.
Context engineering is the full stack. It asks: what is the complete package of information this model needs to complete this task, and how should each component be structured, ordered, and sized? The context window is not just your prompt—it is everything the model conditions on when generating a response.
| Component | What it contains | Who controls it |
|---|---|---|
| System prompt | Role, success criteria, behavioral rules, output format | You |
| User message | Current task or question | User / application |
| Conversation history | Prior turns from this session | Accumulated automatically |
| Retrieved documents | RAG outputs, pasted excerpts, file contents | Your retrieval pipeline |
| Tool definitions | Schemas and descriptions of available tools | Your tool configuration |
| Tool outputs | Results from prior tool calls in the session | Injected by the agent runtime |
| Safety constraints | Policy instructions, content restrictions | You / provider defaults |
Every token in this list costs money and competes for attention. Research on attention mechanisms consistently shows that models weight content at the beginning and end of contexts more heavily than the middle. Burying your most important instructions in the middle of a 50k-token context is context engineering malpractice.
Every token you include shapes what the model prioritizes. Every token you exclude is a decision. Context engineering is the discipline of making those decisions deliberately rather than by default.
Here is the same task handled at two different levels of abstraction.
Prompt engineering approach:
Write a summary of the attached document in 3 bullet points.
Context engineering approach:
[SYSTEM]
You are a research assistant for a biotech company. Your summaries
go into a Slack channel read by non-technical product managers.
Output format: exactly 3 bullets, each under 20 words, no jargon.
Success criteria: a PM who did not read the document can act on
your summary.
[RETRIEVED DOCUMENT - Pages 2-4 only, relevance score: 0.92]
[...targeted excerpt...]
[USER]
Summarize the key findings relevant to our Q3 product decision.
The second version tells the model who it is, who it is writing for, what success looks like, what content to use, and what format to produce. The model does not need to guess at any of those dimensions. The output is more predictable, more consistently useful, and requires fewer repair turns.
The difference compounds in agentic workflows. A vague system prompt in a 40-step agent task does not cause one bad response—it causes 40 suboptimal decisions.
The system prompt is your most durable investment. It sets the frame for everything else. A well-designed system prompt answers:
Weak system prompt:
You are a helpful assistant. Help the user with their questions.
Strong system prompt:
You are a senior backend engineer reviewing pull requests at a
fintech company. Your reviews focus on correctness, security, and
performance—in that order.
Output: markdown with sections ## Summary, ## Critical Issues,
## Suggestions. Critical issues block merge. Suggestions do not.
Constraints:
- Flag any SQL query that could be vulnerable to injection.
- Flag any secret or credential that appears in code or logs.
- Do not suggest stylistic changes unless they affect readability.
If the diff is too large to review in one pass, say so and ask
which files to prioritize.
The second version is longer. It is also dramatically better because the model does not need to infer any of those dimensions from context clues.
One of the most consequential context engineering decisions is whether to paste a document or retrieve targeted excerpts from it.
Paste the full document when:
Retrieve targeted excerpts when:
When you retrieve, include the relevance score and document metadata (title, date, section) alongside the excerpt. This gives the model explicit signals about how much to weight the retrieved content and where it came from.
Poor RAG injection:
[CONTEXT]
[...5000 tokens of full document...]
Better RAG injection:
[SOURCE: Q2 2026 Earnings Report, Section: Revenue Breakdown, Relevance: 0.91]
"Cloud services revenue grew 34% YoY to $2.1B, driven primarily by
enterprise contracts in APAC..."
[SOURCE: Q2 2026 Earnings Report, Section: Guidance, Relevance: 0.87]
"Management expects Q3 revenue of $2.3-2.4B, with gross margins
stable at 68-70%..."
Tool definitions are part of your context budget. In Claude's API, a typical tool definition costs 200–500 tokens depending on description length and parameter complexity. If you have 20 tools defined, you are spending 4,000–10,000 tokens before the conversation starts.
Design tool schemas with context budget in mind:
search_knowledge_base is better than vector_db_cosine_query.Bloated tool definition:
{
"name": "file_system_read_operation",
"description": "This tool can be used to read files from the file system. It supports various file formats including text files, JSON files, YAML files, and others. The tool will return the contents of the file as a string. Note that binary files may not be readable. The path should be absolute.",
"parameters": {
"file_path": {
"type": "string",
"description": "The full absolute path to the file that you want to read from the file system"
}
}
}
Lean tool definition:
{
"name": "read_file",
"description": "Read a file and return its text contents. Use absolute paths.",
"parameters": {
"path": {"type": "string", "description": "Absolute file path"}
}
}
The lean version conveys the same information in ~30% of the tokens.
Conversation history is where context budgets quietly explode. Each turn adds tokens. Long sessions accumulate dozens of turns. By turn 20, you may be spending more tokens on history than on the current task.
What to keep: The current task statement, any constraints the user has specified, any output the user has approved or rejected, the last 2–3 turns for immediate context.
What to summarize: Long explanations, exploratory turns, error-and-retry sequences where the model eventually got it right. Replace these with a one-line summary: "Previously resolved: user needed the output in CSV format, not JSON."
What to drop: Pleasantries, confirmation messages ("Sounds good, thanks!"), tool call outputs that have been superseded by later calls, and any content that is not load-bearing for the current task.
Implement a rolling summarization strategy: when the conversation history exceeds a threshold (e.g., 8k tokens), summarize the oldest N turns into a compact summary and drop the raw turns.
Token budget management is not just cost optimization—it is alignment optimization. A model operating in an overstuffed context window has lower effective attention on any given piece of content. Reducing context size often improves output quality, not just cost.
For any given task, break down your context into components and estimate token counts:
Fixed costs (per session):
- System prompt: ~800 tokens
- Tool definitions (8 tools): ~3,200 tokens
- Safety instructions: ~300 tokens
Total fixed: ~4,300 tokens
Variable costs (per task):
- Retrieved documents: ~2,000–6,000 tokens
- Conversation history: ~500–8,000 tokens
- User message: ~50–500 tokens
- Previous tool outputs: ~0–4,000 tokens
Budget ceiling: 100k tokens (leaving 32k for output)
Available for variable content: ~95,700 tokens
Knowing these numbers lets you make deliberate tradeoffs: if your retrieval pipeline returns 8,000 tokens of documents, you know you need to trim conversation history proportionally.
Unlabeled walls of text. When you paste content without headers or structure, the model has to parse it first. Add minimal labeling: [FILE: config.yaml], [EMAIL: Subject: Q3 budget, From: [email protected], Date: 2026-06-15].
Redundant history. If the model already produced a plan in turn 3 and the user approved it, you do not need to include the full deliberation. Include the final plan.
Irrelevant retrieved content. Set a relevance threshold for RAG. If your similarity score is below 0.75, do not include the excerpt—it adds noise, not signal.
Boilerplate in tool outputs. Many APIs return verbose JSON with metadata you do not need. Pre-process tool outputs to extract only the relevant fields before injecting them into context.
Given attention patterns, order matters:
Agents amplify every context engineering mistake. Here is why: in a single-turn interaction, a poorly assembled context causes one bad response. In a 40-step agent task, a poorly assembled context causes the agent to make 40 slightly-off decisions that compound into significant task failure.
Accumulated history. Each tool call and its result gets added to the context. By step 20, the agent's context is dominated by historical tool outputs, many of which are no longer relevant.
Tool surface explosion. An agent with 20 available tools spends thousands of tokens on definitions and produces unpredictable tool selection. Worse, it may call tools unnecessarily just because they are defined.
Cascading errors. A wrong decision in step 3 creates a wrong context for step 4. The agent can spiral into a local minimum it cannot escape because its context is dominated by the history of the wrong path.
Only define the tools the agent actually needs for the current task. If your agent is doing a code review, it needs read_file and search_codebase. It does not need send_email, create_calendar_event, or update_database_record.
Implement tool gating: dynamically inject tool definitions based on the current task phase. In the "research" phase of an agent workflow, expose read-only tools. In the "write" phase, expose write tools. Never expose all tools at once.
CLAUDE.md is a project-level file that Claude reads at session start to understand project context, conventions, and constraints. It is an explicit context engineering mechanism: instead of relying on the model to infer your codebase's norms from code style alone, you write them down.
Good CLAUDE.md files are:
The same principle applies to any persistent context injection: your SKILL.md files, your cursor rules, your agent configuration files. All of them are context engineering.
Prompt caching (Anthropic, OpenAI) lets you cache the prefix of a context window and pay reduced rates for tokens in the cached prefix on subsequent calls. This changes how you should structure your context:
Put stable content first (cacheable prefix):
Put variable content last (after cache breakpoint):
A well-designed cache boundary can cut inference costs by 50–80% on long-running agent sessions where the system prompt and tool definitions are large.
Before:
You are a helpful AI assistant. Help the user with their requests.
Be professional and accurate.
After:
You are a data analyst for a SaaS company. Users are business
stakeholders who need actionable insights from metrics data.
Output format: for analysis requests, use this structure:
## Finding
## Evidence (with specific numbers)
## Recommended action
## Confidence (High/Medium/Low with reason)
Never fabricate numbers. If you cannot answer from the provided
data, say "Insufficient data" and specify what you would need.
Before:
Here is our entire codebase context: [15,000 tokens of all files]
Now fix the bug in the authentication module.
After:
[FILE: src/auth/middleware.ts - Lines 45-120 (authentication flow)]
[...targeted excerpt...]
[FILE: src/auth/types.ts - Full file (20 lines)]
[...full file because it's small and all relevant...]
Context: The bug report says login fails with valid credentials.
The issue is in the JWT validation step.
Before (20 tool definitions, 8,000 tokens of overhead): Agent task: "Read this PR diff and generate a review."
Available tools: read_file, search_code, send_email, create_issue, update_issue, post_comment, deploy_service, run_tests, query_database, search_docs... [12 more tools]
After (3 tool definitions, ~1,200 tokens of overhead): Agent task: "Read this PR diff and generate a review."
Available tools: read_file, search_code, post_pr_comment
Before: Agent loop with no stopping criteria. Agent runs until it hits the token limit or errors out.
After:
[SYSTEM]
...
Stopping conditions:
- Stop and return results when you have completed all 3 analysis
steps, even if you could do more.
- If you encounter an error on the same step twice, stop and
report BLOCKED: [reason] instead of retrying indefinitely.
- If a required file does not exist, stop and report
MISSING: [filepath] rather than searching for alternatives.
Before: History grows unbounded across a session. By turn 30, the model's context is dominated by old, irrelevant turns.
After: Implement a summarization trigger. When history exceeds 6,000 tokens, summarize the oldest half into a compact summary and drop the raw turns. Include the summary as a labeled section: [CONVERSATION SUMMARY: turns 1-15].
Copy this into your team's engineering templates:
System prompt
Content and retrieval
Tool definitions
Conversation history
Agents
Three metrics signal whether your context engineering is working:
Count how often a task requires follow-up correction. If the model produces a response, the user says "no, I meant X," and you have to prompt again—that is a repair turn. Good context engineering reduces repair turns because the model had enough information to get it right the first time.
Target: under 15% repair turn rate for well-defined tasks. If you are above 30%, your context package is missing critical information.
For agent tasks, measure how often the model calls tools in the expected sequence vs making unexpected calls (wrong tool, wrong arguments, unnecessary calls). Unstable tool calling indicates the model does not have enough context to reason about what to do next.
Target: over 90% expected tool call sequences on your task distribution. If you are below 80%, audit your tool definitions and system prompt.
Do not measure cost per response—measure cost per successfully completed task. A task that costs 5,000 tokens and completes successfully is more efficient than a task that costs 2,000 tokens per attempt but requires 3 attempts.
Baseline: calculate your current cost per task completion. Set a target 20–30% lower. Achieve it by stripping context waste rather than reducing context quality.
The tooling landscape has matured significantly. Key tools worth knowing:
LangChain and LlamaIndex provide context management abstractions: chunking strategies, retrieval pipelines, conversation memory classes, and context compression utilities. Both have matured considerably and are worth using as infrastructure, not fighting against.
Anthropic's prompt caching API (cache_control parameter in the Messages API) lets you mark context prefixes as cacheable. Essential for any production agentic system with large, stable system prompts.
DSPy (Stanford) takes a different approach: instead of hand-engineering prompts, it optimizes prompt structure automatically given a task dataset and metric. Worth evaluating if you have labeled data for your task.
explainx.ai Prompt Generators provide modality-aware context templates for text, image, video, and audio tasks. Useful for bootstrapping structured prompts rather than starting from a blank textarea.
CLAUDE.md and SKILL.md conventions (Anthropic Claude Code ecosystem) are increasingly the standard for persistent context injection in coding agents. The pattern generalizes: any agent that operates across sessions benefits from an explicit project context file.
Read next: What are LLM tokens? · What is MCP? · Agent skills guide · Scalable oversight and RLHF
API behavior, caching parameters, and tooling evolve quickly. Treat this as June 2026 guidance—verify provider documentation before making production infrastructure decisions.