basic_compaction.py
1 #!/usr/bin/env python3 2 """ 3 Basic Context Compaction Example for PraisonAI Agents. 4 5 This example demonstrates how to use context compaction with Agent: 6 1. Create an Agent with context= parameter for compaction 7 2. Use different compaction strategies via ManagerConfig 8 3. Monitor compaction results 9 10 Usage: 11 python basic_compaction.py 12 """ 13 14 from praisonaiagents import Agent 15 from praisonaiagents.context import ManagerConfig 16 17 18 def main(): 19 print("=" * 60) 20 print("Agent-Centric Context Compaction Demo") 21 print("=" * 60) 22 23 # ========================================================================== 24 # Available Strategies 25 # ========================================================================== 26 print("\n--- Available Compaction Strategies ---") 27 28 strategies = [ 29 ("truncate", "Remove oldest messages first"), 30 ("sliding_window", "Keep most recent messages within limit"), 31 ("summarize", "Replace old messages with summary"), 32 ("smart", "Intelligently select messages to keep"), 33 ] 34 35 for name, desc in strategies: 36 print(f" {name}: {desc}") 37 38 # ========================================================================== 39 # Agent with Context Compaction via context= param 40 # ========================================================================== 41 print("\n--- Creating Agent with Context Compaction ---") 42 43 # Use context= parameter with ManagerConfig for compaction 44 agent = Agent( 45 name="LongChat", 46 instructions="You are a helpful assistant for extended conversations.", 47 context=ManagerConfig( 48 auto_compact=True, 49 compact_threshold=0.8, 50 strategy="sliding_window", 51 ) 52 ) 53 54 print(f"Agent: {agent.name}") 55 print(f"Context manager: {agent.context_manager is not None}") 56 if agent.context_manager: 57 print(f"Auto-compact: {agent.context_manager.config.auto_compact}") 58 print(f"Threshold: {agent.context_manager.config.compact_threshold}") 59 print(f"Strategy: {agent.context_manager.config.strategy}") 60 61 # ========================================================================== 62 # Different Strategy Examples via context= param 63 # ========================================================================== 64 print("\n--- Strategy Examples ---") 65 66 # Sliding window strategy 67 sliding_agent = Agent( 68 name="SlidingAgent", 69 instructions="You are helpful.", 70 context=ManagerConfig( 71 auto_compact=True, 72 strategy="sliding_window", 73 ) 74 ) 75 print(f" Sliding: strategy={sliding_agent.context_manager.config.strategy}") 76 77 # Summarize strategy 78 summarize_agent = Agent( 79 name="SummarizeAgent", 80 instructions="You are helpful.", 81 context=ManagerConfig( 82 auto_compact=True, 83 strategy="summarize", 84 ) 85 ) 86 print(f" Summarize: strategy={summarize_agent.context_manager.config.strategy}") 87 88 # Smart strategy 89 smart_agent = Agent( 90 name="SmartAgent", 91 instructions="You are helpful.", 92 context=ManagerConfig( 93 auto_compact=True, 94 strategy="smart", 95 ) 96 ) 97 print(f" Smart: strategy={smart_agent.context_manager.config.strategy}") 98 99 # ========================================================================== 100 # Context Manager Stats Demo 101 # ========================================================================== 102 print("\n--- Context Manager Stats Demo ---") 103 104 # Access context manager stats 105 if agent.context_manager: 106 stats = agent.context_manager.get_stats() 107 print(f" Model: {stats.get('model', 'N/A')}") 108 print(f" Model limit: {stats.get('model_limit', 'N/A')}") 109 print(f" Output reserve: {stats.get('output_reserve', 'N/A')}") 110 print(f" Usable budget: {stats.get('usable_budget', 'N/A')}") 111 else: 112 print(" Context manager not initialized") 113 114 print("\n" + "=" * 60) 115 print("Demo Complete!") 116 print("=" * 60) 117 118 119 if __name__ == "__main__": 120 main()