ospalabs
Guide · Vercel AI SDK

Cost tracking for the Vercel AI SDK, per user and per feature

Updated October 2026

Every AI SDK call returns a usage object with token counts. To get dollars you still need prices per model, a place to store totals, and a way to group them by user and feature.

Pointing the SDK's OpenAI provider at Fuse does that: each request is priced and logged, grouped by the headers you send, and checked against your ceilings.

  1. 01

    Create a provider that points at Fuse

    Use createOpenAI with the Fuse base URL and a Fuse key. Model names stay the same, and other providers' models work too if you switch them on in Fuse.

    lib/ai.ts
    import { createOpenAI } from "@ai-sdk/openai";
    
    export const fuse = createOpenAI({
      baseURL: process.env.FUSE_BASE_URL,
      apiKey: process.env.FUSE_API_KEY,
    });
  2. 02

    Tag calls with user and feature

    Pass headers on each call. Use an opaque user id, not an email.

    route.ts
    const { text } = await generateText({
      model: fuse("gpt-4.1-mini"),
      prompt,
      headers: { "x-fuse-user": user.id, "x-fuse-tag": "summarize" },
    });
  3. 03

    See spend per user and feature

    The Customers and Features pages rank users and tags by spend, with requests and refusals. The usage object is still there in your code if you also want to log it yourself.

  4. 04

    Set limits

    Add a daily ceiling for the project, a cap per user, or a cap per feature tag. When one is reached, that call fails with 402 and the SDK throws an API error you can catch.

Questions

Does streaming still work?
Yes. streamText passes through chunk by chunk; the cost is settled from the usage reported at the end of the stream.
What if I use the Anthropic or Google provider packages?
Fuse speaks the OpenAI protocol, so use the OpenAI provider pointed at Fuse with Claude or Gemini model names. Fuse picks the stored key by model.