Skip to main content
Cloudflare AI Gateway provides a unified interface to access multiple AI providers with observability, caching, and rate limiting. Braintrust automatically traces requests through Cloudflare AI Gateway across all supported providers.

Setup

Install the Braintrust SDK and OpenAI client:
# pnpm
pnpm add braintrust openai
# npm
npm install braintrust openai
Configure your environment variables:
.env
OPENAI_API_KEY=<your-provider-api-key>
CLOUDFLARE_ACCOUNT_ID=<your-cloudflare-account-id>
CLOUDFLARE_AI_GATEWAY_NAME=<your-gateway-name>
BRAINTRUST_API_KEY=<your-braintrust-api-key>

Trace with Cloudflare AI Gateway

Use wrapOpenAI to automatically trace requests through Cloudflare’s unified API endpoint:
import { OpenAI } from "openai";
import { initLogger, wrapOpenAI } from "braintrust";

// Initialize Braintrust logging
initLogger({
  projectName: "My Project",
  apiKey: process.env.BRAINTRUST_API_KEY,
});

// Create OpenAI client configured for Cloudflare AI Gateway
const client = wrapOpenAI(
  new OpenAI({
    // OpenAI SDK automatically adds /chat/completions
    baseURL: `https://gateway.ai.cloudflare.com/v1/${process.env.CLOUDFLARE_ACCOUNT_ID}/${process.env.CLOUDFLARE_AI_GATEWAY_NAME}/compat`,
    apiKey: process.env.OPENAI_API_KEY,
  }),
);

// This request will be automatically traced by Braintrust
const result = await client.chat.completions.create({
  model: "openai/gpt-4o",
  messages: [{ role: "user", content: "What is 1+1?" }],
});

console.log(result);

Switching providers

Cloudflare AI Gateway’s unified API allows you to easily switch between different AI providers by changing the model parameter and corresponding API key. All requests are automatically traced by Braintrust regardless of which provider you use.
import { OpenAI } from "openai";
import { wrapOpenAI } from "braintrust";

// Switch to Anthropic
const client = wrapOpenAI(
  new OpenAI({
    baseURL: `https://gateway.ai.cloudflare.com/v1/${process.env.CLOUDFLARE_ACCOUNT_ID}/${process.env.CLOUDFLARE_AI_GATEWAY_NAME}/compat`,
    apiKey: process.env.ANTHROPIC_API_KEY, // Use Anthropic's API key
  }),
);

const result = await client.chat.completions.create({
  model: "anthropic/claude-sonnet-4-5-20250929", // Use Anthropic model
  messages: [{ role: "user", content: "Hello!" }],
});
Cloudflare AI Gateway supports OpenAI, Anthropic, Google AI Studio, Groq, Mistral, Cohere, Perplexity, DeepSeek, Cerebras, xAI, and Workers AI. See the Cloudflare documentation for the complete list.

Resources