Skip to content
Stop 6 of 7

Practical guidance for context optimization

Context Management Best Practices

Context Management Best Practices

Every agentic system lives or dies by how well it manages context. The same LLM with the same capabilities will produce dramatically different results depending on what information it has access to when making decisions. Context management isn't a supporting function - it's the core engineering challenge that determines whether an agentic system behaves intelligently or flails uselessly.

The difficulty stems from a fundamental tension: language models have fixed context windows, but real tasks require variable amounts of information. A simple lookup might need only a few hundred tokens of context. A complex synthesis task might require tens of thousands. The naive approach - stuff everything in and hope for the best - breaks down quickly. Effective context management requires deliberate strategies for what to include, what to exclude, and how to structure what remains.

This guide covers the principles, patterns, and practical techniques that make context management work in production agentic systems.

The Context Budget Problem

Language models operate within fixed context windows - the maximum number of tokens they can process in a single call. Modern models offer substantial windows (100K+ tokens for leading models), but those windows aren't as generous as they appear.

Consider a typical agentic task: a user asks a question, the system needs to retrieve relevant information, reason about it, and produce a response. The context budget must accommodate:

  • System instructions: The base prompt defining agent behavior (often 500-2000 tokens)
  • Conversation history: Previous exchanges providing continuity (varies widely)
  • Retrieved content: Documents, data, or tool outputs relevant to the task (the bulk of context)
  • Working state: Intermediate reasoning, partial results, accumulated facts
  • Response space: Room for the model to generate its answer

A 100K token context window sounds enormous until you realize that a single detailed document might consume 5,000 tokens, and you might need to reference ten such documents for a synthesis task. Add conversation history and system instructions, and you're suddenly making hard choices about what to include.

The first principle of context management: context is a finite resource that must be budgeted, not an infinite space to fill.

Token Economics

Not all tokens contribute equally to task completion. Some tokens are essential - without them, the model cannot produce a correct response. Others are helpful - they improve response quality but aren't strictly necessary. Still others are wasteful - they consume budget without improving outcomes.

Effective context management requires understanding these economics:

High-value tokens:

  • Facts directly relevant to the current question
  • Constraints that affect the valid solution space
  • Examples that demonstrate desired output format
  • Recent conversation context that affects interpretation

Medium-value tokens:

  • Background information that provides interpretive context
  • Related facts that might inform reasoning
  • Historical context from earlier in conversation
  • Metadata about sources and confidence levels

Low-value tokens:

  • Boilerplate text in retrieved documents
  • Redundant information stated multiple ways
  • Tangentially related content included "just in case"
  • Verbose formatting that could be compressed

The goal isn't to minimize token usage - it's to maximize value per token. A context window filled with high-value tokens produces better results than one padded with filler.

Working Memory vs. Long-Term Memory

Agentic systems need two distinct types of memory, and confusing them leads to poor architecture decisions.

Working memory is what the model can access during a single inference call - the contents of the context window. It's fast, immediately accessible, and strictly bounded by window size. Everything the model needs to know to complete the current step must be in working memory.

Long-term memory is everything the system knows but isn't currently in the context window. It lives in databases, vector stores, file systems, or external services. It's virtually unlimited in size but requires explicit retrieval to access. The model can't reason about information in long-term memory until that information is loaded into working memory.

The relationship between these memory types defines context management:

Long-Term Memory (unbounded) │ │ Retrieval ▼ Working Memory (bounded) │ │ Inference ▼ Output

Every agentic task involves the same fundamental flow: retrieve relevant information from long-term memory, load it into working memory, perform inference, and potentially update long-term memory with results.

The engineering challenge is making this flow efficient:

  • Retrieve the right information (not too much, not too little)
  • Structure it effectively within the context window
  • Avoid repeatedly retrieving the same information
  • Know when cached context has become stale

The Retrieval Trade-off

Retrieval is expensive - both in latency and in the risk of retrieving wrong or irrelevant content. But under-retrieving is equally problematic - the model can't reason about information it doesn't have.

The optimal retrieval strategy depends on task type:

For factual lookups: Retrieve conservatively. Better to acknowledge uncertainty than to retrieve marginally relevant content that might mislead. High precision, accept lower recall.

For synthesis tasks: Retrieve broadly. Cast a wider net to capture potentially relevant information, then let the model's reasoning sort out what matters. High recall, accept lower precision.

