Langfuse

Integrate Kern with Langfuse to send traces and gain insights into your agent's performance.

Integrating Kern with Langfuse

Langfuse provides a robust platform for tracing and monitoring AI model calls. By integrating Kern with Langfuse, you can utilize OpenInference and OpenLIT to send traces and gain insights into your agent's performance.

Prerequisites

  1. Install Dependencies

    Ensure you have the necessary packages installed:

    1uv pip install kern-ai openai langfuse opentelemetry-sdk opentelemetry-exporter-otlp openinference-instrumentation-kern-ai
  2. Setup Langfuse Account

    • Either self-host or sign up for an account at Langfuse.
    • Obtain your public and secret API keys from the Langfuse dashboard.
  3. Set Environment Variables

    Configure your environment with the Langfuse API keys:

    1export LANGFUSE_PUBLIC_KEY=<your-public-key>
    2export LANGFUSE_SECRET_KEY=<your-secret-key>

Sending Traces to Langfuse

  • Example: Using Langfuse with OpenInference

This example demonstrates how to instrument your Kern agent with OpenInference and send traces to Langfuse.

1import base64
2import os
3
4from kern.agent import Agent
5from kern.models.openai import OpenAIResponses
6from kern.tools.yfinance import YFinanceTools
7from openinference.instrumentation.kern import AgnoInstrumentor
8from opentelemetry import trace as trace_api
9from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
10from opentelemetry.sdk.trace import TracerProvider
11from opentelemetry.sdk.trace.export import SimpleSpanProcessor
12
13# Set environment variables for Langfuse
14LANGFUSE_AUTH = base64.b64encode(
15 f"{os.getenv('LANGFUSE_PUBLIC_KEY')}:{os.getenv('LANGFUSE_SECRET_KEY')}".encode()
16).decode()
17os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://us.cloud.langfuse.com/api/public/otel"
18os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"
19
20# Configure the tracer provider
21tracer_provider = TracerProvider()
22tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
23trace_api.set_tracer_provider(tracer_provider=tracer_provider)
24
25# Start instrumenting kern
26AgnoInstrumentor().instrument()
27
28# Create and configure the agent
29agent = Agent(
30 name="Stock Price Agent",
31 model=OpenAIResponses(id="gpt-5.2"),
32 tools=[YFinanceTools()],
33 instructions="You are a stock price agent. Answer questions in the style of a stock analyst.",
34 debug_mode=True,
35)
36
37# Use the agent
38agent.print_response("What is the current price of Tesla?")
  • Example: Using Langfuse with OpenLIT

This example demonstrates how to use Langfuse via OpenLIT to trace model calls.

1import base64
2import os
3
4from kern.agent import Agent
5from kern.models.openai import OpenAIResponses
6from kern.tools.hackernews import HackerNewsTools
7from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
8from opentelemetry.sdk.trace import TracerProvider
9from opentelemetry.sdk.trace.export import SimpleSpanProcessor
10from opentelemetry import trace
11
12# Set environment variables for Langfuse
13LANGFUSE_AUTH = base64.b64encode(
14 f"{os.getenv('LANGFUSE_PUBLIC_KEY')}:{os.getenv('LANGFUSE_SECRET_KEY')}".encode()
15).decode()
16os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://us.cloud.langfuse.com/api/public/otel"
17os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"
18
19# Configure the tracer provider
20trace_provider = TracerProvider()
21trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
22trace.set_tracer_provider(trace_provider)
23
24# Initialize OpenLIT instrumentation
25import openlit
26openlit.init(tracer=trace.get_tracer(__name__), disable_batch=True)
27
28# Create and configure the agent
29agent = Agent(
30 model=OpenAIResponses(id="gpt-5.2"),
31 tools=[HackerNewsTools()],
32 markdown=True,
33 debug_mode=True,
34)
35
36# Use the agent
37agent.print_response("What is currently trending on Twitter?")

Notes

  • Environment Variables: Ensure your environment variables are correctly set for the API keys and OTLP endpoint.
  • Data Regions: Adjust the OTEL_EXPORTER_OTLP_ENDPOINT for your data region or local deployment as needed. Available regions include:
    • https://us.cloud.langfuse.com/api/public/otel for the US region
    • https://eu.cloud.langfuse.com/api/public/otel for the EU region
    • http://localhost:3000/api/public/otel for local deployment

By following these steps, you can effectively integrate Kern with Langfuse, enabling comprehensive observability and monitoring of your AI agents.