Skip to main content
To use an agent for automatic setup, see the Quickstart.

Sign up

If you don’t have a Braintrust account, sign up for free at braintrust.dev.

Install the SDK

Add the Braintrust SDK to your project using your preferred package manager:

Set an API key

On the Settings > API keys page, create an API key. Then, set it as an environment variable:
.env
The SDK picks up your API key automatically. When you’re done testing locally, remember to set it in production.
Never put your API key in version control.

Configure tracing

The way to instrument your application to automatically collect traces depends on the runtime and framework you are using.
Braintrust auto-instrumentation for Node.js uses a startup hook. The hook patches supported AI libraries as your process starts, logging your LLM calls.
Version Requirement: Node.js 18.19.0+ or 20.6.0+ and above.
If you are using a bundler to build your application, also follow the guide for your specific bundler below. (Vite, Esbuild, Rollup, or Webpack)
1

Initialize the logger

Call initLogger() once, as soon as possible on application startup, before making any AI calls. The SDK reads BRAINTRUST_API_KEY from the environment configured above.
2

Run with the import hook

Start your Node.js application with the Braintrust hook:
After following these steps, calls to your AI provider should be traced. For a list of all supported AI providers see TypeScript SDK integrations.
To trace LLM calls from your Cloudflare app, wrap your AI provider’s SDK with Braintrust. The example below uses OpenAI (for other providers, see TypeScript SDK integrations).
If you are using a bundler to build your application, also follow the guide for your specific bundler below. (Vite, Esbuild, Rollup, or Webpack)
1

Set Cloudflare secrets

Store your Braintrust API key as Cloudflare secret:
2

Setting the Node.js compatibility flag

Since the Braintrust SDK uses the Node.js AsyncLocalStorage API, you need to enable Node.js compatibility in your Wrangler configuration:
3

Initialize the logger and wrap your client

Inside fetch, initialize the logger with secrets from the env binding, wrap your OpenAI client, and pass logger.flush() to ctx.waitUntil() so buffered logs ship after the response returns.
To trace LLM calls from your Deno app, wrap your AI provider’s SDK with Braintrust. The example below uses OpenAI (for other providers, see TypeScript SDK integrations).
If you are using a bundler to build your application, also follow the guide for your specific bundler below. (Vite, Esbuild, Rollup, or Webpack)
1

Initialize the logger and wrap your client

Read your BRAINTRUST_API_KEY from Deno.env, initialize Braintrust, and wrap the provider client before making AI calls.
2

Run with Deno permissions

Grant environment variable access via --allow-env and network access for Braintrust via --allow-net and run your application.
To trace LLM calls from a Node.js Lambda function, initialize Braintrust outside the handler once, wrap your AI provider’s SDK, and flush before the invocation returns. AWS Lambda does not keep background promises alive after the handler completes, so set asyncFlush: false when initializing the logger.
If you are using a bundler to build your application, also follow the guide for your specific bundler below. (Vite, Esbuild, Rollup, or Webpack)
1

Initialize the logger and wrap your client

Create the logger and provider client at module scope so Lambda can reuse them across warm invocations.
lambda.ts
To trace LLM calls from a Next.js app, wrap your Next.js config and initialize Braintrust in Next’s instrumentation.ts.
1

Configure the Next.js Config

Wrap your Next.js Config with wrapNextjsConfigWithBraintrust.
next.config.ts
2

Initialize the logger

Create instrumentation.ts in the project root, or src/instrumentation.ts if your app uses src/ and initialize the logger.
instrumentation.ts
To trace LLM calls from a Nest.js application, initialize Braintrust before Nest creates your application module, then start the Node.js process with the Braintrust hook.
1

Initialize the logger

Call initLogger() before NestFactory.create() and adding an import for braintrust/apply-auto-instrumentation. This ensures Braintrust is ready before Nest constructs providers that may create AI clients.
src/main.ts
2

Start Nest with the import hook

Update the scripts you use to run Nest so every normal launch path loads braintrust/hook.mjs. For development with nest start, use NODE_OPTIONS so the hook is inherited by the Nest CLI’s Node process. For production, pass --import to the compiled app directly.
package.json
To trace LLM calls from a Nitro server, initialize Braintrust when Nitro starts, add the Braintrust Rollup plugin to the Nitro server build, and flush traces from global middleware.
1

