/ examples / observability / basic_usage.py
basic_usage.py
 1  """
 2  Basic Observability Usage Example
 3  
 4  This example demonstrates how to use the observability module
 5  with PraisonAI agents.
 6  
 7  Usage:
 8      # Set your provider's API key first
 9      export LANGFUSE_PUBLIC_KEY=pk-lf-xxx
10      export LANGFUSE_SECRET_KEY=sk-lf-xxx
11      
12      python basic_usage.py
13  """
14  
15  from praisonai_tools.observability import obs
16  from praisonaiagents import Agent
17  
18  # Initialize observability (auto-detects provider from env vars)
19  obs.init()
20  
21  # Or specify a provider explicitly
22  # obs.init(provider="langfuse", project_name="my-project")
23  
24  # Create an agent
25  agent = Agent(
26      instructions="You are a helpful assistant.",
27      llm="gpt-4o-mini",
28  )
29  
30  # Use trace context manager for complete workflow tracing
31  with obs.trace("chat-session", session_id="user-123"):
32      # Use span context manager for individual operations
33      with obs.span("user-query"):
34          response = agent.chat("What is the capital of France?")
35          print(response)
36  
37  # Log LLM calls manually if needed
38  obs.log_llm_call(
39      model="gpt-4o-mini",
40      input_messages="What is 2+2?",
41      output="4",
42      input_tokens=10,
43      output_tokens=1,
44  )
45  
46  # Check diagnostics
47  print("\nDiagnostics:")
48  print(obs.doctor())