We've come a long way in this series.
In Part 1, we introduced Context Engineering and why it goes beyond writing better prompts.
In Part 2, we explored tokens, context windows and memory.
In Part 3, we looked at how AI applications get information through RAG, Tool Calling and MCP.
And in Part 4, we explored how AI agents use context and tools to execute multi-step tasks.
Now there's a problem.
A problem that becomes increasingly important as your AI application becomes more capable.
Imagine an agent working on a task for 30 minutes.
During that time, it:
- Reads 20 files.
- Calls 15 tools.
- Retrieves dozens of documents.
- Receives thousands of lines of tool output.
- Makes several decisions.
- Encounters and resolves errors.
- Maintains conversation history.
Eventually, you could end up with something like:
System instructions
+
Conversation history
+
Tool definitions
+
Retrieved documents
+
Tool results
+
Agent state
+
Memory
+
Current request
β
LLM
You might think:
"Great. The model has everything it needs."
But there's a catch.
Having everything doesn't necessarily mean having the right context.
And that's what Part 5 is about.
π― The Real Goal of Context Engineering
The goal isn't:
β Put as much information as possible into the context window.
It's:
β Build the smallest useful set of information required for the model to make the current decision correctly.
Think of context as a limited working budget.
You have a task.
You have a certain amount of useful information.
Your job is to decide:
What should stay?
What should go?
What should be summarized?
What should be retrieved later?
What should be cached?
What should never enter the context?
That's Context Engineering.
π¨ More Context Can Become a Problem
Consider this request:
"Why is the payment service returning 500?"
Suppose your system retrieves:
β Payment service logs
β Recent deployment
β Database errors
β Relevant code
β Configuration
β Recent incidents
β Unrelated authentication logs
β Old deployment logs
β 500 pages of documentation
β 20 previous conversations
Technically, the model has more information.
But did we improve the context?
Not necessarily.
We introduced:
- More tokens
- More latency
- More cost
- More irrelevant information
- More opportunities for conflicting information
The useful information can become harder to distinguish from everything else.
This is why:
Context quality matters more than context quantity.
Recent production guidance and research on context management similarly emphasize that long agent trajectories can accumulate irrelevant history and tool output, making selection and compression important engineering problems.
π§© Five Ways to Optimize Context
A practical context pipeline usually has several levers:
Information
β
βΌ
βββββββββββββ
β Select β
βββββββ¬ββββββ
β
βΌ
βββββββββββββ
β Retrieve β
βββββββ¬ββββββ
β
βΌ
βββββββββββββ
β Compress β
βββββββ¬ββββββ
β
βΌ
βββββββββββββ
β Cache β
βββββββ¬ββββββ
β
βΌ
βββββββββββββ
β LLM β
βββββββββββββ
Let's understand each one.
1οΈβ£ Selection β Don't Send What You Don't Need
The cheapest token is often the token you never send.
Suppose an AI coding assistant is debugging:
payment-service
Does it need the entire repository?
Probably not.
It might only need:
payment/
βββ handler.go
βββ service.go
βββ payment.go
βββ payment_test.go
Instead of:
Entire Repository
β
LLM
Use:
User Question
β
Identify Relevant Files
β
Only Relevant Files
β
LLM
This is context selection.
And it can happen before compression.
Why compress information that shouldn't have been included in the first place?
π‘ A Simple Rule
Before adding something to context, ask:
"Does the model need this information to make the current decision?"
If the answer is no, don't send it.
This sounds obvious.
But it's one of the easiest things to get wrong in real systems.
2οΈβ£ Retrieval β Fetch Information Just in Time
Part 3 introduced RAG.
Now we can look at RAG from a different perspective.
Instead of loading everything upfront:
User Request
β
Load 1,000 Documents
β
LLM
retrieve information when it is actually relevant:
User Request
β
Understand Need
β
Retrieve Relevant Information
β
LLM
This is especially useful for large knowledge bases.
For example:
"What's our refund policy for enterprise customers?"
There is no reason to load:
- HR policies
- Engineering documentation
- Marketing documentation
- Old product specifications
The retrieval layer should narrow the information down.
π Retrieval Is a Trade-Off
There is no universal rule that says:
"Always use RAG."
If you have a short document that fits comfortably into the context window and is directly relevant, retrieving chunks may add unnecessary complexity.
For example:
Document: 2 pages
User: "Summarize this document."
Just provide the document.
But if you have:
10 million documents
you obviously cannot send them all.
Retrieval becomes valuable.
The engineering question is:
What information should be available now and what can be fetched later?
3οΈβ£ Compression β Keep the Meaning, Remove the Bulk
Now let's say we really do need the information.
But the information is huge.
Consider a tool response:
GET /orders/12345
{
"id": 12345,
"customer": {...},
"shipping": {...},
"billing": {...},
"items": [...],
"auditLogs": [...],
"metadata": {...},
"internalDebugInformation": {...}
}
Maybe the agent only needs:
Order ID: 12345
Status: FAILED
Payment Status: DECLINED
Failure Reason: Insufficient funds
Why send the entire response?
We can transform:
Large Tool Output
β
Relevant Information
β
Compact Context
This is context compression.
ποΈ Compression Is Not the Same as Deletion
This distinction matters.
Suppose we have:
50 messages
and replace them with:
"The user wants to migrate PostgreSQL
from version 14 to 16 and cannot tolerate
more than 5 minutes of downtime."
We've reduced the number of tokens.
But we also potentially lost information.
Compression is therefore lossy unless the representation preserves everything required for the task.
Recent research on long-horizon agents specifically studies this trade-off: compressing accumulated interaction history can reduce memory requirements while trying to preserve task performance.
So don't blindly summarize everything.
Instead, identify what must survive.
π§ What Should Survive Compression?
For an agent working on a task, a useful compact state might preserve:
Goal
Current progress
Important decisions
Constraints
Known failures
Important outputs
Pending actions
Relevant identifiers
For example:
Before
User:
We need to migrate the database.
Assistant:
Let's inspect the schema.
Tool:
500 lines...
Assistant:
There are 3 large tables.
Tool:
800 lines...
Assistant:
We should migrate table A first.
Tool:
...
Assistant:
Migration failed because...
...
After compaction
Goal:
Migrate PostgreSQL database.
Progress:
Tables A and B analyzed.
Decision:
Migrate table A first.
Constraint:
Maximum downtime: 5 minutes.
Failure:
Previous migration failed due to missing index.
Next step:
Create the required index and retry.
Much smaller.
But much more useful.
β οΈ Don't Summarize Blindly
This is one of the most important practical lessons.
A generic instruction like:
"Summarize everything above."
doesn't guarantee that the summary will preserve the information your application needs later.
Imagine the original context contains:
Maximum allowed downtime: 5 minutes
If the summary accidentally removes that constraint, the agent might make a technically valid but operationally dangerous decision.
So compression should have explicit preservation rules.
For example:
Always preserve:
- User requirements
- Hard constraints
- IDs
- Decisions
- Pending actions
- Important errors
- Security restrictions
This is much safer than generic summarization.
4οΈβ£ Caching β Don't Recompute What Hasn't Changed
Now let's talk about something different.
Suppose your application repeatedly sends a large, mostly stable piece of context:
System Instructions
+
Tool Definitions
+
Large Reference Information
+
User Request
If the first three parts rarely change, repeatedly processing them can be wasteful.
That's where prompt caching / provider-side caching mechanisms can help, depending on the model provider.
Conceptually:
βββββββββββββββββββββββββββββββ
β Stable Context β
β β
β System Instructions β
β Tool Definitions β
β Reference Information β
ββββββββββββββββ¬βββββββββββββββ
β
CACHE
β
βΌ
Current User Request
β
βΌ
LLM
The exact caching behavior, eligibility rules, pricing and cache lifetime depend on the provider.
So don't assume:
"Caching means the model remembers everything."
It doesn't.
Caching is an infrastructure optimization, not the same thing as memory.
π§ Memory β Cache β Context
This is worth clarifying because these concepts are frequently mixed together.
Context
Information available to the model for the current inference.
Current request
+
Relevant history
+
Retrieved information
Memory
Information intentionally retained for future interactions.
User preference
+
Long-term facts
+
Previous task state
Cache
Previously processed/reusable information used to reduce repeated work, depending on the implementation.
Stable prompt prefix
+
Reusable computation
They solve different problems.
5οΈβ£ Structure β Context Isn't Just About What You Send
There's another subtle aspect:
How you organize the information matters.
Imagine giving an engineer this:
Database:
PostgreSQL
Issue:
Connection timeout
Logs:
...
User:
...
Instructions:
...
Relevant configuration:
...
Versus:
SYSTEM INSTRUCTIONS
TASK
CONSTRAINTS
RELEVANT EVIDENCE
TOOL RESULTS
CURRENT STATE
USER REQUEST
Structured context makes it easier for both developers and models to distinguish different types of information.
A useful pattern is to separate:
Stable information
+
Dynamic information
+
Retrieved information
+
Current task
This also makes your system easier to debug.
When something goes wrong, you can inspect which context component caused the problem.
π° Token Optimization Is More Than "Use Fewer Tokens"
Suppose an agent executes 20 steps.
And each step sends:
10,000 input tokens
That's potentially:
20 Γ 10,000
=
200,000 input tokens
across the run.
Now imagine that 6,000 of those tokens were:
- Old tool outputs
- Repeated instructions
- Irrelevant history
- Duplicate documents
You may have spent tokens carrying information the model didn't need.
This is why optimizing an agent requires looking at the whole execution, not just one prompt.
Recent work on context optimization explicitly treats long-running agents as a lifecycle problem involving accumulated histories, tool outputs, retrieval and repeated model calls.
π Think in Terms of a Context Budget
Instead of asking:
"How much context can my model handle?"
ask:
"How much context should I spend on this task?"
For example:
Context Budget: 20,000 tokens
System Instructions 2,000
Tool Definitions 3,000
Conversation 2,000
Retrieved Documents 5,000
Agent State 2,000
Current Request 1,000
Safety / Constraints 1,000
--------------------------------
Total 16,000
You still have some room.
But if retrieval suddenly returns:
15,000 tokens
you have a problem.
A context budget makes that visible.
π§Ή What Should We Remove First?
When context becomes too large, don't immediately summarize everything.
Use a hierarchy.
First: Remove unnecessary information
Irrelevant tool output
Old duplicate results
Unrelated documents
Then: Reduce verbose outputs
10,000-line log
β
Relevant error lines
Then: Retrieve only what matters
100 documents
β
Top relevant documents
Then: Compress history
50 conversation turns
β
Task state + important decisions
Finally: Consider architectural changes
For example:
- Separate agent contexts
- Store large artifacts externally
- Retrieve information on demand
- Split complex tasks
This approach avoids using expensive compression when simple filtering would have solved the problem.
π§° Keep Large Artifacts Outside the Context
This is particularly important for coding agents and document-heavy systems.
Suppose a tool generates:
50,000 lines of logs
Don't necessarily place all 50,000 lines into the next model request.
Instead:
Tool
β
Large Artifact
β
External Storage
β
Relevant Extract
β
LLM
The model can receive:
"Application crashed at 14:32:08.
Relevant stack trace:
..."
while the complete artifact remains available for retrieval if needed.
This pattern is especially useful when dealing with:
- Logs
- Large files
- Database exports
- Build artifacts
- Test reports
- Large API responses
π§ Context Engineering Is a Selection Problem
At this point, we can define the problem more precisely.
Every model call has a potential information pool:
ALL AVAILABLE INFORMATION
β
ββββββββββββββββββΌβββββββββββββββββ
βΌ βΌ βΌ
Memory RAG Tools
β β β
ββββββββββββββββββΌβββββββββββββββββ
βΌ
SELECT / RANK
β
βΌ
COMPRESS
β
βΌ
CONTEXT BUDGET
β
βΌ
LLM
The LLM doesn't need everything your system knows.
It needs the right working set for the current decision.
That's perhaps the most important idea in this entire series.
π§ͺ But How Do You Know Your Context Strategy Is Working?
This is where many AI projects stop too early.
They measure:
Did the model produce a good answer?
But that's not enough.
You should also ask:
Retrieval
- Did we retrieve the right documents?
- How often are irrelevant documents included?
Context
- How large is the context?
- Which components consume most tokens?
- How often are contexts compressed?
Agent
- How many steps does a task require?
- How often does the agent repeat a tool call?
- How often does it need to recover from an error?
Cost
- Input tokens per request
- Output tokens
- Tokens per successful task
- Cache utilization where applicable
Quality
- Task success rate
- Incorrect answers
- Missing information
- Failures after compression
A context optimization that saves 50% of tokens but causes 20% more task failures isn't necessarily an optimization.
βοΈ Optimize for Cost and Quality
This is an important mindset.
Don't optimize:
Tokens βββ
at any cost.
Optimize:
Quality
β²
β
β β
β β
β β
ββββββββββββββββββΊ
Cost
The goal is to find a useful balance between:
- Accuracy
- Reliability
- Latency
- Cost
- Context size
Sometimes sending more context is absolutely worth it.
Sometimes it isn't.
Context Engineering is about making that decision intentionally.
π¨ Common Context Engineering Mistakes
β 1. "Let's send everything"
More context isn't automatically better.
β 2. "Let's summarize everything"
Compression can remove information that becomes important later.
β 3. "The model has a huge context window, so we're fine"
A large context window is a capabilityβnot a reason to fill it unnecessarily.
β 4. "RAG will solve it"
Poor retrieval produces poor context.
RAG doesn't magically make irrelevant documents relevant.
β 5. "Just truncate the oldest messages"
The oldest information isn't necessarily the least important.
A previous decision or constraint may still matter.
β 6. "We'll optimize tokens later"
Context architecture can affect:
- Cost
- Latency
- Retrieval
- Memory
- Agent behavior
It becomes increasingly difficult to retrofit once the application grows.
ποΈ A Practical Context Pipeline
Putting everything together:
USER REQUEST
β
βΌ
ββββββββββββββββββββ
β Understand Task β
ββββββββββ¬ββββββββββ
β
ββββββββββββββββββΌβββββββββββββββββ
βΌ βΌ βΌ
Memory RAG Tools
β β β
ββββββββββββββββββΌβββββββββββββββββ
βΌ
SELECT RELEVANT
β
βΌ
REMOVE NOISE
β
βΌ
COMPRESS IF NEEDED
β
βΌ
CONTEXT BUDGET
β
βΌ
LLM
β
βΌ
Evaluate Result
β
ββββββββββ΄βββββββββ
β β
Continue Done
β
βΌ
Next Agent Step
This is the mental model I'd keep.
π A Simple Production Checklist
Before shipping an AI feature, ask:
Context
- [ ] What information does the model actually need?
- [ ] Which information is unnecessary?
- [ ] What is retrieved dynamically?
- [ ] What is persistent memory?
Tokens
- [ ] Do we have a context budget?
- [ ] Are large tool outputs filtered?
- [ ] Are duplicate documents removed?
- [ ] Are long histories compressed when appropriate?
Agents
- [ ] What is the maximum number of steps?
- [ ] What happens when a tool fails?
- [ ] What happens when the model chooses the wrong tool?
- [ ] Can the agent loop indefinitely?
Cost & Performance
- [ ] Are repeated stable inputs cacheable?
- [ ] Are large artifacts kept outside the context?
- [ ] Are we measuring tokens per successful task?
- [ ] Are we measuring latency?
Quality
- [ ] Does compression preserve important constraints?
- [ ] Are retrieved documents relevant?
- [ ] Are tool results trustworthy and validated?
- [ ] Do we have evaluations for context-related failures?
π§ The Bigger Picture
Let's step back.
When we started this series, Context Engineering sounded like:
"Writing better context for an LLM."
After five parts, we can see that it's much broader.
A production AI system may need to decide:
What should the model see?
β
What should it retrieve?
β
What should it remember?
β
What should it forget?
β
What should be compressed?
β
What should be cached?
β
What should be fetched only when needed?
β
What should the model act on?
β
What should never enter the context?
That is a very different problem from simply writing a better prompt.
π― The Five-Part Mental Model
Let's bring the entire series together.
Part 1 β Context Engineering
Give the model the right information.
β
Part 2 β Tokens, Context & Memory
Understand the constraints around that information.
β
Part 3 β RAG, Tools & MCP
Retrieve information and connect the model to external capabilities.
β
Part 4 β AI Agents
Let the system use that context and tools across multiple steps.
β
Part 5 β Context Optimization
Continuously decide what information should enter, stay, change or leave the context.
And that gives us the bigger picture:
USER
β
βΌ
ββββββββββββ
β GOAL β
ββββββ¬ββββββ
β
βΌ
βββββββββββββββββ
β Context Layer β
β β
β Memory β
β Retrieval β
β Tools β
β History β
βββββββββ¬ββββββββ
β
βΌ
Select / Rank
β
βΌ
Compress
β
βΌ
Context Budget
β
βΌ
LLM
β
βΌ
Agent
β
βββββββ΄ββββββ
βΌ βΌ
Tool Done
β
βΌ
New Context
β
ββββββββββββΊ
And that loop can continue for as long as the task requires.
π Final Thoughts
One of the biggest shifts happening in AI engineering is that we're moving away from thinking:
"How do I write the perfect prompt?"
and toward:
"How do I build the right information environment for the model at every step?"
That's Context Engineering.
The model matters.
The prompt matters.
But so do:
retrieval, memory, tools, context selection, compression, caching, state, evaluation and orchestration.
And as AI systems become more agentic, this problem becomes even more important.
Because the question isn't simply:
How much can the model see?
It's:
What should the model see right now?
That is the engineering problem.
π Key Takeaways
- More context isn't automatically better context.
- Treat the context window as a resource that needs to be managed.
- Remove irrelevant information before trying to compress it.
- Retrieve information when it's needed instead of loading everything upfront.
- Compress long histories carefully and preserve important decisions, constraints and state.
- Keep large artifacts outside the context when possible and retrieve only the relevant portions.
- Don't confuse context, memory and cachingβthey solve different problems.
- Set practical token budgets for agent runs.
- Measure task success, not just token reduction.
- Context optimization is a trade-off between quality, cost, latency and reliability.
- The goal isn't to give the model everything.
- The goal is to give the model what it needs for the decision it has to make right now.
Top comments (0)