Instrument the server bundle

Add the Braintrust Rollup plugin to nitro.config.ts. Nitro builds the server bundle with Rollup, so this instruments supported AI SDK packages during the Nitro build.
nitro.config.ts
2

Initialize the logger

Add a Nitro plugin that initializes Braintrust once when Nitro starts.
plugins/braintrust.ts
3

Add a flush middleware

Add a global middleware that runs after each handler calling flush() in a finally block and pass the promise to event.waitUntil(). This will make Nitro keep the runtime alive long enough to send buffered traces after the response is ready on runtimes that are supported (like Vercel and Cloudflare).
middleware/braintrust.ts
After following these steps, calls to your AI provider should be traced. For a list of all supported AI providers see TypeScript SDK integrations.
To trace LLM calls from a Nuxt app, instrument both Nuxt’s Vite build and Nitro’s server build, then initialize Braintrust from a Nitro plugin.
1

Instrument the Nuxt build

Nuxt uses Vite for the client build and Rollup through Nitro for the server build. Add both Braintrust plugins to nuxt.config.ts.
nuxt.config.ts
2

Initialize from a Nitro plugin

Add a server plugin that initializes Braintrust when Nitro starts and schedules a flush after each response.
server/plugins/braintrust.ts
3

Add a flush middleware

Add a global middleware that runs on each request calling flush() and pass the promise to event.waitUntil(). This will make Nuxt keep the runtime alive long enough to send buffered traces after the response is ready on runtimes that are supported (like Vercel and Cloudflare).
middleware/braintrust.ts
4

Start Nuxt with the import hook

Update the scripts you use to run Nuxt so every normal launch path loads braintrust/hook.mjs. For development with nuxt dev, use NODE_OPTIONS so the hook is inherited by the Nuxt CLI’s Node process. For production, pass --import to the compiled app directly.
package.json
To trace LLM calls in React Router Framework applications, add the Braintrust Vite plugin to the config, initialize the logger in the server entry, and add a node import hook to your server start.
1

Instrument the Vite build

Add the Braintrust Vite plugin to vite.config.ts alongside the React Router plugin.
vite.config.ts
2

Initialize the logger

If your app uses the default hidden server entry, reveal it first:
Then call initLogger() in the module scope before importing or constructing any AI clients.
app/entry.server.tsx
3

Add the Braintrust loader hook

Run your server with the Braintrust import hook:
package.json
To trace LLM calls on SvelteKit applications, add the Braintrust Vite plugin and initialize the braintrust logger.
1

Instrument the Vite build

Add the Braintrust Vite plugin before sveltekit() in vite.config.ts.
vite.config.ts
2

Initialize and flush from server hooks

Code in SvelteKit hook modules runs when the app starts. Initialize Braintrust at module scope and flush from handle.
src/hooks.server.ts
To trace LLM calls from TanStack Start, initialize Braintrust in src/start.ts, add global request middleware to flush traces, and instrument the Vite build.
1

Instrument the Vite build

Add the Braintrust Vite plugin:
vite.config.ts
2

Initialize and flush from global middleware

Initialize Braintrust in module scope of a global middleware and flush in a finally block.
src/start.ts
3

Add the Braintrust loader hook

Run your server with the Braintrust import hook:
package.json
Add the Braintrust Vite plugin to your Vite config. The plugin instruments supported AI packages at build time.
vite.config.ts
Add the Braintrust esbuild plugin to the plugins array in your build script. The plugin runs during bundling and instruments supported AI SDK packages before the bundle is written.
build.ts
Add the Braintrust Webpack plugin to your Webpack config. The plugin instruments supported AI SDK packages at build time.
webpack.config.ts
Add the Braintrust Rollup plugin to your Rollup config. The plugin instruments supported AI SDK packages during the Rollup build.
rollup.config.ts

Verify tracing

Run your app and make an AI call. A trace will show up in your Braintrust Logs, usually within seconds.
If traces appear in Braintrust, you’ve successfully set up the SDK.
If your traces don’t appear in Braintrust, see Troubleshooting.

Next steps

Learn more about using the SDK to observe, evaluate, and improve your AI application:
  • Instrument — trace LLM calls and application logic
  • Observe — search and analyze production traces
  • Annotate — label traces and build datasets
  • Evaluate — measure quality and catch regressions
  • Deploy — ship to production with the AI gateway