> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agnost.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Pydantic AI

> Capture traces from Pydantic AI by pointing Logfire at Agnost

Pydantic AI uses Logfire for instrumentation. Point Logfire's OTLP exporter at Agnost and disable its hosted backend.

<Note>
  Pydantic AI/Logfire traces can include prompts, completions, tool call arguments, and full message history. Review [Data Governance](/data-governance) before enabling production traffic.
</Note>

## Install

```bash theme={null}
pip install pydantic-ai logfire
```

## Setup

```python theme={null}
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

* [Enable OpenTelemetry export](https://pydantic.dev/docs/ai/integrations/logfire/#otel-without-logfire)
* [Add custom metadata](https://pydantic.dev/docs/ai/integrations/logfire/#adding-custom-metadata)
