On this page

AI Agents with Context

Complete guide to context management – token budgeting, optimization strategies, multi-agent policies, and monitoring.

AI Agents with Context#

See also: Five-Layer Agent Stack โ€” Context is Layer 2.

PraisonAI provides industry-leading context management with smart defaults, lazy loading, and 6 optimization strategies.

Diagram

๐Ÿค– LLM

โš™๏ธ Context Manager

๐Ÿ“ฅ Context Sources

System Prompt

Chat History

Tool Schemas

Tool Outputs

Memory/RAG

Token Estimator

Budget Allocator

Optimizer

Monitor

API Call

FeaturePraisonAILangChainCrewAIAgno
Smart Defaultsโœ…โŒโŒโŒ
Lazy Loading (0ms)โœ…โŒโŒโŒ
6 Strategiesโœ…โŒโŒโŒ
Per-Tool Budgetsโœ…โŒโŒโŒ
Session Deduplicationโœ…โŒโŒโš ๏ธ
LLM Summarizationโœ…โš ๏ธโŒโŒ

Quick Start#

Enable Context
from praisonaiagents import Agent

# Enable with defaults (auto-enabled when tools present)
agent = Agent(
    instructions="You are helpful",
    context=True
)
Custom Config
from praisonaiagents import Agent, ManagerConfig

agent = Agent(
    instructions="You are helpful",
    context=ManagerConfig(
        auto_compact=True,
        compact_threshold=0.8,
        strategy="smart",
        llm_summarize=True,
    )
)
YAML Config
context:
  auto_compact: true
  compact_threshold: 0.8
  strategy: smart
  llm_summarize: true
  tool_limits:
    tavily_search: 2000

What is Context?#

Context is everything sent to the LLM in a single API call. It includes:

Diagram
66%16%13%3%2%2%Token Budget (128K Model)HistoryTool OutputsOutput ReserveMemory/RAGSystem PromptTool Schemas
System Prompt

Agent instructions, role, and goals (~2K tokens)

Chat History

User/assistant messages (variable)

Tool Schemas

Function definitions (~2K tokens)

Tool Outputs

Results from tool calls (~20K tokens)

Memory/RAG

Retrieved context (~4K tokens)

Output Reserve

Space for LLM response (~8-16K tokens)


How Context Flows#

Single Agent Flow#

Diagram
LLMContext ManagerAgentUserLLMContext ManagerAgentUser“Search for AI news”Compose contextEstimate tokensCheck budgetOptimize if neededSend contextResponseResult

Multi-Agent Flow#

Diagram
Session CacheAgent 2Agent 1WorkflowSession CacheAgent 2Agent 1WorkflowSkip duplicate contentCreate shared cacheExecute taskAdd content hashesReturn outputPass output as contextCheck duplicatesReturn result

Optimization Strategies#

When context exceeds the threshold (default 80%), the optimizer kicks in:

Diagram

Choose Strategy

โšก Speed

TRUNCATE

๐Ÿ“œ Recency

SLIDING_WINDOW

๐ŸŽฏ Quality

SUMMARIZE

โš–๏ธ Balance

SMART

๐Ÿ”ง Tool Heavy

PRUNE_TOOLS

๐Ÿ›ก๏ธ Safety

NON_DESTRUCTIVE

StrategyHow It WorksBest For
truncateRemove oldest messagesSimple chatbots
sliding_windowKeep N recent messagesLong conversations
prune_toolsTruncate old tool outputsTool-heavy agents
summarizeLLM summarizes old contextCritical context
smartCombines all strategiesProduction use
non_destructiveTag for exclusion (undo-able)Audit trails

Smart Strategy Flow#

Diagram

Yes

Yes

Yes

No

No

No

Step 4: Summarize History

LLM summarizes conversation

Step 3: Sliding Window

Keep recent messages

Step 2: Truncate Tools

Truncate remaining outputs

Step 1: Summarize Tools

LLM summarizes tool outputs

Over Budget?

Under budget?

โœ… Done

Under budget?

Under budget?


Overflow Handling#

Diagram

< 70%

70-80%

80-90%

90-95%

Unsupported markdown: blockquote

Monitor

Auto-compact

Aggressive

Emergency truncation

Normal

Warning

Critical

Emergency

Overflow

LevelUsageAction
Normal< 70%No action
Warning70-80%Monitor
Critical80-90%Auto-compact triggers
Emergency90-95%Aggressive optimization
Overflow> 95%Emergency truncation

Token Budgeting#

The Context Budgeter allocates tokens across segments:

Diagram

Model Limit (128K)

Usable (112K)

System 2K

Rules 500

Skills 500

Memory 1K

Tools 2K

Tool Out 20K

History 84K

Buffer 1K

Output Reserve 16K

python
from praisonaiagents import ContextBudgeter

budgeter = ContextBudgeter(model="gpt-4o-mini")
budget = budgeter.allocate()

print(f"Model limit: {budget.model_limit:,}")      # 128,000
print(f"Output reserve: {budget.output_reserve:,}") # 16,384
print(f"Usable: {budget.usable:,}")                 # 111,616

Model Limits#

ModelContextOutput Reserve
gpt-4o128K16K
gpt-4o-mini128K16K
claude-3-opus200K8K
gemini-1.5-pro2M8K

Per-Tool Budgets#

Set different limits for different tools:

Diagram

2K limit

10K limit

5K protected

tavily_search

Truncate

code_executor

Keep more

