Vercel AI SDK cost tracking: one OpenAI-compatible provider for every model, with per-user budgets
Use the Vercel AI SDK with an OpenAI-compatible LLM gateway. One provider reaches GPT, Claude, Gemini and Bedrock; tag generateText and streamText calls by feature and user and cap spend before the call.
The AI SDK has a provider per vendor, each with its own key and its own bill. Pointing one OpenAI-compatible provider at the gateway gives you every model behind one key, with spend tracked and limited in one place.
Setup
generateText, streamText, generateObject
Switching a route from GPT to Claude is a one-string change, and the cost comparison is in the dashboard the same day.
Handling a budget stop in the UI
When a budget in Block mode is used up, the call fails with HTTP 429 and a JSON body whose error.type is budget_error (or run_budget_exceeded for a run budget). In streamText, handle it in onError and show a friendly message instead of a broken stream. A Degrade budget avoids the error entirely by switching to a cheaper model; the response carries x-cog-degraded: true.
@ai-sdk/openai works too
createOpenAI({ baseURL, apiKey }) from @ai-sdk/openai also works. Use openai.chat('<model>') for non-OpenAI models; the default model function targets the Responses API, which the gateway supports statelessly (send the full conversation, no hosted tools). See Responses API.
Related articles