Skip to content

OpenAI Integration

Using Invariant Gateway with OpenAI

With just a few changes in your OpenAI client setup, you can start using the Invariant Gateway to visualize and debug your traces.

Getting the Invariant API Key

First, you need to obtain your Invariant API key. This key is essential for authenticating your requests to the Invariant Gateway.

Visit the Explorer Documentation to learn how to obtain your own API key.

Setup OpenAI API Key

In your runtime environment, set the OpenAI API key as an environment variable. This is necessary for the OpenAI client to authenticate requests.

export OPENAI_API_KEY={your-openai-api-key}

Code

You can now use the Invariant Gateway with your Anthropic client as follows:

from httpx import Client
from openai import OpenAI

http_client = Client(
    headers={
        "Invariant-Authorization": "Bearer {your-invariant-api-key}"
    },
)
openai_client = OpenAI(
    http_client=http_client,
    base_url="https://explorer.invariantlabs.ai/api/v1/gateway/{your-dataset-name}/openai",
)

result = openai_client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "user", "content": "What is the capital of France?"},
    ],
)
print("result: ", result)

This code will push your trace to the Invariant Explorer where it will be available under the dataset name used above, i.e. your-dataset-name.

Explore Other Integrations