OpenAI

OpenAI provides access to GPT models including GPT-5 and other cutting-edge language models. Braintrust integrates seamlessly with OpenAI through direct API access, wrapOpenAI wrapper functions for automatic tracing, and proxy support.

Setup

To use OpenAI with Braintrust, you'll need an OpenAI API key.

  1. Visit OpenAI's API platform and create a new API key
  2. Add the OpenAI API key to your organization's AI providers
  3. Set the OpenAI API key and your Braintrust API key as environment variables
.env
OPENAI_API_KEY=<your-openai-api-key>
BRAINTRUST_API_KEY=<your-braintrust-api-key>
 
# If you are self-hosting Braintrust, set the URL of your hosted dataplane
# BRAINTRUST_API_URL=<your-braintrust-api-url>

API keys are encrypted using 256-bit AES-GCM encryption and are not stored or logged by Braintrust.

Install the braintrust and openai packages.

pnpm add braintrust openai

Trace with OpenAI

Trace your OpenAI LLM calls for observability and monitoring.

Using the OpenAI Agents SDK? See the OpenAI Agents SDK framework docs.

Trace automatically with wrapOpenAI

Braintrust provides wrapOpenAI (TypeScript) and wrap_openai (Python) functions that automatically log OpenAI API calls. To use them, initialize the logger and pass the OpenAI client to the wrapOpenAI function.

wrapOpenAI is a convenience function that wraps the OpenAI client with the Braintrust logger. For more control, learn how to customize traces.

import { initLogger, wrapOpenAI } from "braintrust";
import OpenAI from "openai";
 
// Initialize the Braintrust logger
const logger = initLogger({
  projectName: "My Project", // Your project name
  apiKey: process.env.BRAINTRUST_API_KEY,
});
 
// Wrap the OpenAI client with wrapOpenAI
const client = wrapOpenAI(
  new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
  }),
);
 
// All API calls are automatically logged
const result = await client.chat.completions.create({
  model: "gpt-5",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "What is machine learning?" },
  ],
});

Stream OpenAI responses

wrap_openai/wrapOpenAI can automatically log metrics like prompt_tokens, completion_tokens, and tokens for streaming LLM calls if the LLM API returns them. Set include_usage to true in the stream_options parameter to receive these metrics from OpenAI.

const result = await client.chat.completions.create({
  model: "gpt-5-mini",
  messages: [{ role: "user", content: "Count to 10" }],
  stream: true, 
  stream_options: {
    include_usage: true, // Required for token metrics
  }, 
});
 
for await (const chunk of result) {
  process.stdout.write(chunk.choices[0]?.delta?.content || ""); 
} 

Evaluate with OpenAI

Evaluations help you distill the non-deterministic outputs of OpenAI models into an effective feedback loop that enables you to ship more reliable, higher quality products. Braintrust Eval is a simple function composed of a dataset of user inputs, a task, and a set of scorers. To learn more about evaluations, see the Experiments guide.

Basic OpenAI eval setup

Evaluate the outputs of OpenAI models with Braintrust.

eval_openai.ts
import { Eval } from "braintrust";
import { OpenAI } from "openai";
 
const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});
 
Eval("OpenAI Evaluation", {
  // An array of user inputs and expected outputs
  data: () => [
    { input: "What is 2+2?", expected: "4" },
    { input: "What is the capital of France?", expected: "Paris" },
  ],
  task: async (input) => {
    // Your OpenAI LLM call
    const response = await client.chat.completions.create({
      model: "gpt-5-mini",
      messages: [{ role: "user", content: input }],
    });
    return response.choices[0].message.content;
  },
  scores: [
    {
      name: "accuracy",
      // A simple scorer that returns 1 if the output matches the expected output, 0 otherwise
      scorer: (args) => (args.output === args.expected ? 1 : 0),
    },
  ],
});

Learn more about eval data and scorers.

Use OpenAI as an LLM judge

You can use OpenAI models to score the outputs of other AI systems. This example uses the LLMClassifierFromSpec scorer to score the relevance of the outputs of an AI system.

Install the autoevals package to use the LLMClassifierFromSpec scorer.

pnpm add autoevals

Create a scorer that uses the LLMClassifierFromSpec scorer to score the relevance of the outputs of an AI system. You can then include relevanceScorer as a scorer in your Eval function (see above).

eval_openai.ts
import { LLMClassifierFromSpec } from "autoevals";
 
const relevanceScorer = LLMClassifierFromSpec("Relevance", {
  choice_scores: { Relevant: 1, Irrelevant: 0 },
  model: "gpt-5-mini",
  use_cot: true,
});

Additional features

Structured outputs

OpenAI's structured outputs are supported with the wrapper functions.

eval_openai.ts
import { z } from "zod";
 
// Define a Zod schema for the response
const ResponseSchema = z.object({
  name: z.string(), 
  age: z.number(), 
}); 
 
const completion = await client.beta.chat.completions.parse({
  model: "gpt-5-mini",
  messages: [
    { role: "system", content: "Extract the person's name and age." },
    { role: "user", content: "My name is John and I'm 30 years old." },
  ],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "person",
      // The Zod schema for the response
      schema: ResponseSchema, 
    },
  },
});

Function calling and tools

Braintrust supports OpenAI function calling for building AI agents with tools.

