The string form of
context= must name a valid preset (summarize, sliding_window, …). A typo raises ValueError at construction time with a “Did you mean …?” suggestion — see Fail-Loud Defaults. Matching is case-insensitive, whitespace-tolerant, and treats -/_ interchangeably.Before PraisonAI PR #4186, hyphen / uppercase / padded variants were accepted by the validator but silently left
context_manager=None. Upgrade to a build that includes PR #4186 to get the behaviour shown above.Context management is opt-in via the
context= parameter. When disabled (default), there is zero performance overhead.Quick Start
1
Enable with defaults
2
Fine-tune strategy and threshold
Default Behavior
Interactive Mode (praisonai chat)
To enable in CLI:
Auto-Agents Mode (Agents)
To enable:
Optimization Strategies
Strategy Overview
Requires PraisonAI PR #4242 (merged Aug 2026). Before this fix, only
smart actually ran end-to-end. Setting strategy to any of truncate, sliding_window, summarize, prune_tools, non_destructive, or conversation raised TypeError inside ContextManager and was silently swallowed — the history was returned unchanged and the hard-limit emergency truncation never fired. Upgrade to a build that includes PR #4242 to get the strategy you configured.Tool-call/tool-result pairs stay intact. Every strategy — including the default
truncate — snaps its compaction boundary so an assistant tool_calls message and its matching tool result are never separated. Long-running sessions on OpenAI, Anthropic, and Azure OpenAI keep working after compaction; you do not need a workaround for the messages with role 'tool' must be a response to a preceding message with 'tool_calls' 400.When to Use Each
truncate: Simple chatbots, Q&A agentssliding_window: Long conversations where recent context matters mostprune_tools: Tool-heavy agents with large outputsnon_destructive: Heavy tool outputs you must keep — tags old messages out of the effective window instead of deleting themsummarize: When historical context is criticalconversation: Multi-hour planning sessions, iterative development — automatically falls back tosmartwhen compaction ratio isn’t meaningful, making it safe as a default for long-running agentssmart(recommended): Production use, balances all concerns
Tool-call safety
Every strategy keeps assistanttool_calls messages and their matching tool results on the same side of the compaction boundary. If the raw token/message cut lands between a pair, the boundary is snapped outward so the pair is preserved together.
For truncate, the token-budget loop additionally drops a leading assistant tool_calls message together with all of its tool results in a single step. No configuration required — this behaviour is on by default for every strategy.
Overflow Handling
Threshold Playbook
“Immediate truncation” at 100% now counts the system prompt and tool schemas in the overhead, not just history tokens. An agent whose system prompt or tool block grew large will trip emergency truncation earlier than before — this is intentional and keeps the request inside the model window.
Automatic Handling
Whenauto_compact=True, the system automatically:
- Monitors token usage before each API call
- Triggers optimization when threshold is reached
- Applies the configured strategy
- Logs the optimization event
- If the configured strategy fails, falls back to emergency truncation targeting ~80% of the model window (result payload includes
emergency_truncated: True)
Failure Fallback
If the configured strategy raises,_apply_context_management no longer returns the history unchanged. It:
- Logs the failure at
errorlevel (waswarning). - Recomputes the model limit via
get_model_limit. - Adds the system prompt and tool-schema token cost as overhead.
- If
history + overhead > 95%of the window, callsemergency_truncatetargetingmax(1, int(model_limit * 0.8) - overhead). - Returns
{"emergency_truncated": True}so observability sinks can see the fallback fired.
Compaction never emits an orphaned
tool message. The recent-window boundary and any per-message drop pass keep the assistant tool_calls message and its matching tool result together — so strict providers (OpenAI, Anthropic, Azure OpenAI) never receive a partial pair.Budgeting
Token Allocation
The context budget is divided into segments:Custom Budgets
Monitoring
Enable Context Monitoring
Snapshot Output Example
Percentage Display
Context utilization is displayed with smart formatting:- Values
< 0.1%: Shows<0.1% - Values
< 1%: Shows 2 decimal places (e.g.,0.02%) - Values
>= 1%: Shows 1 decimal place (e.g.,5.3%)
Multi-Agent Policies
Isolated (Default)
Each agent has its own context ledger:Shared
Agents share a common context ledger:Redaction & Security
Sensitive data is automatically redacted in snapshots:- API keys (OpenAI, Anthropic, Google, AWS, etc.)
- Passwords and secrets
- Email addresses (optional)
- Custom patterns
Configuration Reference
ManagerConfig Options
How It Works
Best Practices
Pick strategy by session length
Pick strategy by session length
Short chats can use
truncate; long support threads benefit from smart or LLM summarisation.Isolate multi-agent context
Isolate multi-agent context
Use
policy="isolated" unless agents explicitly share a workspace.Leave redaction on
Leave redaction on
redact_sensitive=True protects API keys in tool results from appearing in logs.Revisit strategy after model changes
Revisit strategy after model changes
A larger context window may let you switch to lighter strategies and save latency.
Verify your build includes PR #4242 before non-smart strategies
Verify your build includes PR #4242 before non-smart strategies
If you pinned PraisonAI at a build before PR #4242, only
strategy="smart" actually compacted. All other values raised TypeError inside ContextManager and were silently swallowed — including the emergency truncation. Verify your build includes PR #4242 (merged Aug 2026) before shipping a non-smart strategy to production.Tool-using bots stay valid across compaction
Tool-using bots stay valid across compaction
When your agent calls tools inside a long conversation, compaction never separates an assistant
tool_calls message from its tool result. This holds for every strategy — truncate, sliding_window, prune_tools, summarize, conversation, and smart. Long-lived Telegram/Slack/gateway bots on strict providers keep working across weeks of history without provider 400s.Related
Context Budgeter
Token budget allocation
Context Monitor
Real-time context snapshots
Context Optimizer
Reduce context when over budget
Fast Context
High-performance context handling

