/ examples / observability / langfuse_example.py
langfuse_example.py
 1  """
 2  Langfuse Integration Example (Updated for TraceSinkProtocol)
 3  
 4  This example shows how to use Langfuse for LLM observability with PraisonAI's native trace infrastructure.
 5  
 6  Setup:
 7      pip install "praisonai[langfuse]"
 8      export LANGFUSE_PUBLIC_KEY=pk-lf-xxx
 9      export LANGFUSE_SECRET_KEY=sk-lf-xxx
10  
11  Usage:
12      python langfuse_example.py
13  """
14  from praisonaiagents import Agent
15  from praisonai.observability import LangfuseSink
16  from praisonaiagents.trace.protocol import (
17      TraceEmitter, set_default_emitter
18  )
19  
20  # Initialize Langfuse observability
21  sink = LangfuseSink()
22  emitter = TraceEmitter(sink=sink, enabled=True)
23  set_default_emitter(emitter)
24  
25  # Create and run agent — all traces automatically captured
26  agent = Agent(
27      name="Coder",
28      instructions="You are a helpful coding assistant.",
29      llm="openai/gpt-4o-mini",
30  )
31  
32  try:
33      result = agent.start("Write a Python function to calculate fibonacci numbers")
34      print(result)
35  finally:
36      # Ensure traces are flushed and resources cleaned up
37      sink.flush()
38      sink.close()
39  
40  print("\nCheck Langfuse dashboard for traces!")