
๐ง What Is Context Engineering for AI Agents?
๐ง Learn what context engineering is, how it differs from prompt engineering, and the 4 core operations (Write, Select, Compress, Isolate) that keep AI agents reliable.
๐ Key Takeaways
- ๐ฏ Context engineering is the discipline of designing everything that enters an LLM's context window on every call: instructions, tools, retrieved knowledge, memory, conversation state, and constraints.
- โ๏ธ Prompt engineering optimizes the instruction. Context engineering manages the full, dynamic information environment across multi-step runs.
- ๐ The context window is finite and degrades with length and noise. Every token competes for attention.
- ๐ In practice it reduces to four operations: Write โ๏ธ | Select ๐ฏ | Compress ๐๏ธ | Isolate ๐งฑ

๐ Introduction
Early LLM work was mostly about finding the right words. Agent work is different. ๐ค An agent generates new data every turn: tool outputs, observations, partial plans. That data must be stored, refined, or discarded before the next call.
The question changes from:
โ "How do I phrase this?"
โ "What configuration of information is most likely to produce the right behavior on this turn?"
That broader practice is context engineering.
โ๏ธ 1. Context Engineering vs Prompt Engineering
| ๐ Dimension | โ๏ธ Prompt Engineering | ๐ง Context Engineering |
|---|---|---|
| ๐ญ Scope | Instructions / one message | Entire context window over time |
| ๐พ State | Mostly stateless | Stateful, evolves across steps |
| ๐ ๏ธ Core skill | Phrasing and examples | Information-flow architecture |
| ๐ฅ Failure mode | Vague or misread instruction | Context rot, distraction, contradictory state |
| ๐ฌ Typical use | Single completion or chat turn | Multi-step, long-running agents |
Prompt engineering is still necessary: clear instructions, good examples, and format constraints matter. But it is a subset of context engineering, not a substitute. Once an agent runs for many turns, failures shift from "the instruction was ambiguous" to "the model saw the wrong, stale, or overwhelming set of tokens."
๐ก Prompt engineering shapes the ask. Context engineering shapes what the model knows before it answers.
๐งฉ 2. Anatomy of the Context Window
A typical agent call assembles:
- ๐ System / developer instructions
- ๐ง Tool definitions and schemas
- ๐ Retrieved knowledge (RAG / hybrid search)
- ๐ง Durable memory (preferences, facts, policies, past episodes)
- ๐ฌ Recent conversation (working memory)
- ๐ฆ Tool results from the current run
- ๐ง Constraints, output format, safety rules
Two realities follow:
- ๐ The window is finite. Every token competes for attention.
- ๐ฏ Position and noise matter. Models show "lost-in-the-middle" effects: a long, low-signal transcript hurts quality even when the right fact is somewhere inside it.
โ ๏ธ More context is not automatically better. High-signal, well-scoped, timely context is better.
๐ค 3. Why Agents Force This Discipline
A single-turn task can survive a well-written prompt. An agent can't, because every loop iteration produces new observations, and each one might matter next turn, next session, or never.
The harness must decide:
- โ What to keep
- ๐พ What to store outside the window
- ๐ What to retrieve later
- ๐๏ธ What to drop
Context assembly is the first step of every loop iteration. Context engineering makes that step deliberate.
๐ ๏ธ 4. The Four Core Operations
โ๏ธ Write: persist outside the window
- ๐๏ธ Use memory stores, scratchpads, logs, and files
- ๐ฏ Information stops competing for attention every turn
- โ ๏ธ Without a write path, the only memory is the current window, and it disappears when the run ends
๐ฏ Select: pull in only the relevant slice
- ๐ Vector or hybrid retrieval, scoped memory search
- ๐งน Filtered tool results, ranking by relevance and recency
- โ ๏ธ Noise and cross-user leakage are selection failures
๐๏ธ Compress: keep the window high-signal
- ๐ Thread summaries, context cards, progressive disclosure of older turns
- โ๏ธ Trimmed tool output
- โ ๏ธ Unbounded appending leads to context rot and rising token cost
๐งฑ Isolate: give subtasks clean boundaries
- ๐ค Sub-agents that return only a summary
- ๐ช Separate windows for research and synthesis
- ๐ Memory scoped per user or tenant
- โ ๏ธ One contaminated window can derail an otherwise correct plan
๐ 5. How Retrieval, Memory, and Compression Work Together
- ๐ Retrieval supplies external knowledge on demand
- ๐ง Memory supplies continuity across sessions
- ๐๏ธ Compression keeps the active window usable
1
2
3
4
5
6
7
8
9
10
11
12
13
User task
โ
โผ
[Select] โ memory store + retriever (scoped by user/tenant)
โ
โผ
[Compress] โ summarize old history, trim tool output
โ
โผ
[Assemble context] โ LLM reasons โ tool call
โ โ
โ โผ
โโโโโโโโโ [Write] โโโโโโโโโ tool result / new facts
๐ 6. Practical Principles
- โ Prefer high-signal tokens over volume
- ๐ Rebuild context every turn instead of blindly appending
- ๐ Scope memory and retrieval (user / tenant / agent) before ranking
- ๐ฆ Treat tool results as first-class context that must be managed, not just appended
- ๐ Measure it: context quality shows up in task success, token cost, and long-run consistency
๐ซ 7. Common Anti-Patterns
- ๐ Append-only history: adding everything from every turn. Context rot and token cost both grow.
- ๐๏ธ Dumping raw tool output: passing a 5,000-token JSON when only 3 fields mattered.
- ๐งฐ Too many tools: large tool lists confuse the model and increase wrong-tool calls.
- ๐ฐ๏ธ Stale memory: outdated preferences or facts create contradictory state.
- ๐ No scoping: one user's memory leaking into another user's context.
๐ป 8. A Minimal Example
1
2
3
4
5
6
def build_context(task, user_id, history):
memory = memory_store.search(task, scope=user_id, k=3) # select
docs = retriever.search(task, k=4) # select
older, recent = history[:-6], history[-6:]
summary = summarize(older) # compress
return [system_prompt, *tool_schemas, *memory, *docs, summary, *recent, task]
๐ 9. How to Evaluate Context Quality
- ๐ Task success vs context size: does more context actually help?
- ๐ฐ Token cost per completed task
- ๐ Long-run consistency: at turn 30, does the agent still follow the instructions from turn 1?
- ๐งช Ablation tests: remove one context component and measure how much the output changes
โ FAQ
๐ค Is prompt engineering dead?
No. It remains essential for instructions, format, and examples. It is a subset of context engineering.
No. It remains essential for instructions, format, and examples. It is a subset of context engineering.
๐ช Context window vs memory?
The context window is what the model sees on the current call (working memory). Durable memory is stored state that can be selected into future windows.
The context window is what the model sees on the current call (working memory). Durable memory is stored state that can be selected into future windows.
๐ Do I still need RAG?
Often yes. RAG is one of the main "select" mechanisms. Context engineering decides when and how to use it.
Often yes. RAG is one of the main "select" mechanisms. Context engineering decides when and how to use it.
๐๏ธ Where do summaries and context cards fit?
They are compression techniques that turn long history or large tool results into a compact, prompt-ready package.
They are compression techniques that turn long history or large tool results into a compact, prompt-ready package.
๐ฅ What is context rot?
The gradual drop in answer quality as the window fills with low-signal, stale, or contradictory tokens.
The gradual drop in answer quality as the window fills with low-signal, stale, or contradictory tokens.
๐ How does this relate to the agent loop and harness?
Context assembly is the first step of each loop iteration. The harness is the code that performs write, select, compress, and isolate around the model call.
Context assembly is the first step of each loop iteration. The harness is the code that performs write, select, compress, and isolate around the model call.
๐ Final Thought
๐ง Better agents don't come from better prompts alone. They come from better information flow. Design what goes into the window, and the model's behavior follows.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article