Skip to main content
AgentBasis supports Pydantic AI with built-in OpenTelemetry instrumentation.

Setup

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:
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:
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)
ParameterDefaultDescription
include_contentTrueLog prompts and completions
include_binary_contentFalseInclude images and other binary data

Per-Agent Instrumentation

If you need different settings for different agents, use get_instrumentation_settings():
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:
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:
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:
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:
FieldDescription
gen_ai.systemLLM provider (e.g., openai)
gen_ai.request.modelModel name
gen_ai.promptInput messages (if include_content=True)
gen_ai.completionResponse (if include_content=True)
user_idUser ID from context
session_idSession ID from context
durationRequest latency