Skip to main content
Intelligent Conversation Compaction extracts the conversation’s topic, current goal, key decisions, and action items into a structured summary that preserves narrative continuity.

Quick Start

1

Enable on Agent via ManagerConfig

from praisonaiagents import Agent, ManagerConfig

agent = Agent(
    name="LongChat",
    instructions="Help the user across many turns.",
    context=ManagerConfig(
        auto_compact=True,
        strategy="conversation",
        conversation_compaction=True,
        conversation_analyzer_strategy="hybrid",
        conversation_min_compaction_ratio=0.3,
    ),
)

agent.start("Let's design a new product over the next few hours...")
2

Direct API for full control

from praisonaiagents import (
    get_conversation_analyzer,
    get_conversation_compactor,
)

analyzer = get_conversation_analyzer(strategy="hybrid")
compactor = get_conversation_compactor(analyzer, min_compaction_ratio=0.3)

compacted, context = compactor.compact_conversation(
    messages,
    target_tokens=4000,
    preserve_recent=5,
)
print(context.main_topic, context.current_goal, context.key_decisions)

How It Works

Analyzer Strategy Decision:
StrategyUses LLM?When to use
hybrid (default)LLM with rule-based fallbackBest balance — recommended
rule_basedNoPredictable / no extra LLM cost
llm_onlyYes (required)Highest fidelity, requires llm_analyze_fn

ConversationContext Shape

FieldTypeDescription
main_topicstrPrimary conversation topic
current_goalstrUser’s current objective
progress_summarystrProgress made towards goal
key_decisionsList[str]Important decisions made
important_factsList[str]Critical factual information
action_itemsList[str]Next steps or todo items
user_preferencesList[str]Expressed user preferences
tool_results_summaryList[str]Summaries of tool outputs
conversation_tonestrTone: casual, professional, technical
original_message_countintMessages before compaction
compacted_message_countintMessages after compaction
compaction_timestampfloatWhen compaction occurred
.to_summary_message() example output:
[💬 **Conversation Summary** - 47 messages compacted]

📋 **Topic**: Product design for developer tools
🎯 **Current Goal**: Create wireframes for the main dashboard
📈 **Progress**: Defined user personas, sketched initial layouts
🔑 **Key Decisions**:
• Using React with TypeScript
• Focus on mobile-first design
• Integrate with GitHub API
💡 **Important Context**:
• Users prefer dark mode interface
• Performance is critical for large repos
✅ **Action Items**:
• Finalize color scheme
• Create interactive prototype
⚙️ **User Preferences**: minimal UI, keyboard shortcuts, offline support

ManagerConfig Fields

FieldTypeDefaultDescription
conversation_compactionboolFalseEnable intelligent conversation compaction
conversation_analyzer_strategystr"hybrid""hybrid", "rule_based", or "llm_only"
conversation_min_compaction_ratiofloat0.3Skip compaction if savings below this ratio

OptimizerStrategy.CONVERSATION

Use strategy="conversation" for:
  • Long conversations with topic evolution — preserves narrative flow better than basic summarization
  • Multi-hour planning sessions — tracks decisions and action items across topic changes
  • Iterative development — maintains context of completed work and next steps
When to use different strategies:
  • strategy="conversation" → Structured summaries with topic/goal tracking
  • strategy="smart" → Adaptive optimization (pruning, sliding window, etc.)
  • strategy="summarize" → Simple LLM summarization without structure

Common Patterns

Multi-hour planning sessions:
agent = Agent(
    name="ProductPlanner",
    instructions="Help design products over extended sessions.",
    context=ManagerConfig(
        auto_compact=True,
        strategy="conversation",
        conversation_compaction=True,
        conversation_min_compaction_ratio=0.3,
    ),
)
Agent handoff with context preservation:
# First agent analyzes conversation
analyzer = get_conversation_analyzer(strategy="hybrid")
context = analyzer.analyze_conversation(messages)

# Pass structured context to new agent
new_agent.start(f"""
Continue this conversation about {context.main_topic}.
Current goal: {context.current_goal}
Progress: {context.progress_summary}
Recent decisions: {'; '.join(context.key_decisions[-3:])}
""")
Recover from ineffective basic summarization:
# If basic summarization lost important context
compactor = get_conversation_compactor(
    analyzer=get_conversation_analyzer("hybrid"),
    min_compaction_ratio=0.3
)
recovered_messages, rich_context = compactor.compact_conversation(
    original_messages,
    target_tokens=4000,
    preserve_recent=5
)

Best Practices

Setting the minimum compaction ratio to at least 30% ensures summaries provide meaningful token savings. Lower ratios may waste computational resources on minimal gains.
The hybrid strategy provides the best balance of quality and reliability. Use rule_based only when LLM costs are prohibitive, and llm_only only when maximum fidelity is critical.
Preserving the last 5 messages maintains immediate context while allowing effective compaction. Increase this for tool-heavy conversations where recent outputs are critical.
Always verify that critical decisions were captured correctly by checking context.key_decisions. This helps catch cases where important information might be lost.

LLM Context Compression

LLM-driven compression with session lineage and head/tail protection

Context Optimizer

Overview of all optimization strategies including conversation compaction

Context Strategies

Choosing the right optimization approach for your use case

Context Compaction

Basic compaction strategies and when to use intelligent compaction