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
-
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 -
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.
-
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 base642import os34from kern.agent import Agent5from kern.models.openai import OpenAIResponses6from kern.tools.yfinance import YFinanceTools7from openinference.instrumentation.kern import AgnoInstrumentor8from opentelemetry import trace as trace_api9from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter10from opentelemetry.sdk.trace import TracerProvider11from opentelemetry.sdk.trace.export import SimpleSpanProcessor1213# Set environment variables for Langfuse14LANGFUSE_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}"1920# Configure the tracer provider21tracer_provider = TracerProvider()22tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))23trace_api.set_tracer_provider(tracer_provider=tracer_provider)2425# Start instrumenting kern26AgnoInstrumentor().instrument()2728# Create and configure the agent29agent = 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)3637# Use the agent38agent.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 base642import os34from kern.agent import Agent5from kern.models.openai import OpenAIResponses6from kern.tools.hackernews import HackerNewsTools7from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter8from opentelemetry.sdk.trace import TracerProvider9from opentelemetry.sdk.trace.export import SimpleSpanProcessor10from opentelemetry import trace1112# Set environment variables for Langfuse13LANGFUSE_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}"1819# Configure the tracer provider20trace_provider = TracerProvider()21trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))22trace.set_tracer_provider(trace_provider)2324# Initialize OpenLIT instrumentation25import openlit26openlit.init(tracer=trace.get_tracer(__name__), disable_batch=True)2728# Create and configure the agent29agent = Agent(30 model=OpenAIResponses(id="gpt-5.2"),31 tools=[HackerNewsTools()],32 markdown=True,33 debug_mode=True,34)3536# Use the agent37agent.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_ENDPOINTfor your data region or local deployment as needed. Available regions include:https://us.cloud.langfuse.com/api/public/otelfor the US regionhttps://eu.cloud.langfuse.com/api/public/otelfor the EU regionhttp://localhost:3000/api/public/otelfor local deployment
By following these steps, you can effectively integrate Kern with Langfuse, enabling comprehensive observability and monitoring of your AI agents.