Logfire
Integrate Kern with Logfire to send traces and gain insights into your agent's performance.
Integrating Kern with Logfire
Logfire is Pydantic's observability platform that provides comprehensive tracing and monitoring for AI applications. By integrating Kern with Logfire, you can utilize OpenInference 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 opentelemetry-sdk opentelemetry-exporter-otlp openinference-instrumentation-kern-ai -
Setup Logfire Account
- Sign up for an account at Logfire.
- Obtain your write token from the Logfire dashboard.
-
Set Environment Variables
Configure your environment with the Logfire write token:
1export LOGFIRE_WRITE_TOKEN=<your-write-token>
Sending Traces to Logfire
This example demonstrates how to instrument your Kern agent with OpenInference and send traces to Logfire.
1import os23from kern.agent import Agent4from kern.models.openai import OpenAIResponses5from kern.tools.hackernews import HackerNewsTools6from openinference.instrumentation.kern import AgnoInstrumentor7from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter8from opentelemetry.sdk.trace import TracerProvider9from opentelemetry.sdk.trace.export import SimpleSpanProcessor1011# Set environment variables for Logfire12os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://logfire-eu.pydantic.dev" # EU data region13os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization={os.getenv('LOGFIRE_WRITE_TOKEN')}"1415# Configure the tracer provider16tracer_provider = TracerProvider()17tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))1819# Start instrumenting kern20AgnoInstrumentor().instrument(tracer_provider=tracer_provider)2122# Create and configure the agent23agent = Agent(24 name="Stock Price Agent",25 model=OpenAIResponses(id="gpt-5.2"),26 tools=[HackerNewsTools()],27 instructions="You are a stock price agent. Answer questions in the style of a stock analyst.",28 debug_mode=True,29)3031# Use the agent32agent.print_response("What is the current price of Tesla?")Notes
- Environment Variables: Ensure your environment variables are correctly set for the write token and OTLP endpoint.
- Data Regions: Adjust the
OTEL_EXPORTER_OTLP_ENDPOINTfor your data region. Available regions include:https://logfire-us.pydantic.devfor the US regionhttps://logfire-eu.pydantic.devfor the EU region
- Logfire Dashboard: View your traces and monitor performance at Logfire Dashboard
By following these steps, you can effectively integrate Kern with Logfire, enabling comprehensive observability and monitoring of your AI agents.