Skip to main content
Pydantic AI uses Logfire for instrumentation. Point Logfire’s OTLP exporter at Agnost and disable its hosted backend.
Pydantic AI/Logfire traces can include prompts, completions, tool call arguments, and full message history. Review Data Governance before enabling production traffic.

Install

pip install pydantic-ai logfire

Setup

import os, logfire
from pydantic_ai import Agent
from opentelemetry import trace

os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://otel.agnost.ai"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = "X-Agnost-Org-ID=<your-org-id>"

logfire.configure(send_to_logfire=False)  # required: don't dual-send to Logfire
logfire.instrument_pydantic_ai()

agent = Agent("anthropic:claude-sonnet-4-5", system_prompt="...")

tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("chat-turn") as span:
    span.set_attribute("gen_ai.conversation.id", "sess-123")
    span.set_attribute("user.id", "u-42")
    agent.run_sync("Hello")
Spans use OTel GenAI semconv (gen_ai.input.messages, gen_ai.output.messages, gen_ai.tool.call.arguments, gen_ai.usage.*) plus pydantic_ai.all_messages. gen_ai.conversation.id is emitted natively when message history is provided to the agent.

Caveats

Do not set LOGFIRE_TOKEN: that re-enables the hosted backend.

Verify

Run one agent.run_sync call, then open Raw logs in Agnost. Confirm gen_ai.* and pydantic_ai.* attributes are present.

Troubleshooting

  • Keep send_to_logfire=False if you do not want dual-send to Logfire.
  • Do not set LOGFIRE_TOKEN unless you explicitly want hosted Logfire enabled.
  • Confirm OTEL_EXPORTER_OTLP_HEADERS contains X-Agnost-Org-ID=<your-org-id>.

References