Skip to main content
Go from install to live trace in under 5 minutes. This guide covers everything you need to add TracePilot observability to a Vercel AI SDK project.

Prerequisites

  • A Next.js or Node.js project using the Vercel AI SDK (ai package)
  • An OpenAI API key (or any AI SDK-compatible provider)
  • Node.js 18+

1. Install

2. Add your API key

Get your key from tracepilotai.com → Account Settings. It looks like tp_live_xxxxxxxxxxxxxxxx.
.env.local
For production, add this in your Vercel project settings → Environment Variables. Never commit it to source control.

3. Initialize

Create a single TracePilot instance and reuse it across your app.
lib/tracepilot.ts

4. Trace generateText

app/api/generate/route.ts

5. Trace streamText

app/api/chat/route.ts
wrapStream does not buffer or delay the stream. Time-to-first-token is unaffected.

Expected trace output

After running your app, open tracepilotai.com/dashboard. You should see:
For streaming:
Screenshot pending: Live trace in the TracePilot dashboard after a streamText call.

Troubleshooting

  • Confirm TRACEPILOT_API_KEY is set. Run console.log(process.env.TRACEPILOT_API_KEY) to verify it’s loaded.
  • Make sure tp.startTrace() is called before any wrapGenerate or wrapStream call.
  • Check that you’re not using .env instead of .env.local for local dev.
Pass the wrapped stream (returned by tp.wrapStream) to StreamingTextResponse — not the raw result from streamText. TracePilot reads from the stream tap; passing the raw stream skips the instrumentation.
The TracePilotVercel instance is undefined. This usually means TRACEPILOT_API_KEY is not set. The constructor receives undefined and throws lazily. Add a startup assertion:
Cost tracking requires the model name to be recognized. Ensure you’re passing a standard OpenAI model string (gpt-4o, gpt-4o-mini, etc.). Custom fine-tuned model names are not mapped automatically.

Next steps

Debugging Vercel AI SDK

Step-by-step guide to debugging generateText, streamText, and tool calls in production.

Multi-step agents

Chain spans with parentSpanId to trace complex pipelines.

Time-travel debugging

Fork any failing span and replay it without redeploying.

Cost tracking

Monitor token spend across every AI SDK call.