For multi-step tasks: Retrieve iteratively. Start with focused retrieval, see what the first inference step reveals, then retrieve again based on new understanding.

Context Layers

Production systems benefit from organizing context into explicit layers, each serving a distinct purpose and managed by different strategies.

Layer 1: System Context

System context defines what the agent is, what it can do, and how it should behave. This includes:

  • Role definition and personality
  • Capabilities and limitations
  • Output format requirements
  • Safety guidelines and constraints
  • Tool definitions and usage instructions

System context is the most stable layer - it changes rarely if ever during a session. Because it's always needed and never changes, it should be optimized for token efficiency. Every word in system context gets loaded on every inference call, so unnecessary verbosity compounds quickly.

Best practices for system context:

  • Keep it as short as possible while remaining complete
  • Use structured formats (lists, tables) for dense information
  • Avoid redundant explanations of obvious behaviors
  • Test whether removing a line changes agent behavior - if not, remove it

Layer 2: Session Context

Session context accumulates during a conversation - the history of exchanges, established facts, current topic, and user preferences revealed through interaction. Unlike system context, session context grows over time and eventually must be compressed or discarded.

Session context creates the illusion of memory across multiple exchanges. The model doesn't actually remember previous turns - it processes them fresh each time. But from the user's perspective, context persistence creates continuity.

Best practices for session context:

  • Summarize older exchanges rather than including full text
  • Track explicitly established facts separately from conversation history
  • Maintain a "current topic" indicator for retrieval optimization
  • Set clear boundaries for how far back context extends

Layer 3: Retrieved Context

Retrieved context is information pulled from long-term memory in response to a specific query or task. This is typically the most variable layer - it changes with every exchange, and its size can vary dramatically based on task complexity.

Retrieved context is where the context budget battle is usually won or lost. Retrieval systems often return far more content than fits in the context window, requiring hard decisions about what to include.

Best practices for retrieved context:

  • Rank retrieved content by relevance and select top results
  • Chunk documents into smaller pieces for finer-grained selection
  • Include source attribution to enable verification
  • Prefer recent content when freshness matters

Layer 4: Working State

Working state captures intermediate results from multi-step processes - partial reasoning, temporary variables, accumulated findings. For simple tasks, working state might not exist. For complex tasks, it can dominate the context budget.

Best practices for working state:

  • Explicitly structure working state (not free-form text)
  • Compress intermediate results when possible
  • Offload completed sub-task results to long-term memory
  • Clear working state when switching to unrelated tasks

Context Compilation Strategies

Context compilation is the process of assembling the context window for an inference call. The naive approach - concatenate everything in order - works but wastes budget and can confuse the model about what's important.

Priority-Based Assembly

Assign each context component a priority, then assemble in priority order until budget is exhausted:

Priority 1: System instructions (always included) Priority 2: Current query/task (always included) Priority 3: Directly relevant retrieved content Priority 4: Recent conversation context Priority 5: Background information Priority 6: Extended history ...

When the budget is exceeded, lower priority content gets dropped. This ensures critical information is always present even when the context window is under pressure.

Recency Weighting

For conversation history, recency matters. The last few exchanges are almost always more relevant than exchanges from ten turns ago. A simple strategy: include the last N exchanges in full, then summarize or sample from earlier exchanges.

More sophisticated approaches use semantic relevance - keep old exchanges that are topically related to the current query, summarize or drop those that aren't.

Query-Aware Selection

Retrieved content should be selected based on relevance to the current query, not just generic similarity to the conversation topic. A user asking "What's the pricing?" needs different context than a user asking "How does this compare to competitors?" even if both questions arise in the same conversation.

Query-aware selection means re-ranking retrieved content for each exchange, not relying on a static context set.

Progressive Summarization

As conversations grow, including full history becomes impossible. Progressive summarization compresses older context while preserving essential information.

The technique works in layers:

Layer 0 (raw): Full text of exchanges, kept for recent history only Layer 1 (key points): Main facts and decisions from each exchange Layer 2 (summary): High-level summary of conversation topic and outcomes Layer 3 (facts): Bare facts established during conversation, no narrative

Older exchanges progress through these layers. A conversation from five turns ago might exist at Layer 1. A conversation from fifty turns ago (if still relevant) might exist only at Layer 3.

The model that creates summaries needs clear instructions about what to preserve:

  • Explicit commitments or decisions made
  • Facts established or confirmed
  • User preferences revealed
  • Open questions or pending items
  • Topic transitions

