Skip to Content

How do I see token counts for each call to the Vercel AI SDK?

Rius records input and output token counts on the span for each Vercel AI SDK model call, and shows them in the trace waterfall and the span detail. On ai 7, init() registers the telemetry for you. On ai 5 and 6, each call sets experimental_telemetry: { isEnabled: true }. Open the trace in the console and read the tokens on the model call span, nested under an agent span for the call.

How it works

The Vercel AI SDK integration is TypeScript only. It reads the AI SDK’s OpenTelemetry telemetry and turns it into spans. How that telemetry starts depends on your ai version:

  • ai 7: the AI SDK no longer emits spans itself. They come from @ai-sdk/otel, which init() registers once the package is installed. This needs Node 22 or newer.
  • ai 5 and 6: the AI SDK has OpenTelemetry built in but emits nothing until a call opts in with experimental_telemetry: { isEnabled: true }. Nothing extra is installed, and Node 18 or newer works.

For each generateText, streamText, generateObject or streamObject call, Rius stores:

  • An agent span for the call.
  • A chat span for each model call, with its messages, model, finish reason and token usage. Usage includes cache reads, cache writes when the AI SDK reports them, and reasoning tokens.
  • An execute_tool span for each tool execution, with the tool’s arguments and result.
  • Embedding spans.

A streamed call also carries time to first token. Cost is computed by Rius from the model and the token counts, so you never send a cost value.

Each model call is counted once in token and cost totals. The wrapper span around the calls, such as the agent span, still shows the run’s total where the AI SDK reports one. This needs @glassflow-ai/rius 1.2.0, or 1.1.0 on ai 7. The counting happens in the Rius SDK, so @ai-sdk/otel spans sent without it can be counted twice.

Set it up

  1. Install and initialize Rius. The Integrations page covers the shared setup in full and lists every supported library.

    npm install @glassflow-ai/rius @opentelemetry/api
  2. On ai 7, install the telemetry package. Rius registers it for you.

    npm install @ai-sdk/otel
  3. Call init(), wait for ready, then make your call. With ready awaited, the integration is registered before the first call, and shutdown() exports queued spans before a short script exits:

    import { init } from '@glassflow-ai/rius' import { openai } from '@ai-sdk/openai' import { generateText } from 'ai' const rius = init({ apiKey: 'ri_...', serviceName: 'my-agent' }) await rius.ready // the AI SDK integration is registered before the first call const { text } = await generateText({ model: openai('gpt-4o-mini'), prompt: 'Summarize this ticket', }) await rius.shutdown() // export queued spans before the process exits
  4. On ai 5 and 6, turn telemetry on for every call you want traced:

    const { text } = await generateText({ model: openai('gpt-4o-mini'), prompt: 'Summarize this ticket', experimental_telemetry: { isEnabled: true }, })
  5. Optional: wrap the call in observe if you want it to appear inside your agent’s run instead of as a standalone trace. You do not need your own span to get token counts, because the model call span carries them.

  6. Check that it attached. On ai 7, await rius.ready lists vercel-ai. On ai 5 and 6 it does not list vercel-ai even when tracing works, so look for spans in the console. If a call shows no spans on ai 5 or 6, the usual cause is a call missing experimental_telemetry: { isEnabled: true }. On ai 7, Rius warns at init() when it finds ai 7 without @ai-sdk/otel.

What you get

Open the trace from the Traces page. Exploring traces walks through each view. Token counts appear in several places:

  • Trace list: each row shows the token total for the trace. The strip above the list sums tokens for the traces that match your filters.
  • Waterfall: model call spans show their token counts inline in the tree, and the header shows total tokens and cost for the whole trace.
  • Span detail: select the chat span. Its Model section shows provider, requested and response model, input and output tokens, and cost.
  • Spans tab: every span has a Tokens column you can sort. Filter by kind with the All kinds menu, choosing LLM, to list model calls only.
  • Context growth: for a trace with several model calls, a strip above the tree plots input tokens per call in order, so a growing prompt shows early. Click a bar to select that call.

If a call reports no token usage, its cost is blank rather than $0, so it does not look free. Costs are list-price estimates in US dollars. The same views are described in Monitor AI agents in production.

The prompt and completion appear on the Input / Output tab unless your privacy controls strip content. Timings, tokens and cost still show either way.

When something else fits

  • On ai 5 and 6, the AI SDK’s image, speech and transcription calls are not traced because they take no telemetry settings. On ai 7 they are traced, so upgrade if you need token counts there.
  • For a call made outside the Vercel AI SDK, such as the OpenAI or Anthropic client directly, use that library’s page from Integrations. Python has no Vercel AI SDK integration, so trace those calls with manual generations.

Still stuck? Get help

Last updated on