> ## Documentation Index
> Fetch the complete documentation index at: https://traceroot.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel AI SDK

> Trace Vercel AI SDK agents — no instrumentModules config required

Automatically capture agent runs, LLM calls, tool executions, and token usage from the [Vercel AI SDK](https://ai-sdk.dev) through its built-in OpenTelemetry support — no `instrumentModules` config required.

## Install

> [tab] AI SDK 7
>
> ```bash theme={null}
> npm install @traceroot-ai/traceroot ai@^7 @ai-sdk/otel@^1 @ai-sdk/openai@^4 zod@^4
> ```

> [tab] AI SDK 6 (legacy)
>
> ```bash theme={null}
> npm install @traceroot-ai/traceroot ai@^6 @ai-sdk/openai@^3 zod@^4
> ```

## Set up

> [tab] AI SDK 7
>
> Call `TraceRoot.initialize()` and register the AI SDK's OpenTelemetry integration before your first model call:
>
> ```typescript theme={null}
> import { TraceRoot } from '@traceroot-ai/traceroot';
> import { registerTelemetry } from 'ai';
> import { OpenTelemetry } from '@ai-sdk/otel';
>
> TraceRoot.initialize();
>
> // Required on AI SDK 7 — without it, no spans are emitted.
> registerTelemetry(new OpenTelemetry({ usage: true }));
> ```

> [tab] AI SDK 6 (legacy)
>
> Call `TraceRoot.initialize()` before your first model call:
>
> ```typescript theme={null}
> import { TraceRoot } from '@traceroot-ai/traceroot';
>
> TraceRoot.initialize();
> ```
>
> Then set `experimental_telemetry: { isEnabled: true }` on each call (see [Usage](#usage)).

## Usage

Once initialized, every `generateText` / `streamText` call, model call, and tool execution is captured automatically:

> [tab] AI SDK 7
>
> ```typescript theme={null}
> import { TraceRoot } from '@traceroot-ai/traceroot';
> import { registerTelemetry, generateText, tool, isStepCount } from 'ai';
> import { OpenTelemetry } from '@ai-sdk/otel';
> import { openai } from '@ai-sdk/openai';
> import { z } from 'zod';
>
> TraceRoot.initialize();
> registerTelemetry(new OpenTelemetry({ usage: true }));
>
> // The agent run, both model calls, and the getWeather tool call are all traced.
> const result = await generateText({
>   model: openai('gpt-4o-mini'),
>   instructions: 'You are a concise weather assistant.',
>   prompt: 'What is the weather in Tokyo?',
>   stopWhen: isStepCount(5),
>   telemetry: { functionId: 'weather_agent' },
>   tools: {
>     getWeather: tool({
>       description: 'Get current weather for a city',
>       inputSchema: z.object({ city: z.string() }),
>       execute: async ({ city }) => ({ city, temp: 72, condition: 'sunny' }),
>     }),
>   },
> });
>
> console.log(result.text);
> await TraceRoot.flush();
> ```

> [tab] AI SDK 6 (legacy)
>
> ```typescript theme={null}
> import { TraceRoot } from '@traceroot-ai/traceroot';
> import { generateText, tool, stepCountIs } from 'ai';
> import { openai } from '@ai-sdk/openai';
> import { z } from 'zod';
>
> TraceRoot.initialize();
>
> // The agent run, both model calls, and the getWeather tool call are all traced.
> const result = await generateText({
>   model: openai('gpt-4o-mini'),
>   system: 'You are a concise weather assistant.',
>   prompt: 'What is the weather in Tokyo?',
>   stopWhen: stepCountIs(5),
>   // Required on every call in AI SDK 6.
>   experimental_telemetry: { isEnabled: true, functionId: 'weather_agent' },
>   tools: {
>     getWeather: tool({
>       description: 'Get current weather for a city',
>       inputSchema: z.object({ city: z.string() }),
>       execute: async ({ city }) => ({ city, temp: 72, condition: 'sunny' }),
>     }),
>   },
> });
>
> console.log(result.text);
> await TraceRoot.flush();
> ```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `ai.generateText`. `functionId` sets a telemetry attribute on the span; it does not rename it.
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Model | The model passed to `generateText` / `streamText` / `ToolLoopAgent` |
| Messages | Input prompt and message history |
| Response | Generated text output |
| Tool calls | Each tool invocation with input and output |
| Tokens | Input and output token counts |
| Cost | Calculated when the model ID matches traceroot's pricing table |
| Latency | Request duration per span |
| Multi-step iterations | Each step of a tool loop captured as a child span |

## Limitations

* AI SDK 7 requires Node >= 22.
* On AI SDK 6 (legacy), each model call must explicitly set `experimental_telemetry: { isEnabled: true }`.

## Embedding usage

`embed` and `embedMany` record reported input-token usage on their provider-call spans. Cost is calculated when the model matches an entry in traceroot's pricing table; otherwise, cost remains unset. The current standard pricing table has no embedding models. The operation wrapper remains in the trace, but its aggregate legacy usage is not counted again. This also applies when a batch is split across multiple provider calls.

Embedding spans without reported usage keep their token counts unset. traceroot does not estimate embedding tokens from input text or output vectors, avoiding duplicate estimates on orchestration spans whose provider-call children report the actual usage.

For AI SDK 7's `OpenTelemetry` integration, use `@ai-sdk/otel@1.0.76` or newer. If you use an older version, upgrade `@ai-sdk/otel`: older semconv emitters can repeat usage on both the embedding wrapper and its provider calls, while the fixed emitter reports usage only on provider calls.

AI SDK 6's built-in telemetry and AI SDK 7's `LegacyOpenTelemetry` integration report raw embedding usage as `ai.usage.tokens`. traceroot reads this field only on provider calls identified by `ai.operationId` or `operation.name` as `ai.embed.doEmbed` or `ai.embedMany.doEmbed`. Preserve these operation attributes in custom span processors so usage can be distinguished from wrapper aggregates. If your emitter omits them, use an emitter that supplies them or switch to the current AI SDK 7 `OpenTelemetry` integration. Normalized OpenInference or GenAI usage fields continue to be supported independently.

## Run the example

Clone the repo and run a complete agent end-to-end.

> [card] [TypeScript](https://github.com/traceroot-ai/traceroot/tree/main/examples/typescript/vercel-ai)
>
> Run the TypeScript example

## Next steps

> [card] [TypeScript SDK Reference](https://traceroot.ai/docs/tracing/typescript-sdk.md)
>
> Explore the full TypeScript SDK reference and configuration options.

> [card] [Cost Tracking](https://traceroot.ai/docs/tracing/cost-tracking.md)
>
> Learn how traceroot calculates token costs and tracks model spend.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.