eval_openai.ts
const tools = [
  {
    type: "function" as const,
    function: {
      name: "get_weather",
      description: "Get current weather for a location",
      parameters: {
        type: "object",
        properties: {
          location: { type: "string" },
        },
        required: ["location"],
      },
    },
  },
];
 
const response = await client.chat.completions.create({
  model: "gpt-5-mini",
  messages: [{ role: "user", content: "What's the weather in San Francisco?" }],
  tools, 
});

Multimodal content, attachments, errors, and masking sensitive data

To learn more about these topics, check out the customize traces guide.

Use OpenAI with Braintrust AI proxy

You can also access OpenAI models through the Braintrust AI Proxy, which provides a unified interface for multiple providers.

import { OpenAI } from "openai";
 
const client = new OpenAI({
  baseURL: "https://api.braintrust.dev/v1/proxy",
  apiKey: process.env.BRAINTRUST_API_KEY, 
});
 
const response = await client.chat.completions.create({
  model: "gpt-5-mini",
  messages: [{ role: "user", content: "What is a proxy?" }],
  seed: 1, // A seed activates the proxy's cache
});

Cookbooks

Models and capabilities

ModelMultimodalReasoningMax inputMax outputInput $/1MOutput $/1M
gpt-5400,000128,000$1.25$10.00
gpt-5-2025-08-07400,000128,000$1.25$10.00
gpt-5-mini400,000128,000$0.25$2.00
gpt-5-mini-2025-08-07400,000128,000$0.25$2.00
gpt-5-nano400,000128,000$0.05$0.40
gpt-5-nano-2025-08-07400,000128,000$0.05$0.40
gpt-5-chat-latest400,000128,000$1.25$10.00
gpt-4o128,00016,384$2.50$10.00
gpt-4o-2024-11-20128,00016,384$2.50$10.00
gpt-4o-2024-08-06128,00016,384$2.50$10.00
gpt-4o-2024-05-13128,0004,096$5.00$15.00
gpt-4.11,047,57632,768$2.00$8.00
gpt-4.1-2025-04-141,047,57632,768$2.00$8.00
gpt-4o-mini128,00016,384$0.15$0.60
gpt-4o-mini-2024-07-18128,00016,384$0.15$0.60
gpt-4.1-mini1,047,57632,768$0.40$1.60
gpt-4.1-mini-2025-04-141,047,57632,768$0.40$1.60
gpt-4.1-nano1,047,57632,768$0.10$0.40
gpt-4.1-nano-2025-04-141,047,57632,768$0.10$0.40
o4-mini200,000100,000$1.10$4.40
o4-mini-2025-04-16200,000100,000$1.10$4.40
o3-mini200,000100,000$1.10$4.40
o3-mini-2025-01-31200,000100,000$1.10$4.40
o3-pro200,000100,000$20.00$80.00
o3-pro-2025-06-10200,000100,000$20.00$80.00
o3200,000100,000$2.00$8.00
o3-2025-04-16200,000100,000$2.00$8.00
o1200,000100,000$15.00$60.00
o1-2024-12-17200,000100,000$15.00$60.00
o1-preview128,00032,768$15.00$60.00
o1-preview-2024-09-12128,00032,768$15.00$60.00
o1-mini128,00065,536$1.10$4.40
o1-mini-2024-09-12128,00065,536$3.00$12.00
o1-pro200,000100,000$150.00$600.00
o1-pro-2025-03-19200,000100,000$150.00$600.00
chatgpt-4o-latest128,0004,096$5.00$15.00
gpt-4-turbo128,0004,096$10.00$30.00
gpt-4-turbo-2024-04-09128,0004,096$10.00$30.00
gpt-4-turbo-preview128,0004,096$10.00$30.00
gpt-48,1924,096$30.00$60.00
gpt-4-0125-preview128,0004,096$10.00$30.00
gpt-4-1106-preview128,0004,096$10.00$30.00
gpt-4-06138,1924,096$30.00$60.00
gpt-4-03148,1924,096$30.00$60.00
gpt-4.5-preview128,00016,384$75.00$150.00
gpt-4.5-preview-2025-02-27128,00016,384$75.00$150.00
gpt-4o-search-preview128,00016,384$2.50$10.00
gpt-4o-search-preview-2025-03-11128,00016,384$2.50$10.00
gpt-4o-mini-search-preview128,00016,384$0.15$0.60
gpt-4o-mini-search-preview-2025-03-11128,00016,384$0.15$0.60
gpt-3.5-turbo-012516,3854,096$0.50$1.50
gpt-3.5-turbo16,3854,096$1.50$2.00
gpt-3.5-turbo-110616,3854,096$1.00$2.00
gpt-3.5-turbo-instruct8,1924,096$1.50$2.00
gpt-3.5-turbo-instruct-09148,1924,097$1.50$2.00
gpt-4-32k32,7684,096$60.00$120.00
gpt-4-32k-061332,7684,096$60.00$120.00
gpt-4-32k-031432,7684,096$60.00$120.00
gpt-4-vision-preview128,0004,096$10.00$30.00
gpt-4-1106-vision-preview128,0004,096$10.00$30.00
gpt-3.5-turbo-16k16,3854,096$3.00$4.00
gpt-3.5-turbo-16k-061316,3854,096$3.00$4.00
gpt-3.5-turbo-06134,0974,096$1.50$2.00
gpt-3.5-turbo-03014,0974,096$1.50$2.00
text-davinci-003$2.00$2.00
OpenAI - Docs - Braintrust