Skip to main content

Documentation Index

Fetch the complete documentation index at: https://wb-21fd5541-style-guide-models-integrations-20260527-015516.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Use the W&B OpenAI API integration to log requests, responses, token counts, and model metadata for all OpenAI models, including fine-tuned models. This guide is for developers who call the OpenAI API and want visibility into their prompts, completions, and usage without adding manual logging code. By logging your API inputs and outputs, you can quickly evaluate the performance of different prompts, compare different model settings (such as temperature), and track other usage metrics such as token usage.
See the OpenAI fine-tuning integration to learn how to use W&B to track your fine-tuning experiments, models, and datasets and share your results with your colleagues.
W&B trace view showing OpenAI API requests, responses, and token usage logged automatically

Install OpenAI Python API library

The W&B autolog integration works with OpenAI version 0.28.1 and earlier, so you must install a compatible version before enabling autologging. To install OpenAI Python API version 0.28.1:
pip install openai==0.28.1

Use the OpenAI Python API

The following steps walk you through enabling autologging, calling the OpenAI API, and viewing the resulting traces in W&B.

Import and initialize autolog

First, import autolog from wandb.integration.openai and initialize it. This sets up the W&B run that captures every subsequent OpenAI API call.
import os
import openai
from wandb.integration.openai import autolog

autolog({"project": "gpt5"})
You can optionally pass a dictionary with arguments that wandb.init() accepts to autolog. This includes a project name, team name, entity, and more. For more information, see the wandb.init() API reference.

Call the OpenAI API

With autolog enabled, W&B logs each call you make to the OpenAI API automatically. You don’t need to add any logging code to your existing API calls.
os.environ["OPENAI_API_KEY"] = "XXX"

chat_request_kwargs = dict(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers"},
        {"role": "user", "content": "Where was it played?"},
    ],
)
response = openai.ChatCompletion.create(**chat_request_kwargs)

View your OpenAI API inputs and responses

After making one or more API calls, you can inspect the captured data in the W&B App. Click the W&B run link generated by autolog. This redirects you to your project workspace in the W&B App. Select a run you created to view the trace table, trace timeline, and the model architecture of the OpenAI LLM used.

Turn off autolog

Call disable() to close all W&B processes when you’re finished using the OpenAI API. This ensures that W&B flushes any pending data and doesn’t capture further API calls unintentionally.
autolog.disable()