> ## Documentation Index
> Fetch the complete documentation index at: https://puzzlet-9ba7bb98.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Pydantic AI

> Trace Pydantic AI agents in AgentMark using the OpenInference span processor.

[Pydantic AI](https://github.com/pydantic/pydantic-ai) emits OpenTelemetry spans natively. The OpenInference `Pydantic AI` package adds a span processor that maps those spans onto the OpenInference conventions before they're exported, so AgentMark reads model calls, tool calls, and agent runs as normalized traces. Pydantic AI is a Python framework.

## Setup

<Steps>
  <Step title="Install the span processor and the OTLP exporter">
    ```bash theme={null}
    pip install openinference-instrumentation-pydantic-ai pydantic-ai \
      opentelemetry-sdk opentelemetry-exporter-otlp-proto-http
    ```
  </Step>

  <Step title="Point the exporter at AgentMark and enable instrumentation">
    Add the `OpenInferenceSpanProcessor` ahead of the OTLP exporter, and enable instrumentation on each agent with `InstrumentationSettings`. Use your AgentMark API key and app id from project settings.

    ```python theme={null}
    from pydantic_ai import Agent
    from pydantic_ai.models.instrumented import InstrumentationSettings
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import BatchSpanProcessor
    from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
    from openinference.instrumentation.pydantic_ai import OpenInferenceSpanProcessor

    provider = TracerProvider()
    # Maps Pydantic AI's native spans onto the OpenInference conventions.
    provider.add_span_processor(OpenInferenceSpanProcessor())
    provider.add_span_processor(
        BatchSpanProcessor(
            OTLPSpanExporter(
                endpoint="https://api.agentmark.co/v1/traces",
                headers={
                    "Authorization": "<YOUR_API_KEY>",  # raw key, no "Bearer" prefix
                    "X-Agentmark-App-Id": "<YOUR_APP_ID>",
                },
            )
        )
    )

    instrumentation = InstrumentationSettings(version=2, tracer_provider=provider)
    agent = Agent("openai:gpt-4o", instrument=instrumentation)
    ```
  </Step>

  <Step title="Run your agent">
    Run your agent as usual. Each model call, tool call, and agent run arrives in AgentMark as a span, grouped into a trace. See [Traces and logs](/observe/traces-and-logs).
  </Step>
</Steps>

## What AgentMark captures

After the span processor maps them, Pydantic AI spans use the OpenInference attribute conventions: model, token usage, input and output messages, tool calls, settings, and span kind are all mapped onto AgentMark's normalized trace fields, and token counts feed [cost tracking](/observe/cost-and-token-tracking). See [OpenInference](/integrations/tracing/openinference#what-agentmark-captures) for the full attribute mapping.

## Next steps

<CardGroup cols={2}>
  <Card title="OpenInference" icon="diagram-project" href="/integrations/tracing/openinference">
    How AgentMark reads OpenInference attributes
  </Card>

  <Card title="Traces and logs" icon="list-tree" href="/observe/traces-and-logs">
    Explore traces once they arrive
  </Card>
</CardGroup>

<div className="mt-8 rounded-lg bg-blue-50 p-6 dark:bg-blue-900/30">
  <h3 className="font-semibold mb-3">Have questions?</h3>
  <p className="mb-4">Reach out any time:</p>

  <ul>
    <li>
      Email the team at <a href="mailto:hello@agentmark.co" className="text-blue-600 hover:text-blue-800 dark:text-blue-400 dark:hover:text-blue-200">[hello@agentmark.co](mailto:hello@agentmark.co)</a> for support
    </li>

    <li>
      Schedule an <a href="https://cal.com/ryan-randall/enterprise" className="text-blue-600 hover:text-blue-800 dark:text-blue-400 dark:hover:text-blue-200">Enterprise Demo</a> to learn about AgentMark's business solutions
    </li>
  </ul>
</div>