Summarization is lossy by design - the goal is to lose the right information (verbose discussion, false starts, tangents) while preserving the right information (outcomes, facts, decisions).

Selective Retrieval Patterns

Not all retrieval situations are the same. Different patterns optimize for different needs.

Exact Match Retrieval

When looking for specific, known information: document by ID, fact by key, definition by term. No semantic interpretation needed - just fetch the precise item requested.

Use for: Specific document lookup, configuration values, known entity retrieval.

Semantic Search

When looking for conceptually relevant information where exact terms may not match. Vector similarity finds documents about the same topic even when vocabulary differs.

Use for: Finding relevant documentation, similar examples, topically related content.

Graph Traversal

When the starting point is known but related context is needed. Follow explicit relationships: what does this document reference? What references this document? What shares the same topic?

Use for: Finding context around a known item, exploring document relationships, building comprehensive views.

Hybrid Retrieval

Combine semantic search (finds conceptually similar) with graph traversal (finds structurally related). Neither alone captures everything relevant. Together they provide more complete context.

Most production systems benefit from hybrid retrieval. Pure semantic search misses explicit relationships. Pure graph traversal misses semantic similarity. The combination typically retrieves 30-50% more relevant content than either approach alone.

Context Freshness

Cached context goes stale. Documents get updated. Facts change. Conversations that established certain context may no longer be relevant. Managing freshness is essential for systems that run over extended periods.

Staleness Signals

Watch for signals that context may be outdated:

  • Time elapsed since retrieval
  • Source document modification timestamps
  • User signals that something seems wrong
  • Contradictions between cached and freshly retrieved content

Refresh Strategies

Time-based refresh: Re-retrieve periodically, regardless of apparent relevance. Simple but wasteful if context hasn't changed.

Event-based refresh: Re-retrieve when source systems signal updates. Efficient but requires integration with source systems.

Contradiction-triggered refresh: Re-retrieve when the model encounters apparent contradictions. Only refreshes when problems appear, but problems may not always be visible.

User-triggered refresh: Let users signal that context seems stale. Relies on user awareness but respects user agency.

Most systems combine strategies: time-based refresh for background maintenance, event-based refresh for critical updates, user-triggered refresh as a safety valve.

State vs. Context

A subtle but important distinction: state is what the system knows, context is what the model can see.

State includes:

  • Everything in long-term memory
  • Session variables and configuration
  • User profile and preferences
  • System status and capabilities

Context is the subset of state loaded into the current context window.

State management and context management are related but different concerns:

  • State management asks: what information should the system maintain?
  • Context management asks: what information should this inference call see?

Good state management makes good context management possible. If the right information isn't in state, it can't be in context. But good state management doesn't guarantee good context management - you can have excellent state and still load the wrong information into context.

State-Context Synchronization

State changes must propagate to context appropriately. When state updates:

Immediate propagation: Critical changes that affect current reasoning must appear in context immediately. User corrections, safety-relevant updates, task redefinitions.

Lazy propagation: Changes that aren't immediately relevant can wait until next retrieval. Background document updates, accumulated statistics, preference refinements.

Batch propagation: Some changes only make sense in aggregate. Don't update context every time a counter increments - update periodically with accumulated changes.

Common Pitfalls

Context management failures have recognizable patterns.

Pitfall: Context Stuffing

Loading everything available into context, hoping the model will figure out what's relevant.

Why it fails: Models struggle with irrelevant context. Attention is diluted across unimportant content. Important information gets lost in the noise. Response quality degrades even though "more information" is available.

How to avoid: Treat context budget as precious. Justify each item's inclusion. When in doubt, retrieve less and iterate.

Pitfall: Stale Context Blindness

Trusting that cached context remains accurate without verification.

Why it fails: Information changes. Documents are updated. What was true yesterday may not be true today. Stale context produces confident but wrong answers.

How to avoid: Implement freshness tracking. Set maximum cache ages. When accuracy matters, re-retrieve rather than relying on cache.

Pitfall: Missing Context Cascades

When context is incomplete, the model makes assumptions. Those assumptions propagate through reasoning, leading to conclusions that seem logical but are based on missing information.

Why it fails: The model doesn't know what it doesn't know. It will reason confidently from incomplete premises. There's no built-in "wait, I might be missing something" signal.

How to avoid: For important decisions, verify that context includes all relevant factors. Ask explicitly what information would change the conclusion. Build in uncertainty when operating on partial information.

