Observability (OpenTelemetry tracing)

pydantic-ai ships native OpenTelemetry instrumentation that emits GenAI spans for each agent run, model call, and tool call, following the OpenTelemetry GenAI semantic conventions. When enabled, this provider turns that instrumentation on for every agent it builds and routes the spans through the OpenTelemetry exporter Airflow already uses, so they appear in whatever backend your deployment runs (Jaeger, Tempo, Grafana, Phoenix, Langfuse, an OTLP collector, …), correlated to the task that produced them.

This covers all of the LLM operators (AgentOperator, @task.agent / @task.llm and the SQL / branch / file-analysis / schema-compare operators), because they all build their agent through create_agent().

How it works

  • No extra infrastructure. The provider does not configure an exporter or a TracerProvider of its own. It reuses the global provider that Airflow’s core tracing installs, so the spans share the exporter and endpoint already configured under [traces] / the standard OTEL_EXPORTER_OTLP_* environment variables. If core tracing is not enabled in the worker process, no GenAI spans are emitted.

  • Correlation. The worker opens a task span before the operator runs, so the agent’s spans nest under it and share its trace_id. AgentOperator (and @task.agent) additionally stamps the task-instance identity on every GenAI span it emits: the five keys core tracing already puts on the task span (airflow.dag_id, airflow.task_id, airflow.dag_run.run_id, airflow.task_instance.try_number, airflow.task_instance.map_index) plus airflow.task_instance.id as the per-attempt run join key. So a span is filterable by dag, task, run, attempt, or map index directly, without walking up to the parent span (OpenTelemetry children inherit trace context, not attributes). An automatic retry reuses the task instance’s persisted trace context, so all attempts share one trace and appear as repeated task-run spans on it, distinguished by try number. Only a manual clear or rerun regenerates the context and starts a new trace.

  • Run join key. For an AgentOperator run, the task-instance id (unique per attempt, since Airflow regenerates it on each retry) is passed to pydantic-ai as the run’s run_id. It surfaces on the run’s GenAI spans as gen_ai.agent.call.id, and the operator also exposes it, alongside the run’s token usage, on XCom under the run_id and usage keys. A downstream task can then reference the run (ti.xcom_pull(task_ids="my_agent", key="run_id")) and a trace backend can join a task’s output to its agent trace without parsing logs. With enable_hitl_review the run_id and usage reflect the initial model run, not the human-feedback regenerations.

  • Scope. The airflow.* identity attributes and the run_id / usage XComs come only from AgentOperator and @task.agent. The other LLM operators still emit GenAI spans correlated to the task span by nesting, but without the identity attributes or the run join key.

  • Content is off by default. Only token counts, model id, latency, tool names, and finish reason are recorded. Prompt and completion text is never emitted unless you opt in (see below).

  • Cost is already on the span. pydantic-ai’s own instrumentation sets a best-effort operation.cost attribute on the model-call span whenever it can price the response – no provider configuration is needed for this.

Note

The agent-run span reports token usage under gen_ai.aggregated_usage.* while the per-model-call span keeps gen_ai.usage.*. This avoids double-counting in backends that sum a parent span and its children. Dashboards or alerts that read run-level token usage from gen_ai.usage.* should switch to gen_ai.aggregated_usage.*.

Enabling it

Enable core tracing and turn on the provider option:

[traces]
otel_on = True

[common.ai]
otel_export_enabled = True

Configure the exporter destination with the standard OpenTelemetry environment variables, for example:

# Core tracing defaults the exporter to OTLP/gRPC. For an OTLP/HTTP
# endpoint (port 4318, ``/v1/traces`` path) also select the HTTP exporter:
export OTEL_TRACES_EXPORTER="otlp_proto_http"
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://otel-collector:4318/v1/traces"

Capturing prompt and completion content

By default the spans carry no message text. To also record model inputs and outputs (gen_ai.input.messages / gen_ai.output.messages), set:

[common.ai]
capture_content = True

Warning

With capture_content enabled, prompts, completions, and tool IO are exported to your tracing backend without redaction. Airflow’s secret masking applies to logs and rendered template fields, not to OpenTelemetry span attributes, so it does not scrub this content. Enable it only for debugging in a trusted environment. It has no effect unless otel_export_enabled is True.

See Configuration Reference for the full list of options.

Was this entry helpful?