/ examples / context / 05_monitoring.py
05_monitoring.py
 1  """
 2  Context Monitoring Example
 3  
 4  Demonstrates monitoring context usage with logging and metrics.
 5  """
 6  
 7  from praisonaiagents import Agent
 8  from praisonaiagents.context import ContextConfig, MonitorConfig, OptimizerStrategy
 9  
10  # Example 1: Human-readable monitoring
11  agent_human = Agent(
12      name="MonitoredAgent",
13      instructions="You are a helpful assistant",
14      context=ContextConfig(
15          auto_compact=True,
16          monitor=MonitorConfig(
17              enabled=True,
18              path="./context_logs/",
19              format="human",           # Human-readable format
20              frequency="turn",         # Log on every turn
21              redact_sensitive=True,    # Redact sensitive data
22              multi_agent_files=True,   # Separate files per agent
23          )
24      )
25  )
26  
27  # Example 2: JSON monitoring (for parsing)
28  agent_json = Agent(
29      name="JSONMonitorAgent",
30      instructions="You are a helpful assistant",
31      context=ContextConfig(
32          monitor=MonitorConfig(
33              enabled=True,
34              path="./context_logs/",
35              format="json",            # JSON format for parsing
36              frequency="tool_call",    # Log on tool calls
37          )
38      )
39  )
40  
41  # Example 3: Overflow-only monitoring
42  agent_overflow = Agent(
43      name="OverflowAgent",
44      instructions="You are a helpful assistant",
45      context=ContextConfig(
46          monitor=MonitorConfig(
47              enabled=True,
48              path="./context_logs/",
49              format="human",
50              frequency="overflow",     # Only log on overflow
51          )
52      )
53  )
54  
55  if __name__ == "__main__":
56      print("=== Context Monitoring Example ===")
57      print()
58      
59      # Show monitor configuration
60      config = MonitorConfig(
61          enabled=True,
62          path="./context_logs/",
63          format="human",
64          frequency="turn",
65          redact_sensitive=True,
66      )
67      
68      print("Monitor Config:")
69      print(f"  enabled: {config.enabled}")
70      print(f"  path: {config.path}")
71      print(f"  format: {config.format}")
72      print(f"  frequency: {config.frequency}")
73      print(f"  redact_sensitive: {config.redact_sensitive}")
74      print()
75      
76      print("Frequency options:")
77      print("  'turn'      - Log on every conversation turn")
78      print("  'tool_call' - Log when tools are called")
79      print("  'manual'    - Manual logging only")
80      print("  'overflow'  - Log only when context overflows")
81      print()
82      
83      print("Format options:")
84      print("  'human' - Human-readable format")
85      print("  'json'  - JSON format for parsing")