- 01
Keep a cost_limit on the run
Set UsageLimits(cost_limit=...) to the most one run should cost. When the accumulated cost crosses it, the next step raises UsageLimitExceeded. The crossing request itself has already completed.
- 02
Point the provider at Fuse
OpenAIProvider with a base URL is the client to use for a transparent proxy in front of OpenAI. The model id has to be enabled on the project. Chat completions are the safe path: Fuse refuses image inputs and previous_response_id.
agent.pyimport os from pydantic_ai.models.openai import OpenAIChatModel from pydantic_ai.providers.openai import OpenAIProvider model = OpenAIChatModel( "gpt-4.1-mini", provider=OpenAIProvider( base_url="https://fuse.ospalabs.com/v1", api_key=os.environ["FUSE_API_KEY"], ), ) - 03
Set a project ceiling for every run
A daily or monthly ceiling covers all runs that share the key, including ones your UsageLimits do not wrap. A 402 surfaces as an API error from the provider call. Do not retry it.
- 04
Reconcile the two numbers
result usage is Pydantic AI's sum, priced with the genai-prices snapshot you have installed. Fuse settles from the provider's reported tokens and its own price table. They should be close. The ceiling uses Fuse's number, because that is the one checked before the call.
Pydantic AI cost tracking that can also stop a run
Updated November 2026
A Pydantic AI run accumulates usage: input and output tokens, cache reads, reasoning tokens, and a best-effort USD cost from genai-prices when the model is known. UsageLimits can set a cost_limit, a request_limit, and token limits. Token and cost checks run against usage the model already returned. The request that crosses cost_limit has been billed. count_tokens_before_request can pre-check input size on some providers, and it is off by default. It is a token check, not a dollar reservation.
Keep UsageLimits. They bound a run inside the process. For a ceiling that holds across runs, processes, and deploys, point the OpenAI provider at Fuse. The call is priced before it is sent.
Questions
Does cost_limit block the request that exceeds it?
It blocks the following step. The request whose usage crossed the limit has already been sent and billed. A Fuse ceiling refuses a request whose worst case would cross, before the provider sees it.
What if genai-prices has no row for the model?
Usage.cost stays empty, so a cost_limit cannot fire on that run. Fuse refuses an unpriced model with model_not_priced instead of forwarding it.