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
| Category | Captured |
|---|---|
| LLM calls | Model, provider, prompt, system message, messages |
| Responses | Generated text, finish reason |
| Token usage | Input tokens, output tokens, total tokens |
| Tool calls | Tool name, input args, output result per call |
| Latency | Duration in milliseconds |
Installation
Setup Options
There are two ways to configure tracing depending on your needs.Option A: initTracing (recommended for scripts and multi-agent systems)
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
Enableexperimental_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 withtool.name, tool.input, and tool.output:
Single Trace with traceRun
Important: WithoutWrap atraceRun, everygenerateTextcall becomes its own separate trace in the dashboard, namedai.generateText. To give your traces a meaningful name and group related AI calls together, always wrap them intraceRun.
generateText call (including its tool executions) under a named root trace using traceRun.
Multi-Agent Traces (Single Trace)
Important: WithoutWrap multipletraceRun, eachgenerateTextcall in a multi-agent pipeline creates a completely separate trace. To see all agents as one unified execution, wrap all of them inside a singletraceRun. The outertraceRunbecomes the root, and everygenerateTextcall inside becomes a child.
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.
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.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
| Variable | Required | Description |
|---|---|---|
CASCADE_API_KEY | Yes | Your Cascade API key (csk_live_...) |
CASCADE_ENDPOINT | No | Custom backend URL. Defaults to https://api.runcascade.com/v1/traces |
Span Hierarchy
The Vercel AI SDK creates the following OTEL span structure, all captured by Cascade:streamText, the pattern is ai.streamText → ai.streamText.doStream.
Flushing in Serverless
If you usetraceRun, 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:
process.on('beforeExit') hook as a safety net, but explicit flushing is more reliable in serverless environments where the process is killed externally.