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:
ai7: the AI SDK no longer emits spans itself. They come from@ai-sdk/otel, whichinit()registers once the package is installed. This needs Node 22 or newer.ai5 and 6: the AI SDK has OpenTelemetry built in but emits nothing until a call opts in withexperimental_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
chatspan 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_toolspan 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
-
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 -
On
ai7, install the telemetry package. Rius registers it for you.npm install @ai-sdk/otel -
Call
init(), wait forready, then make your call. Withreadyawaited, the integration is registered before the first call, andshutdown()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 -
On
ai5 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 }, }) -
Optional: wrap the call in
observeif 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. -
Check that it attached. On
ai7,await rius.readylistsvercel-ai. Onai5 and 6 it does not listvercel-aieven when tracing works, so look for spans in the console. If a call shows no spans onai5 or 6, the usual cause is a call missingexperimental_telemetry: { isEnabled: true }. Onai7, Rius warns atinit()when it findsai7 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
chatspan. 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
ai5 and 6, the AI SDK’s image, speech and transcription calls are not traced because they take no telemetry settings. Onai7 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.