Skip to main content

Vercel AI SDK Integration

Automatically track all Vercel AI SDK calls with full input/output logging, token usage, and tool executions. Two setup paths available depending on your use case.

What Gets Tracked Automatically


Installation


Setup Options

There are two ways to configure tracing depending on your needs. Use this when you need session tracking, multi-agent traces grouped under one root, or explicit control over flushing. Best for standalone scripts, Lambda, and multi-agent pipelines.

Basic Usage

Enable experimental_telemetry on every AI call. That’s all that’s needed — Cascade captures everything automatically.

With Tools

Tool calls are automatically traced as child spans with tool.name, tool.input, and tool.output:

Single Trace with traceRun

Important: Without traceRun, every generateText call becomes its own separate trace in the dashboard, named ai.generateText. To give your traces a meaningful name and group related AI calls together, always wrap them in traceRun.
Wrap a generateText call (including its tool executions) under a named root trace using traceRun.
This produces one trace:

Multi-Agent Traces (Single Trace)

Important: Without traceRun, each generateText call in a multi-agent pipeline creates a completely separate trace. To see all agents as one unified execution, wrap all of them inside a single traceRun. The outer traceRun becomes the root, and every generateText call inside becomes a child.
Wrap multiple generateText calls under one root trace using traceRun. Use traceAgent to label each agent — this creates named agent spans in the dashboard so you can tell which LLM calls and tool calls belong to which agent.
This produces one trace with clearly labelled agents:
traceAgent is optional — without it the agents still appear under the root, but all ai.generateText spans look identical in the sidebar. With traceAgent, each agent gets its own named span with a distinct icon, making it easy to navigate complex multi-agent traces.

Sessions (Multi-Turn Conversations)

Group multiple traces from one conversation under a session. Each turn is its own trace, all linked together on the Sessions page.
Key rule: Always wrap each turn in traceRun with session_id in metadata. Without traceRun, setSessionId has no effect because the Vercel AI SDK creates its own root spans independently.

Next.js API Routes


Environment Variables


Span Hierarchy

The Vercel AI SDK creates the following OTEL span structure, all captured by Cascade:
For streamText, the pattern is ai.streamTextai.streamText.doStream.

Flushing in Serverless

If you use traceRun, it automatically calls forceFlush when the root span ends. For simple scripts with a single traceRun, no extra flush call is needed. If you don’t use traceRun (or in serverless where you need a hard guarantee before returning the HTTP response), always call await flushTracing() explicitly:
The SDK also registers a process.on('beforeExit') hook as a safety net, but explicit flushing is more reliable in serverless environments where the process is killed externally.