Context Engineering Best Practices: Prompt Compression for AI Agents
Prompt compression prevents context window overflow by condensing historical conversation data while preserving semantic meaning for accurate agent reasoning.
Context engineering determines what information an AI agent can access at each decision point. In bojieli/ai-agent-book, Chapter 2 establishes the foundational framework for managing this information efficiently, particularly when conversations grow beyond model token limits. The source code analysis reveals three distinct families of compression techniques, each suited to different operational requirements.
Understanding the Context Engineering Problem Space
Every AI agent conversation consists of two structural components: the static prefix and the dynamic trajectory book-en/chapter2.md#L31-L66.
- Static prefix: System prompt and tool definitions that remain constant
- Dynamic trajectory: Message history that grows with each interaction
The trajectory expansion creates the primary constraint. Modern models support 128k token contexts, but long-running agent sessions quickly exhaust this capacity. Without intervention, the agent loses access to early conversation turns, degrading reasoning quality and tool selection accuracy.
The Three Families of Prompt Compression
The book defines compression strategies based on their awareness of surrounding context book-en/chapter2.md#L165-L190:
Non-Context-Aware Summarization
This approach applies standard LLM summarization to message blocks without considering their role in the reasoning chain. It works fastest when the content contains primarily narrative text without critical dependencies.
Trade-off: Maximum compression ratio with maximum information loss. Early tool results referenced later become unrecoverable.
Context-Aware Summarization
Context-aware methods preserve the reasoning chain—the logical connections between observations, thoughts, and actions. The book emphasizes maintaining tool call structures and their outcomes when subsequent decisions depend on them.
Implementation insight: Summarize the narrative wrapper while retaining structured data (JSON tool outputs, numeric results) in original or compressed tabular form.
Selective Trajectory Truncation
Rather than summarizing, this family aggressively removes entire message blocks based on relevance scoring. The book recommends hybrid approaches that combine truncation for distant history with summarization for recent but non-critical turns.
| Criterion | Action |
|---|---|
| Messages older than N turns | Truncate completely |
| Tool results unreferenced subsequently | Remove or compress heavily |
| System-critical configuration data | Preserve verbatim |
Practical Implementation Guidelines
When to Trigger Compression
Set token thresholds based on model limits minus safety margin:
COMPRESSION_THRESHOLD = model_context_limit * 0.85 # 85% utilization
This reserves capacity for the current turn's generation and prevents hard failures.
Preserve These Elements
According to the source analysis, never compress without retaining:
- Current task objective from system prompt
- Active tool schemas required for valid function calling
- Recent trajectory (typically last 5-10 exchanges) in full detail
- Cross-referenced historical data that subsequent turns depend upon
Compression Quality Validation
Implement verification that compressed context maintains task coherence:
def verify_compression(original, compressed, validator_model):
"""
Ensure critical facts and relationships survive compression.
Returns: (is_valid, missing_elements)
"""
validation_prompt = f"""
Original context length: {len(original)} tokens
Compressed context length: {len(compressed)} tokens
Identify any task-critical information present in original
but missing or altered in compressed version.
"""
return validator_model.generate(validation_prompt)
Performance Characteristics
| Method | Latency | Fidelity | Best For |
|---|---|---|---|
| Non-context-aware summarization | Low | Low | Disposable chat history |
| Context-aware summarization | Medium | Medium | Multi-turn reasoning tasks |
| Selective truncation | Very low | Variable | High-volume, low-dependency sessions |
Summary
- Context engineering structures agent information into static prefix and dynamic trajectory components
- Prompt compression becomes necessary when trajectory growth approaches model context limits
- Three compression families offer different fidelity-speed trade-offs: non-context-aware summarization, context-aware summarization, and selective truncation
- Context-aware approaches preserve reasoning chains essential for correct tool use
- Implement compression triggers at 85% of context capacity with validation checks
Frequently Asked Questions
What is the most common cause of context window overflow in AI agents?
Unbounded growth of the dynamic trajectory—message history that accumulates with each user interaction and tool execution. As described in book-en/chapter2.md, this historical record can exceed 128k tokens in extended sessions, forcing the agent to discard or compress older information.
How does context-aware summarization differ from standard summarization?
Context-aware summarization explicitly preserves the reasoning chain connecting observations to actions, as implemented in the compression framework from bojieli/ai-agent-book. Standard summarization treats all text uniformly, often destroying the logical dependencies that subsequent agent decisions require.
At what token threshold should prompt compression activate?
The book recommends triggering compression at approximately 85% of model context capacity. This safety margin accommodates the current turn's generation needs while avoiding hard failures from window overflow.
Can compressed context be recovered or reversed?
Generally no—compression is destructive. The source analysis emphasizes implementing validation checkpoints before compression and maintaining checkpoints at key decision boundaries when full history might be needed for debugging or audit purposes.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →