OpenAI model provider configuration and integration guide
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.
Add the OpenAI API key to your organization’s AI providers
Set the OpenAI API key and your Braintrust API key as environment variables
.env
Report incorrect code
Copy
Ask AI
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.
import OpenAI from "openai";// Initialize the Braintrust loggerconst logger = initLogger({ projectName: "My Project", // Your project name apiKey: process.env.BRAINTRUST_API_KEY,});// Wrap the OpenAI client with wrapOpenAIconst client = wrapOpenAI( new OpenAI({ apiKey: process.env.OPENAI_API_KEY, }),);// All API calls are automatically loggedconst 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?" }, ],});
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.
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.
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.
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).
OpenAI’s structured outputs are supported with the wrapper functions.
Report incorrect code
Copy
Ask AI
import { z } from "zod";// Define a Zod schema for the responseconst 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, }, },});