How to Implement Context Engineering for Better AI Agent Performance

Context engineering is the systematic design of the information context that an AI agent receives at each interaction step, implemented through a declarative configuration stack that manages token budgets, compression triggers, and stateful message containers.

The ai-agent-book repository by bojieli provides a production-ready framework for implementing context engineering in AI agents. By structuring how conversational state is stored, optimized, and compressed, you can prevent token limit errors, reduce latency, and maintain essential information across multi-turn interactions. This approach centers on three core abstractions that work together to manage the agent's working memory efficiently.

What is Context Engineering?

Context engineering refers to the deliberate architecture of the information payload fed to large language models during agent execution. Rather than passively accumulating messages, engineered contexts apply token-budget constraints, selective pruning, and LLM-based compression to keep the prompt within model limits while preserving task-critical information.

In the ai-agent-book implementation, this engineering happens declaratively through configuration objects that specify when and how to optimize the context, allowing the runtime to handle the mechanical details of compression and state management.

Core Components of the Context Engineering Stack

The framework builds context engineering around three primary abstractions defined in aworld/config/conf.py and aworld/core/context/base.py.

Context: The Mutable State Container

The Context class serves as the mutable container that carries user messages, intermediate tool results, and system-level metadata throughout the agent's lifecycle. Implemented in aworld/core/context/base.py, it provides a simple API including add_message(), add_tool_result(), and get() methods that agents and tools can read and write without coupling to a specific LLM provider.

This separation of concerns allows the conversational state to persist across multiple execution steps while remaining agnostic to the underlying model.

ContextRuleConfig: Declarative Optimization Rules

ContextRuleConfig acts as the declarative configuration that instructs the runtime how to treat the context. Defined in aworld/config/conf.py (lines 47-56), this configuration enables:

  • Performance optimization through token-budget limits
  • Automatic LLM-based compression when contexts grow too large
  • Selective pruning of irrelevant entries based on priority scores

Compression and Optimization Configurations

Two concrete configuration objects control the optimization behavior:

  • LlmCompressionConfig (lines 35-45 in aworld/config/conf.py): Triggers a second-pass LLM call when the accumulated token count exceeds trigger_compress_token_length. The compress_model parameter specifies which model handles the compression task.

  • OptimizationConfig: Caps the total context size using max_token_budget_ratio, which calculates the maximum tokens as a percentage of the model's total capacity defined in ModelConfig.max_model_len.

How Context Engineering Works in Practice

The execution flow follows a four-step pipeline managed by the ContextManager and MetricContext utilities in aworld/engine:

  1. Initialize a Context instance for each task or reuse a global context object to maintain state.

  2. Configure ContextRuleConfig via AgentConfig.context_rule or pass it directly to the Engine. This rule set defines the triggers for compression and pruning.

  3. Monitor token budgets during execution. The engine tracks accumulated tokens against ModelConfig.max_model_len and activates compression when thresholds are breached.

  4. Apply optimization based on the configuration. When max_token_budget_ratio is set aggressively low, the system prunes earlier, trading context richness for reduced latency.

Implementation Examples

Basic Setup with ContextRuleConfig

This example demonstrates configuring an agent with automatic compression triggered at 4000 tokens and a token budget capped at 40% of the model's capacity:

from aworld.config.conf import AgentConfig, ContextRuleConfig, OptimizationConfig, LlmCompressionConfig, ModelConfig
from aworld.core.context.base import Context
from aworld.agent import Agent

# Define the compression model (any OpenAI-compatible backend)

compress_model = ModelConfig(
    llm_provider="openai",
    llm_model_name="gpt-4o-mini",
    llm_api_key="YOUR_KEY",  # Load from environment variables in production

)

# Build the context engineering rules

context_rule = ContextRuleConfig(
    optimization_config=OptimizationConfig(
        enabled=True, 
        max_token_budget_ratio=0.4
    ),
    llm_compression_config=LlmCompressionConfig(
        enabled=True,
        trigger_compress_token_length=4000,
        compress_model=compress_model,
    ),
)

