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

  1. Install Dependencies

    Ensure you have the necessary packages installed:

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

    • Sign up for an account at Logfire.
    • Obtain your write token from the Logfire dashboard.
  3. 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 os
2
3from kern.agent import Agent
4from kern.models.openai import OpenAIResponses
5from kern.tools.hackernews import HackerNewsTools
6from openinference.instrumentation.kern import AgnoInstrumentor
7from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
8from opentelemetry.sdk.trace import TracerProvider
9from opentelemetry.sdk.trace.export import SimpleSpanProcessor
10
11# Set environment variables for Logfire
12os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://logfire-eu.pydantic.dev" # EU data region
13os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization={os.getenv('LOGFIRE_WRITE_TOKEN')}"
14
15# Configure the tracer provider
16tracer_provider = TracerProvider()
17tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
18
19# Start instrumenting kern
20AgnoInstrumentor().instrument(tracer_provider=tracer_provider)
21
22# Create and configure the agent
23agent = 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)
30
31# Use the agent
32agent.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_ENDPOINT for your data region. Available regions include:
    • https://logfire-us.pydantic.dev for the US region
    • https://logfire-eu.pydantic.dev for 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.