How to · Vercel

Vercel AI Gateway Cost Tracking: From Provider Spend to Per User Margin

By Ghiles Asmani, founder of Weckr · Published August 7, 2026

Short version: the AI Gateway gives you one endpoint for many providers, with request observability and spend visibility per provider. It cannot tell you cost per end user or per plan, because your users do not exist at the gateway layer. That attribution happens in your app: capture the usage object on every call, tag it with userId, feature, and plan, and log the provider model id (not the prefixed gateway string) so it prices correctly.

This is the gateway sibling of Vercel AI SDK cost tracking: same goal, adjusted for what the gateway adds and the one gotcha it introduces.

What the gateway solves, and the layer it does not

With the gateway, a model is a string: anthropic/claude-sonnet-4-6, openai/gpt-5.4-mini. One credential, provider fallbacks, and a dashboard that shows what your traffic costs across providers, per request. For provider accounting, that is genuinely the easy mode, and if you route through it you should use its spend view.

The missing layer is the same one every provider console misses: attribution to your customers. “$140 on Anthropic this week” is a fact about your vendor bill. “u_1093 on the $29 plan cost $41 of that on the research feature” is a fact about your business, and only your app can produce it, because only your app knows which user made each call and what that user pays.

The per user pattern, gateway edition

The gateway changes the path to the provider, not the shape of the result: you still get a usage object per call. So the pattern from the AI SDK guide carries over, with one adjustment, strip the provider prefix before logging so your cost layer prices the real model:

const MODEL = 'anthropic/claude-sonnet-4-6';   // gateway string

const result = streamText({
  model: MODEL,
  messages,
  onFinish: async ({ usage }) => {
    await fetch('https://useweckr.com/api/v1/log', {
      method: 'POST',
      headers: {
        'content-type': 'application/json',
        'x-api-key': process.env.WECKR_API_KEY,
      },
      body: JSON.stringify({
        userId: session.user.id,
        feature: 'chat',
        model: MODEL.split('/')[1],      // 'claude-sonnet-4-6', priceable
        provider: MODEL.split('/')[0],   // 'anthropic'
        inputTokens: usage.inputTokens,
        outputTokens: usage.outputTokens,
        planName: user.plan,
        planRevenueUsd: PLAN_PRICES[user.plan],
      }),
    });
  },
});

Weckr recomputes cost server side from current provider rates, so a client cannot spoof it and a price change cannot silently break it, the same table behind the LLM pricing JSON feed.

Two gateway specific gotchas

  • Log the served model, not the requested one. Gateway fallbacks are the point of a gateway: when your primary is overloaded, traffic reroutes, sometimes to a pricier model. If your logs record the requested model, incident traffic gets priced wrong and the fallback bill disappears into the average. The economics of that are in LLM fallback routing.
  • Unknown model ids price as zero. If the prefixed string reaches your cost layer, it matches nothing in the price table, logs at zero cost, and everything downstream (caps, margins, alerts) quietly under counts. Zero cost rows in your tracking are a smell worth alerting on.

Where each number should live

  • Gateway dashboard: provider spend, request inspection, fallback behavior. Vendor accounting.
  • Your per user layer: cost per user, per feature, margin against plan, per user caps. Customer accounting, the method from tracking AI costs per user.
  • Monthly: the two should reconcile within a few percent. A gap means unattributed traffic: background jobs, retries, or a path missing the onFinish log.

FAQ

What is the Vercel AI Gateway?

A unified endpoint in front of many model providers. You call one API with a provider/model string like anthropic/claude-sonnet-4-6, and the gateway handles provider credentials, routing, fallbacks, and gives you request level observability and spend visibility in the Vercel dashboard. With the AI SDK you use it by passing the model string instead of wiring a provider package.

Does the AI Gateway track my costs?

At the account and request level, yes: you can see what your gateway usage costs across providers and inspect individual requests. What it does not model is your business: which of your end users caused the spend, on which feature, against which subscription plan. Gateway observability is provider accounting, not customer accounting.

How do I get cost per user when using the AI Gateway?

Attribute at call time in your app, where the user id lives. With the AI SDK, read the usage object in onFinish or from the generateText result and forward it with your userId, feature, and plan to your tracking layer. The gateway changes how the call reaches the provider, not what usage data comes back, so the same per user pattern works unchanged.

Do gateway fallbacks change my costs?

They can, silently. A fallback chain that reroutes from a cheap model to an available pricier one keeps your feature up while multiplying its cost per call for the duration. If you track per user cost, log the model that actually served each call, not the one you requested, so incident traffic is visible instead of averaged away.

Which model string do I log for pricing, the gateway one or the provider one?

The provider model id. Gateway strings carry a provider prefix, for example anthropic/claude-sonnet-4-6. Strip the prefix and log claude-sonnet-4-6 so your cost layer can price it against provider rates. Logging the prefixed string usually prices as unknown, which shows up as zero cost and quietly breaks caps and margins.

Keep reading

One endpoint in, one margin view out

Route through the gateway for the provider conveniences, forward usage per user for the business view. Weckr holds the second half: cost and margin per user and per feature, caps included, from one POST per call. See the result on the live demo, or start from the AI cost and margin guide.

See the dashboard with real data, no signup needed.

Try the demo →