> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-evaltables.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Use W&B with the OpenAI API to log and monitor chat completions, fine-tuning jobs, and token usage metrics.

# OpenAI API

export const ColabLink = ({url}) => <a href={url} target="_blank" rel="noopener noreferrer" className="colab-link">
    <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
      <path d="M14.25.18l.9.2.73.26.59.3.45.32.34.34.25.34.16.33.1.3.04.26.02.2-.01.13V8.5l-.05.63-.13.55-.21.46-.26.38-.3.31-.33.25-.35.19-.35.14-.33.1-.3.07-.26.04-.21.02H8.77l-.69.05-.59.14-.5.22-.41.27-.33.32-.27.35-.2.36-.15.37-.1.35-.07.32-.04.27-.02.21v3.06H3.17l-.21-.03-.28-.07-.32-.12-.35-.18-.36-.26-.36-.36-.35-.46-.32-.59-.28-.73-.21-.88-.14-1.05-.05-1.23.06-1.22.16-1.04.24-.87.32-.71.36-.57.4-.44.42-.33.42-.24.4-.16.36-.1.32-.05.24-.01h.16l.06.01h8.16v-.83H6.18l-.01-2.75-.02-.37.05-.34.11-.31.17-.28.25-.26.31-.23.38-.2.44-.18.51-.15.58-.12.64-.1.71-.06.77-.04.84-.02 1.27.05zm-6.3 1.98l-.23.33-.08.41.08.41.23.34.33.22.41.09.41-.09.33-.22.23-.34.08-.41-.08-.41-.23-.33-.33-.22-.41-.09-.41.09zm13.09 3.95l.28.06.32.12.35.18.36.27.36.35.35.47.32.59.28.73.21.88.14 1.04.05 1.23-.06 1.23-.16 1.04-.24.86-.32.71-.36.57-.4.45-.42.33-.42.24-.4.16-.36.09-.32.05-.24.02-.16-.01h-8.22v.82h5.84l.01 2.76.02.36-.05.34-.11.31-.17.29-.25.25-.31.24-.38.2-.44.17-.51.15-.58.13-.64.09-.71.07-.77.04-.84.01-1.27-.04-1.07-.14-.9-.2-.73-.25-.59-.3-.45-.33-.34-.34-.25-.34-.16-.33-.1-.3-.04-.25-.02-.2.01-.13v-5.34l.05-.64.13-.54.21-.46.26-.38.3-.32.33-.24.35-.2.35-.14.33-.1.3-.06.26-.04.21-.02.13-.01h5.84l.69-.05.59-.14.5-.21.41-.28.33-.32.27-.35.2-.36.15-.36.1-.35.07-.32.04-.28.02-.21V6.07h2.09l.14.01.21.03zm-6.47 14.25l-.23.33-.08.41.08.41.23.33.33.23.41.08.41-.08.33-.23.23-.33.08-.41-.08-.41-.23-.33-.33-.23-.41-.08-.41.08z" />
    </svg>
    Try in Colab
  </a>;

<ColabLink url="https://colab.research.google.com/github/wandb/examples/blob/master/colabs/openai/OpenAI_API_Autologger_Quickstart.ipynb" />

Use the W\&B OpenAI API integration to log requests, responses, token counts, and model metadata for all OpenAI models, including fine-tuned models. Use this integration if you call the OpenAI API and want visibility into your 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.

<Note>
  See the [OpenAI fine-tuning integration](./openai-fine-tuning) to learn how to use W\&B to track your fine-tuning experiments, models, and datasets and share your results with your colleagues.
</Note>

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541-evaltables/hsXNuiRRG1GLq0zd/images/integrations/open_ai_autolog.png?fit=max&auto=format&n=hsXNuiRRG1GLq0zd&q=85&s=ae5c2b6e683715c12dcf02c8f97e1f1c" alt="W&B trace view showing OpenAI API requests, responses, and token usage logged automatically" width="2560" height="1292" data-path="images/integrations/open_ai_autolog.png" />
</Frame>

## 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:

```bash theme={null}
pip install openai==0.28.1
```

## Use the OpenAI Python API

The following steps explain how to enable autologging, call the OpenAI API, and view 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.

```python theme={null}
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](/models/ref/python/functions/init).

### 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.

```python theme={null}
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 W\&B.

Click the W\&B [run](/models/runs/) link generated by `autolog`. This redirects you to your project workspace in W\&B.

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.

```python theme={null}
autolog.disable()
```
