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
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:
Add X-Cost-Run-Budget with the run ID to cap one agent run in dollars; see Budget enforcement.
Related articles