> ## Documentation Index
> Fetch the complete documentation index at: https://docs.braintrust.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloudflare AI Chat

> Trace Cloudflare AI Chat agent conversations in Braintrust to debug chat turns, LLM calls, tool use, and errors

[Cloudflare AI Chat](https://developers.cloudflare.com/agents/) (`@cloudflare/ai-chat`) provides a chat-agent base class built on the Cloudflare Agents platform. Braintrust traces each `onChatMessage` call, capturing the conversation history, LLM turns, tool calls, and errors.

> [!NOTE]
>
> For Cloudflare Workers deployments, use [manual instrumentation](#manual-instrumentation-typescript) with `wrapCloudflareAIChat()`. The `--import` auto-instrumentation hook only runs under Node, not in the Cloudflare Workers runtime (`workerd`). See the [Cloudflare setup guide](https://docs.braintrust.dev/sdks/typescript/install-and-instrument.md#cloudflare) for enabling `nodejs_compat` and flushing traces with `ctx.waitUntil()`.

**TypeScript**

## Setup

Install Braintrust alongside `@cloudflare/ai-chat`, then set your API keys. Requires `@cloudflare/ai-chat` v0.9.0 or later.

**1. Install packages**

```bash pnpm theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
pnpm add braintrust @cloudflare/ai-chat
```

```bash npm theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
npm install braintrust @cloudflare/ai-chat
```

**2. Set environment variables**

```bash title=".env" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
BRAINTRUST_API_KEY=<your-braintrust-api-key>

# For organizations on the EU data plane, use https://api-eu.braintrust.dev
# For self-hosted deployments, use your data plane URL
# BRAINTRUST_API_URL=<your-braintrust-api-url>
```

## Manual instrumentation

Manual instrumentation is the recommended approach for Cloudflare Workers. Wrap the module with `wrapCloudflareAIChat()` at module scope, then extend the wrapped `AIChatAgent` class so all instances receive Braintrust tracing.

```typescript title="cloudflare-ai-chat-manual.ts" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
import { initLogger, wrapCloudflareAIChat } from "braintrust";
import * as aiChat from "@cloudflare/ai-chat";

const { AIChatAgent } = wrapCloudflareAIChat(aiChat);

export class MyAgent extends AIChatAgent {
  async onChatMessage(onFinish) {
    // Your chat handling logic here
  }
}
```

`wrapCloudflareAIChat()` accepts the module namespace (`import * as aiChat from "@cloudflare/ai-chat"`) and returns a proxy. Extend the wrapped `AIChatAgent` export to ensure instances receive Braintrust tracing.

Initialize the logger with your `env` bindings and flush traces with `ctx.waitUntil(logger.flush())` inside your Worker's `fetch` handler. Deploying to Cloudflare Workers also requires the `nodejs_compat` compatibility flag and storing `BRAINTRUST_API_KEY` as a Wrangler secret. See the [Cloudflare setup guide](https://docs.braintrust.dev/sdks/typescript/install-and-instrument.md#cloudflare) for the full deployment configuration.

## Auto-instrumentation

Auto-instrumentation patches the SDK at runtime without modifying your application code, but the `--import` hook only runs under Node (local development or tests), not in the Cloudflare Workers runtime. For a deployed Worker, use manual instrumentation above.

**1. Initialize Braintrust and define your agent**

```javascript title="cloudflare-ai-chat-auto.js" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
import { initLogger } from "braintrust";
import { AIChatAgent } from "@cloudflare/ai-chat";

initLogger({
  projectName: "cloudflare-ai-chat-example", // Replace with your project name
  apiKey: process.env.BRAINTRUST_API_KEY,
});

export class MyAgent extends AIChatAgent {
  async onChatMessage(onFinish) {
    // Your chat handling logic here
  }
}
```

**2. Run with the import hook**

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
node --import braintrust/hook.mjs cloudflare-ai-chat-auto.js
```

> [!WARNING]
>
> The `--import` hook only patches the SDK when your code runs under Node, such as local development or tests. It does not run in the Cloudflare Workers runtime (`workerd`), so a Worker deployed with Wrangler stays uninstrumented. To trace a deployed Worker, use [manual instrumentation](#manual-instrumentation-typescript) with `wrapCloudflareAIChat()`.

The auto-instrumentation example uses plain JavaScript so `node --import` can run the file directly. The Braintrust APIs work the same in TypeScript projects. Compile your TypeScript to JavaScript and run the compiled file with the import hook, or [run TypeScript directly](https://docs.braintrust.dev/instrument/trace-llm-calls.md#auto-instrumentation) with a TypeScript loader.

> [!NOTE]
>
> If you're using a bundler, see [Trace LLM calls](https://docs.braintrust.dev/instrument/trace-llm-calls.md#auto-instrumentation) for plugin and loader setup.

## What Braintrust traces

Braintrust captures:

* Task spans for each `AIChatAgent.onChatMessage` call, with the conversation history as input and the response message as output.
* Nested LLM spans for underlying AI SDK calls, with messages, model, and token usage.
* Tool call spans for any tools invoked during the conversation.
* Errors captured on the span if the chat turn fails or the stream encounters an error.

## Resources

* [Cloudflare Agents documentation](https://developers.cloudflare.com/agents/)
* [`@cloudflare/ai-chat` on npm](https://www.npmjs.com/package/@cloudflare/ai-chat)
* [Trace LLM calls](https://docs.braintrust.dev/instrument/trace-llm-calls.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.