Trace generateText, streamText, generateObject, streamObject, and ToolLoopAgent with Fallom
Fallom provides a wrapper for the Vercel AI SDK that automatically traces all your LLM calls, including streaming with time-to-first-token metrics.
AI SDK v6 Support: Fallom fully supports AI SDK v6 including the new ToolLoopAgent class, tool approval workflows, and all v6 features. Both v5 and v6 patterns work with our SDK.
We recommend upgrading to AI SDK v6 for the latest features including reusable agents and improved type safety. Run npx @ai-sdk/codemod upgrade v6 to migrate automatically.
Full tracing - captures prompts, completions, tokens, costs, and all metadata.
Copy
import fallom from "@fallom/trace";import * as ai from "ai";import { createOpenAI } from "@ai-sdk/openai";// Initialize Fallomawait fallom.init({ apiKey: "your-fallom-api-key" });// Create a sessionconst session = fallom.session({ configKey: "my-app", sessionId: "session-123", customerId: "user-456",});// Wrap the AI SDKconst { generateText, streamText } = session.wrapAISDK(ai);// Create your providerconst openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });// Use as normal - fully traced!const { text } = await generateText({ model: openai("gpt-4o"), prompt: "What is the capital of France?",});console.log(text);// Trace includes: prompt, completion, tokens, costs, finish reason
Simpler integration - captures tokens and timing only.
This approach does NOT capture prompt or completion content. Use Option 1 if
you need full observability.
Copy
import fallom from "@fallom/trace";import { generateText } from "ai"; // Import original SDKimport { createOpenAI } from "@ai-sdk/openai";// Initialize Fallomawait fallom.init({ apiKey: "your-fallom-api-key" });// Create a sessionconst session = fallom.session({ configKey: "my-app", sessionId: "session-123", customerId: "user-456",});// Create your providerconst openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });// Wrap the MODELconst tracedModel = session.traceModel(openai("gpt-4o"));// Use original SDK with traced modelconst { text } = await generateText({ model: tracedModel, prompt: "What is the capital of France?",});console.log(text);// Trace includes: tokens, timing (no prompt/completion content)
Streaming responses automatically capture time to first token (TTFT):
Copy
import fallom from "@fallom/trace";import * as ai from "ai";import { createOpenAI } from "@ai-sdk/openai";await fallom.init({ apiKey: process.env.FALLOM_API_KEY });const session = fallom.session({ configKey: "my-app", sessionId: "session-123", customerId: "user-456",});const { streamText } = session.wrapAISDK(ai);const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY,});const result = await streamText({ model: openai("gpt-4o"), prompt: "Write a short poem about coding.",});// Consume the streamfor await (const chunk of result.textStream) { process.stdout.write(chunk);}// Trace is sent after stream completes with:// - Total duration// - Time to first token// - Token counts// - Session and user IDs
AI SDK v6 introduces the ToolLoopAgent class for building reusable agents. Fallom supports tracing ToolLoopAgent by wrapping the underlying AI SDK functions:
Copy
import fallom from "@fallom/trace";import * as ai from "ai";import { ToolLoopAgent } from "ai";import { createOpenAI } from "@ai-sdk/openai";import { z } from "zod";await fallom.init({ apiKey: "your-fallom-api-key" });const session = fallom.session({ configKey: "my-agent", sessionId: "session-123", customerId: "user-456",});// Wrap the AI SDKconst { generateText, streamText } = session.wrapAISDK(ai);const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY,});// Define your toolsconst weatherTool = ai.tool({ description: "Get the current weather for a location", parameters: z.object({ location: z.string().describe("The city and country"), }), execute: async ({ location }) => ({ location, temperature: 72, condition: "sunny", }),});// Create a ToolLoopAgent with traced generateTextconst weatherAgent = new ToolLoopAgent({ model: openai("gpt-4o"), instructions: "You are a helpful weather assistant.", tools: { weather: weatherTool }, // Pass the wrapped generateText for tracing generateText: generateText,});// Use the agent - all calls are automatically traced!const result = await weatherAgent.generate({ prompt: "What's the weather in San Francisco?",});console.log(result.text);
// Create agent with traced streamText for streamingconst streamingAgent = new ToolLoopAgent({ model: openai("gpt-4o"), instructions: "You are a helpful assistant.", tools: { weather: weatherTool }, streamText: streamText, // Use wrapped streamText});const result = await streamingAgent.stream({ prompt: "What's the weather in New York?",});for await (const chunk of result.textStream) { process.stdout.write(chunk);}
If you’re using OpenRouter and prefer zero-SDK tracing, use OpenRouter Broadcast. However, Vercel AI SDK doesn’t pass custom body fields to OpenRouter, so you’ll need to use the OpenAI SDK directly for session tracking:
Copy
import OpenAI from "openai";// Use OpenAI SDK for broadcast (not Vercel AI SDK)const openrouter = new OpenAI({ apiKey: process.env.OPENROUTER_API_KEY, baseURL: "https://openrouter.ai/api/v1", defaultHeaders: { "X-Broadcast-URL": "https://broadcast.fallom.com/v1/traces", "X-Broadcast-Auth": "Bearer YOUR_FALLOM_API_KEY", },});// For structured output, use JSON modeconst response = await openrouter.chat.completions.create({ model: "openai/gpt-4o-mini", messages: [ { role: "system", content: "Output JSON: {name: string, age: number}" }, { role: "user", content: "Generate a person." }, ], response_format: { type: "json_object" }, // @ts-ignore - OpenRouter extensions for session tracking session_id: "conversation-123", user: "customer-456",});const person = JSON.parse(response.choices[0].message.content!);
Feature
SDK Wrapper (session.wrapAISDK)
Broadcast (OpenAI SDK)
Vercel AI SDK
✅ Full support
❌ Use OpenAI SDK
Session tracking
fallom.session()
Body fields
Model A/B Testing
✅
❌
Prompt Management
✅
❌
Time to first token
✅ (streaming)
❌
Setup complexity
More code
Just headers
Recommendation: Use session.wrapAISDK() for Vercel AI SDK. It provides
full tracing, session tracking, and works with all Vercel AI SDK functions
including generateObject and streamObject.