Frameworks

Pydantic AI cost tracking: route Pydantic AI agents through an OpenAI-compatible gateway

Point Pydantic AI's OpenAI model at an LLM gateway to use any provider, tag agent runs by feature and user, and cap spend with budgets enforced before each call.

Pydantic AI's OpenAI model class accepts your own AsyncOpenAI client, which is where the gateway goes in.

Setup

# pip install pydantic-ai-slim[openai]
from openai import AsyncOpenAI
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider
 
client = AsyncOpenAI(
    api_key="sk-cog-YOUR-KEY",
    base_url="https://api.cognocient.com/v1",
    default_headers={"X-Cost-Feature": "invoice-parser"},
)
model = OpenAIChatModel("claude-sonnet-4-6", provider=OpenAIProvider(openai_client=client))
 
class Invoice(BaseModel):
    vendor: str
    total: float
 
agent = Agent(model, output_type=Invoice, system_prompt="Extract invoice fields.")
result = agent.run_sync("ACME Corp invoice, total due $1,240.50")
print(result.output)        # Invoice(vendor='ACME Corp', total=1240.5)

Older Pydantic AI versions call the class OpenAIModel; the arguments are the same.

Per-request tags

Use one client per tenant or request when tags change per call (the client is light), or set extra_headers in the model settings:

result = agent.run_sync(
    text,
    model_settings={"extra_headers": {"X-Cost-User": f"user_{user_id}", "X-Cost-Run-ID": run_id}},
)

Add X-Cost-Run-Budget with the run ID to cap one agent run in dollars; see Budget enforcement.

On this page