Cloudflare

Cloudflare AI Gateway provides a unified interface to access multiple AI providers with enhanced observability, caching, and rate limiting. Braintrust automatically traces requests made through Cloudflare AI Gateway, giving you comprehensive observability for your AI applications.

Install the braintrust and openai packages.

pnpm add braintrust openai

Use wrapOpenAI to automatically trace requests made through Cloudflare AI Gateway. Configure your client to use Cloudflare's unified API endpoint, and Braintrust will capture all requests, responses, and metrics.

.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>
cloudflare-tracing.ts
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.

// 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-3-5-sonnet-20241022", // Use Anthropic model
  messages: [{ role: "user", content: "Hello!" }],
});

Supported providers include OpenAI, Anthropic, Google AI Studio, Groq, Mistral, Cohere, Perplexity, DeepSeek, Cerebras, xAI, and Workers AI. See the Cloudflare documentation for the complete list.

On this page

Cloudflare - Docs - Braintrust