Integrations / Next.js
To trace a Next.js AI route with Tracify, wrap the handler's work with traceAgent, wrap the model with the Tracify middleware, and wrap your tools. Each request becomes one trace with its model calls and tool calls.
Your code
Next.js
Tracify
One run.
Setup
The Tracify SDK plus the Vercel AI SDK and a provider.
npm install tracify-sdk ai @ai-sdk/openaiPut the key in .env.local for development and in your Vercel project settings for production.
TRACIFY_API_KEY=tracify_sk_live_...The SDK needs Node.js to keep concurrent requests in separate traces, so set the route to the Node.js runtime. It will not run on the edge runtime.
export const runtime = "nodejs";tracifyMiddleware records every generateText and streamText call made with the wrapped model. traceAgent turns each request into one run.
// app/api/chat/route.ts
import { openai } from "@ai-sdk/openai";
import { streamText, wrapLanguageModel } from "ai";
import { traceAgent, tracifyMiddleware } from "tracify-sdk";
export const runtime = "nodejs";
const model = wrapLanguageModel({
model: openai("gpt-4o-mini"),
middleware: tracifyMiddleware(),
});
const handle = traceAgent(async (prompt: string) => {
const result = streamText({ model, prompt });
return result.toTextStreamResponse();
}, { name: "chat-route" });
export async function POST(request: Request) {
const { prompt } = await request.json();
return handle(prompt);
}traceTools wraps each tool's execute function, so every tool call is its own step with input, output and time.
import { tool } from "ai";
import { z } from "zod";
import { traceTools } from "tracify-sdk";
const tools = traceTools({
getOrder: tool({
description: "Look up an order",
inputSchema: z.object({ id: z.string() }),
execute: async ({ id }) => fetchOrder(id),
}),
});Run your code one time, then open the Tracing page of your project. Your route shows up as a run with its steps in order, plus the tokens, time and any error for each call.
Good to know
A streamed model call is recorded when its stream ends. Data is sent in the background, so on a serverless host the last spans of a request can be lost if the function is frozen right after it responds. Check the first runs in Tracing after you deploy.
Good to know
Wrap the function the Server Action calls with traceAgent the same way. Anything marked use server runs on Node.js.
Full details in the documentation. All integrations are listed on the integrations page.
