Skip to content

Send Pydantic AI Traces to KloudMate

Pydantic AI has OpenTelemetry tracing built in, and its spans already follow the GenAI semantic conventions. Point an OTLP exporter at KloudMate and turn instrumentation on to see every agent run in AI Observability. You don’t need a Logfire account.

  • A KloudMate API key, from Settings → Ingest Keys.
  • Pydantic AI 1.89 or later.
pip install pydantic-ai opentelemetry-sdk opentelemetry-exporter-otlp-proto-http

Set up a tracer provider with an OTLP exporter, then instrument every agent:

from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.trace import set_tracer_provider
from pydantic_ai import Agent

tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
set_tracer_provider(tracer_provider)

Agent.instrument_all()

Call tracer_provider.shutdown() before a script exits, so the last spans are sent.

OTLPSpanExporter() reads the standard OpenTelemetry variables. Set them in the environment that runs your app:

export OTEL_SERVICE_NAME="support-agent"
export OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otel.kloudmate.com:4318"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=YOUR_KLOUDMATE_API_KEY"

KloudMate expects the raw API key in the Authorization header, with no Bearer prefix. If the KloudMate agent runs on the same machine, set the endpoint to http://localhost:4318 and leave out the header.

Pydantic AI sends a conversation ID automatically. A new run starts a new conversation, and a run that passes message_history from an earlier run continues it. To use your own chat or thread ID instead, pass conversation_id:

result = await agent.run(question, conversation_id="chat-42")

Pydantic AI records prompts, responses, and tool arguments by default. To leave them out, pass InstrumentationSettings:

from pydantic_ai import Agent, InstrumentationSettings

Agent.instrument_all(InstrumentationSettings(include_content=False))

Token counts, models, and latency are still recorded. Pydantic AI also reports its own cost estimate on each call it can price, and KloudMate keeps that value instead of calculating one.

Run the agent once, then open AI Observability → Traces. The run appears as a turn named after the agent, with its model, steps, tokens, and estimated cost.