Frameworks

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

npm install ai @ai-sdk/openai-compatible
// lib/ai.ts
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
 
export const cognocient = createOpenAICompatible({
  name: 'cognocient',
  baseURL: 'https://api.cognocient.com/v1',
  apiKey: process.env.COGNOCIENT_API_KEY,       // sk-cog-...
  headers: { 'X-Cost-Feature': 'web-chat' },
  includeUsage: true,                           // token usage on streamed responses
});

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.

On this page