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

# Pydantic AI Integration

> Monitor Pydantic AI agents

AgentBasis supports Pydantic AI with built-in OpenTelemetry instrumentation.

## Setup

```python theme={null}
import agentbasis
from agentbasis.frameworks.pydanticai import instrument

# Initialize AgentBasis first
agentbasis.init(api_key="your-api-key", agent_id="your-agent-id")

# Enable global instrumentation for all Pydantic AI agents
instrument()
```

## Basic Usage

Once instrumented, all Pydantic AI agents are automatically traced:

```python theme={null}
from pydantic_ai import Agent

agent = Agent("openai:gpt-4")
result = agent.run_sync("Hello!")

print(result.data)
```

## Privacy Controls

Control what data is captured in traces:

```python theme={null}
from agentbasis.frameworks.pydanticai import instrument

# Don't log prompts and completions (for sensitive data)
instrument(include_content=False)

# Include binary content like images (increases trace size)
instrument(include_content=True, include_binary_content=True)
```

| Parameter                | Default | Description                          |
| ------------------------ | ------- | ------------------------------------ |
| `include_content`        | `True`  | Log prompts and completions          |
| `include_binary_content` | `False` | Include images and other binary data |

## Per-Agent Instrumentation

If you need different settings for different agents, use `get_instrumentation_settings()`:

```python theme={null}
from agentbasis.frameworks.pydanticai import get_instrumentation_settings
from pydantic_ai import Agent

# Agent with full tracing
agent_full = Agent(
    "openai:gpt-4",
    instrument=get_instrumentation_settings()
)

# Agent without content logging (privacy mode)
agent_private = Agent(
    "openai:gpt-4",
    instrument=get_instrumentation_settings(include_content=False)
)
```

## User Context Integration

### Using create\_traced\_agent

The easiest way to create an agent with context support:

```python theme={null}
from agentbasis.frameworks.pydanticai import create_traced_agent
import agentbasis

# Create an agent pre-configured with tracing and context
agent = create_traced_agent(
    "openai:gpt-4",
    system_prompt="You are a helpful assistant."
)

# Set user context - automatically included in traces
agentbasis.set_user("user-123")
agentbasis.set_session("session-456")

result = agent.run_sync("Hello!")
```

### Using get\_metadata\_callback

For more control, use the metadata callback directly:

```python theme={null}
from agentbasis.frameworks.pydanticai import get_metadata_callback
from pydantic_ai import Agent
import agentbasis

agent = Agent(
    "openai:gpt-4",
    metadata=get_metadata_callback()
)

# Set context before running
agentbasis.set_user("user-123")

# This run will include user_id in the trace metadata
result = agent.run_sync("Hello!")
```

## Async Usage

All methods work with async agents:

```python theme={null}
from pydantic_ai import Agent
import asyncio

agent = Agent("openai:gpt-4")

async def main():
    result = await agent.run("Hello!")
    print(result.data)

asyncio.run(main())
```

## Captured Data

The integration automatically records:

| Field                  | Description                                |
| ---------------------- | ------------------------------------------ |
| `gen_ai.system`        | LLM provider (e.g., `openai`)              |
| `gen_ai.request.model` | Model name                                 |
| `gen_ai.prompt`        | Input messages (if `include_content=True`) |
| `gen_ai.completion`    | Response (if `include_content=True`)       |
| `user_id`              | User ID from context                       |
| `session_id`           | Session ID from context                    |
| `duration`             | Request latency                            |