Pitfall: Context Format Neglect

Providing good information in poor format: wall of text, inconsistent structure, buried key facts.

Why it fails: Models process sequential context. If the answer is buried on page 37 of a 50-page document dump, it's effectively not in context. Format affects accessibility.

How to avoid: Structure context for easy extraction. Put critical information early. Use clear headers and separators. Match format to what the model needs to find.

Pitfall: History Hoarding

Keeping full conversation history far longer than useful, consuming budget that could hold more relevant content.

Why it fails: Old conversation turns are rarely relevant to current questions. Every token spent on ancient history is a token not available for current context.

How to avoid: Implement progressive summarization. Set clear horizon for full history retention. Explicitly surface historical facts that remain relevant rather than keeping full transcripts.

Practical Checklists

Context Budget Checklist

Before each inference call:

  • System context is as concise as possible while complete
  • Retrieved content is ranked by relevance to current query
  • Conversation history is appropriately compressed
  • Working state is structured and current
  • Total context is within budget with room for response
  • Highest-priority items are positioned for attention

Retrieval Checklist

When implementing retrieval:

  • Query is formulated for the specific information needed
  • Retrieval method matches the type of information sought
  • Results are ranked and filtered before inclusion
  • Source attribution is preserved for verification
  • Freshness is appropriate for the use case
  • Fallback exists when retrieval returns nothing

State Management Checklist

For maintaining system state:

  • Long-term memory is organized for efficient retrieval
  • Session state is persisted appropriately
  • State changes propagate to context correctly
  • Stale state is refreshed or invalidated
  • State boundaries are clear (what's session vs. global)
  • Recovery path exists for corrupted state

Debugging Context Issues

When outputs seem wrong:

  • Verify the information needed to produce correct output is in context
  • Check that relevant information is positioned where the model will attend to it
  • Look for contradictory information that might confuse reasoning
  • Confirm retrieved content is fresh and accurate
  • Test whether additional context improves or degrades output
  • Examine whether context format obscures important information

Architectural Recommendations

Based on these principles, effective context management architectures share common characteristics:

Explicit context layers. Don't treat context as undifferentiated text. Maintain clear separation between system, session, retrieved, and working state context. Manage each layer with appropriate strategies.

Budget-aware assembly. Never blindly concatenate content. Always assemble context with budget constraints in mind. Know what gets dropped when budget is exceeded.

Retrieval abstraction. Separate retrieval logic from context assembly. The system should be able to change how it retrieves without changing how it assembles context.

Freshness tracking. Maintain metadata about when context was retrieved and from what sources. Enable staleness detection and refresh.

Progressive compression. Implement summarization as a first-class capability. Old context should gracefully degrade into more compact representations.

Observability. Log what context was assembled for each inference call. When outputs go wrong, you need to know exactly what the model saw.

Where Context Management Meets Other Concerns

Context management doesn't exist in isolation. It intersects with other agentic system concerns:

Orchestration: Multi-agent systems need coordinated context management. When one agent hands off to another, what context transfers? How is that handoff managed?

Tool use: Tools produce outputs that may need to enter context. Tool results vary wildly in size and structure. Context management must handle this variability.

Safety: Some context is sensitive. Context management must enforce access controls and prevent sensitive information from leaking into inappropriate contexts.

Evaluation: Testing agentic systems requires testing context management. Can the system retrieve relevant information? Does it include the right context? Do context management failures cascade into output failures?

Each of these topics deserves dedicated treatment. But all of them depend on solid context management fundamentals.

Conclusion

Context management is the invisible architecture that determines whether an agentic system performs intelligently. The same model with good context management produces dramatically better results than the same model with poor context management.

The core principles are straightforward: treat context as a finite resource, organize it into explicit layers, retrieve selectively, maintain freshness, and compress gracefully. The implementation details are where engineering effort concentrates.

Agentic systems that neglect context management hit invisible ceilings. They work for simple tasks, then fail mysteriously on complex ones. They handle short conversations, then degrade as sessions extend. They produce good results with clean inputs, then break down in realistic conditions.

Context management is where those ceilings get raised. The principles in this guide provide the foundation. The specific implementations will vary by system, domain, and requirements. But the fundamentals - budget consciousness, layered organization, selective retrieval, and graceful degradation - remain constant.

Build context management into the architecture from the start. It's far harder to retrofit than to design in.