Context Optimizer CLI
CLI reference for context optimization strategies and configuration
from praisonaiagents import Agent
agent = Agent(name="optimizer", instructions="Optimise agent prompts and configurations.")
agent.start("Optimise this agent's system prompt for better accuracy.")Configure context optimization behaviour via CLI flags and interactive commands.
The user chats in the CLI; context flags compact history before the model hits token limits.
How It Works
Quick Start
- Simple Usage
bash praisonai chat --context-strategy smart --context-auto-compactInside the session, compact manually when needed:
bash > /context compact - With Configuration
bash praisonai chat \ --context-strategy smart \ --context-threshold 0.7 \ --no-context-auto-compactOr set defaults in
config.yaml:yaml context: auto_compact: true compact_threshold: 0.8 strategy: smart
CLI Flags
Strategy
# Smart optimizer (default, recommended)
praisonai chat --context-strategy smart
# Simple truncation
praisonai chat --context-strategy truncate
# Sliding window
praisonai chat --context-strategy sliding_window
# Summarize old messages
praisonai chat --context-strategy summarize
# Prune old tool outputs
praisonai chat --context-strategy prune_tools| Strategy | Description | Best For |
|---|---|---|
smart | Intelligent combination | General use |
truncate | Remove oldest messages | Fast, simple |
sliding_window | Keep recent N messages | Conversation flow |
summarize | Compress old messages | Context preservation |
prune_tools | Truncate tool outputs | Tool-heavy workflows |
Auto-Compaction
# Enable (default)
praisonai chat --context-auto-compact
# Disable
praisonai chat --no-context-auto-compactThreshold
# Trigger at 80% usage (default)
praisonai chat --context-threshold 0.8
# More aggressive (70%)
praisonai chat --context-threshold 0.7
# Less aggressive (90%)
praisonai chat --context-threshold 0.9Interactive Commands
Manual Compaction
> /context compactOutput:
Optimizing context...
✓ Optimized: 45,000 → 30,000 tokens
Saved 15,000 tokens (33.3%)
Strategy: smartView Optimization History
> /context historyOutput:
Optimization History
Time Event Tokens Saved
----------------------------------------------------------------------
2024-01-07T12:00:00 overflow_detected 45,000 -
2024-01-07T12:00:01 auto_compact 45,000 -15,000View Current Config
> /context configShows auto_compact, threshold, and strategy settings.
Environment Variables
export PRAISONAI_CONTEXT_AUTO_COMPACT=true
export PRAISONAI_CONTEXT_THRESHOLD=0.8
export PRAISONAI_CONTEXT_STRATEGY=smartconfig.yaml
context:
auto_compact: true
compact_threshold: 0.8
strategy: smart
compression_min_gain_pct: 5.0
compression_max_attempts: 3Strategy Details
Smart (Recommended)
Combines multiple strategies intelligently:
- First tries non-destructive pruning
- Falls back to sliding window
- Uses truncation as last resort
praisonai chat --context-strategy smartTruncate
Simply removes oldest messages until under budget.
praisonai chat --context-strategy truncateSliding Window
Keeps the most recent N messages that fit in budget.
praisonai chat --context-strategy sliding_windowSummarize
Compresses old messages into a summary (requires LLM call).
praisonai chat --context-strategy summarizePrune Tools
Truncates old tool outputs while preserving recent ones.
praisonai chat --context-strategy prune_toolsTroubleshooting
Context still overflowing
# Lower threshold
praisonai chat --context-threshold 0.6
# Use more aggressive strategy
praisonai chat --context-strategy truncateLosing important context
# Use smart strategy
praisonai chat --context-strategy smart
# Or increase threshold
praisonai chat --context-threshold 0.9Auto-compact not triggering
# Check if enabled
> /context config
# Verify threshold
> /context statsBest Practices
Start with smart strategy
smart combines pruning, sliding window, and summarisation — use it unless you have a specific reason not to.
Set threshold below 1.0
Trigger compaction at 0.7–0.8 so optimisation runs before the model hard-fails on context overflow.
Use /context stats during long sessions
Check token usage before manually compacting — /context history shows what each compaction saved.