Monitoring and tracing LLM applications with Braintrust
Braintrust is an end-to-end platform for building AI applications. When building with the AI SDK, you can integrate Braintrust to log, monitor, and take action on real-world interactions.
If you are using Node.js without a framework, you must configure the NodeSDK directly. In this case, it's more straightforward to use the BraintrustSpanProcessor.
First, install the necessary dependencies:
npm install ai @ai-sdk/openai @ai-sdk/otel braintrust @opentelemetry/sdk-node @opentelemetry/sdk-trace-base zod
Then, set up the OpenTelemetry SDK:
import { NodeSDK } from '@opentelemetry/sdk-node';import { registerTelemetry, generateText, tool, isStepCount } from 'ai';import { LegacyOpenTelemetry } from '@ai-sdk/otel';import { openai } from '@ai-sdk/openai';import { z } from 'zod';import { BraintrustSpanProcessor } from 'braintrust';const sdk = new NodeSDK({ spanProcessors: [ new BraintrustSpanProcessor({ parent: 'project_name:your-project-name', filterAISpans: true, }), ],});sdk.start();registerTelemetry(new LegacyOpenTelemetry());async function main() { const result = await generateText({ model: openai('gpt-6-luna'), messages: [ { role: 'user', content: 'What are my orders and where are they? My user ID is 123', }, ], tools: { listOrders: tool({ description: 'list all orders', inputSchema: z.object({ userId: z.string() }), execute: async ({ userId }) => `User ${userId} has the following orders: 1`, }), viewTrackingInformation: tool({ description: 'view tracking information for a specific order', inputSchema: z.object({ orderId: z.string() }), execute: async ({ orderId }) => `Here is the tracking information for ${orderId}`, }), }, context: { something: 'custom', someOtherThing: 'other-value', }, telemetry: { functionId: 'my-awesome-function', }, stopWhen: isStepCount(10), }); await sdk.shutdown();}main().catch(console.error);