Skip to main content

AI Agents with Context

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

Quick Start


What is Context?

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

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

Multi-Agent Flow


Optimization Strategies

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

Smart Strategy Flow


Overflow Handling


Token Budgeting

The Context Budgeter allocates tokens across segments:

Model Limits


Per-Tool Budgets

Set different limits for different tools:

Session Deduplication

Prevents duplicate content across agents in multi-agent workflows:

Multi-Agent Policies

Control how context is shared between agents:

Context Monitoring

Real-time snapshots for debugging:

Snapshot Output


Token Estimation

Fast offline token counting (no API calls):

Rapid parallel code search for AI agents:

CLI Commands

In-Session Commands


Configuration Reference

ManagerConfig Options

Environment Variables


Best Practices

Always enable context=True for agents with tools to prevent token overflow from large search results.
The smart strategy combines all optimization techniques intelligently.
Configure lower limits for verbose tools (search, web scraping) and higher limits for code execution.
Enable monitor_enabled=True to debug context issues and understand token usage.
In multi-agent workflows, deduplication prevents the same content from being processed multiple times.

Memory

Persistent storage across sessions

Knowledge

Pre-loaded reference documents

Context vs Memory

When to use each system

Context vs Knowledge

Runtime vs pre-loaded data
Context management uses lazy loading throughout. Setting context=True adds only 1 boolean assignment at creation time (0ms). The ContextManager is only instantiated when first accessed.