file_read

Never prune

Context

python
from praisonaiagents import Agent, ManagerConfig

agent = Agent(
    instructions="You are helpful",
    context=ManagerConfig(
        tool_limits={
            "tavily_search": 2000,    # Search: 2K chars
            "tavily_extract": 5000,   # Full page: 5K chars
            "code_executor": 10000,   # Code output: 10K chars
        },
        protected_tools=["file_read"],  # Never pruned
    )
)

Session Deduplication#

Prevents duplicate content across agents in multi-agent workflows:

Diagram

Agent 3

Agent 2

Session Cache

Agent 1

Duplicate

Duplicate

Duplicate

Content A

Hash A โœ“

Hash B โœ“

Content A (skip)

Content B

Content A (skip)

Content B (skip)

Content C


Multi-Agent Policies#

Control how context is shared between agents:

Diagram

Policy: SHARED

Agent 1

Shared Context

Agent 2

Agent 3

Policy: ISOLATED (Default)

Agent 1 Context

Agent 2 Context

Agent 3 Context

ModeDescriptionUse Case
NONENo context sharedIndependent agents
SUMMARYSummarized contextReduce tokens
FULLFull context (bounded)Continuity needed
python
from praisonaiagents import ContextPolicy, ContextShareMode

policy = ContextPolicy(
    share=True,
    share_mode=ContextShareMode.SUMMARY,
    max_tokens=5000,
    preserve_recent_turns=3,
)

Context Monitoring#

Real-time snapshots for debugging:

Diagram

Agent

Context Manager

Monitor

context.txt

context.json

python
from praisonaiagents import Agent, ManagerConfig

agent = Agent(
    instructions="You are helpful",
    context=ManagerConfig(
        monitor_enabled=True,
        monitor_path="./context.txt",
        monitor_format="human",  # or "json"
        redact_sensitive=True,
    )
)

Snapshot Output#

================================================================================
PRAISONAI CONTEXT SNAPSHOT
================================================================================
Timestamp: 2026-01-24T06:00:00Z
Model: gpt-4o-mini
Model Limit: 128,000 tokens
Usable Budget: 111,616 tokens

--------------------------------------------------------------------------------
TOKEN LEDGER
--------------------------------------------------------------------------------
Segment              |     Tokens |     Budget |    Usage
--------------------------------------------------------------------------------
System Prompt        |        150 |      2,000 |    7.5%
History              |      5,230 |     84,616 |    6.2%
Tool Outputs         |      1,200 |     20,000 |    6.0%
--------------------------------------------------------------------------------
TOTAL                |      6,580 |    111,616 |    5.9%

Token Estimation#

Fast offline token counting (no API calls):

python
from praisonaiagents import (
    estimate_tokens_heuristic,
    estimate_messages_tokens,
)

# Estimate text tokens
tokens = estimate_tokens_heuristic("Hello world!")  # ~3

# Estimate message tokens
messages = [
    {"role": "user", "content": "Hello"},
    {"role": "assistant", "content": "Hi!"},
]
tokens = estimate_messages_tokens(messages)  # ~12
Content TypeAccuracy
English text~90-95%
Code~85-90%
Non-ASCII~80-85%

Rapid parallel code search for AI agents:

Diagram

๐Ÿ” Query

โšก FastContext

Parallel Executor

grep_search

glob_search

read_file

list_dir

๐Ÿ’พ Cache

๐Ÿ“„ Results

python
from praisonaiagents.context.fast import FastContext

fc = FastContext(
    workspace_path=".",
    max_turns=4,
    max_parallel=8,
)

result = fc.search("find authentication handlers")
print(f"Found {result.total_files} files in {result.search_time_ms}ms")
FeatureValue
Search Latency100-200ms
Cache Hit< 1ms
Parallel Speedup2-5x

CLI Commands#

bash
# Enable context in chat
praisonai chat --context

# Set strategy
praisonai chat --context-strategy smart

# Set threshold
praisonai chat --context-threshold 0.8

# Enable monitoring
praisonai chat --context-monitor

In-Session Commands#

CommandDescription
/context onEnable monitoring
/context offDisable monitoring
/context statsShow token ledger
/context dumpWrite snapshot now
/context compactForce optimization

Configuration Reference#

ManagerConfig Options#

OptionTypeDefaultDescription
auto_compactboolTrueAuto-optimize on threshold
compact_thresholdfloat0.8Trigger at this usage %
strategystr"smart"Optimization strategy
output_reserveintModel-specificReserved for output
llm_summarizeboolFalseUse LLM for summarization
tool_limitsdict{}Per-tool token limits
protected_toolslist[]Tools never pruned
monitor_enabledboolFalseEnable snapshots
redact_sensitiveboolTrueRedact secrets

Environment Variables#

bash
PRAISONAI_CONTEXT_OUTPUT_RESERVE=8000
PRAISONAI_CONTEXT_THRESHOLD=0.8
PRAISONAI_CONTEXT_MONITOR=true

Best Practices#

Enable for tool-heavy agents

Always enable context=True for agents with tools to prevent token overflow from large search results.

Use smart strategy in production

The smart strategy combines all optimization techniques intelligently.

Set per-tool limits

Configure lower limits for verbose tools (search, web scraping) and higher limits for code execution.

Monitor during development

Enable monitor_enabled=True to debug context issues and understand token usage.

Use session deduplication

In multi-agent workflows, deduplication prevents the same content from being processed multiple times.