# Assemble agent configuration

agent_cfg = AgentConfig(
    llm_config=ModelConfig(
        llm_model_name="gpt-4o", 
        llm_provider="openai"
    ),
    context_rule=context_rule,
)

# Instantiate and run

agent = Agent(config=agent_cfg)
ctx = Context()
response = await agent.run("Explain the impact of climate change on coral reefs.", ctx)

Multi-Turn Dialogue Management

To maintain context across multiple interactions, reuse the same Context instance. The engine automatically monitors token accumulation and applies compression when the trigger_compress_token_length threshold is exceeded:


# Reuse ctx from previous example

conversation_turns = [
    "What are the main stressors?",
    "How can we mitigate them?",
    "What are the economic implications?"
]

for user_msg in conversation_turns:
    await agent.run(user_msg, ctx)
    # Context automatically accumulates; compression triggers if budget exceeded

Custom Compression Strategies

For lightweight, rule-based compression without LLM overhead, configure the llmlingua compressor type. The framework automatically selects LinguaCompressor from aworld/util/compression.py when specified:

from aworld.util.compression import LinguaCompressor

compression_cfg = LlmCompressionConfig(
    enabled=True,
    compress_type="llmlingua",
    trigger_compress_token_length=6000,
    compress_model=ModelConfig(),  # Dummy config; not used by llmlingua

)

# Update the context rule

context_rule = ContextRuleConfig(
    llm_compression_config=compression_cfg
)

This approach uses tokenization-based compression rather than a secondary LLM call, reducing costs for high-volume applications.

Key Source Files and Architecture

Understanding these source files is essential for customizing the context engineering pipeline:

Summary

  • Context engineering manages AI agent performance by systematically designing the information payload through declarative configuration.
  • The Context class in aworld/core/context/base.py provides a mutable, model-agnostic container for conversational state.
  • ContextRuleConfig enables token-budget management and automatic compression without imperative code changes.
  • LlmCompressionConfig triggers secondary LLM calls for intelligent summarization when contexts exceed trigger_compress_token_length.
  • OptimizationConfig provides hard caps via max_token_budget_ratio to prevent token limit errors and control latency.
  • The framework supports both LLM-based and lightweight tokenizer-based compression via the compress_type parameter.

Frequently Asked Questions

What is the difference between context engineering and prompt engineering?

Prompt engineering focuses on crafting individual input strings to elicit better responses, while context engineering manages the entire conversational state across multiple turns. In the ai-agent-book framework, context engineering handles token budgets, compression triggers, and state persistence through the Context and ContextRuleConfig abstractions, whereas prompt engineering would involve modifying the specific text sent in a single add_message() call.

How does the compression trigger work in practice?

When the accumulated token count in a Context instance exceeds the trigger_compress_token_length defined in LlmCompressionConfig, the ContextManager automatically invokes the specified compress_model. This secondary LLM receives the current context messages and returns a shortened version that preserves essential task information. The compressed context then replaces the original in subsequent inference calls, as demonstrated in test_context_compressor.py.

Can I use different compression strategies for different agents?

Yes, each AgentConfig can specify its own ContextRuleConfig with distinct compression settings. You can configure one agent to use LLM-based compression with gpt-4o-mini for high-fidelity summarization, while another uses compress_type="llmlingua" for cost-sensitive operations. Since the Context object is passed explicitly to agent.run(), different agents can also share contexts while applying their own optimization rules during their respective execution cycles.

What happens if the context exceeds the token budget before compression triggers?

The OptimizationConfig.max_token_budget_ratio parameter acts as a safety cap calculated against ModelConfig.max_model_len. If the context approaches this hard limit before the compression trigger fires, the system applies aggressive pruning to remove lower-priority entries. This prevents runtime token limit errors at the cost of potentially losing older conversation details, ensuring the agent remains functional even during extended interactions.

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:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →