> ## Documentation Index
> Fetch the complete documentation index at: https://docs.muxx.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Integration

> Use Muxx with the OpenAI Python SDK.

The Muxx SDK provides seamless integration with the OpenAI Python SDK.

## Wrapping the Client

```python theme={null}
from muxx import Muxx
from openai import OpenAI

muxx = Muxx()
client = muxx.wrap(OpenAI())

# All calls are now automatically traced
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)
```

## Async Client

```python theme={null}
from muxx import Muxx
from openai import AsyncOpenAI

muxx = Muxx()
client = muxx.wrap(AsyncOpenAI())

response = await client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)
```

## Supported Methods

The wrapper automatically traces these methods:

| Method                        | Traced |
| ----------------------------- | ------ |
| `chat.completions.create`     | Yes    |
| `completions.create`          | Yes    |
| `embeddings.create`           | Yes    |
| `images.generate`             | Yes    |
| `audio.transcriptions.create` | Yes    |

## Streaming

Streaming is fully supported:

```python theme={null}
stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

# The complete response is logged when streaming finishes
```

## Function Calling

Function calls are automatically captured:

```python theme={null}
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                }
            }
        }
    }
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
    tools=tools
)

# Tool calls are logged with the response
```

## Adding Context

Combine with traces for better organization:

```python theme={null}
from muxx import Muxx, trace
from openai import OpenAI

muxx = Muxx()
client = muxx.wrap(OpenAI())

@trace("customer-support")
def handle_support_request(user_message: str):
    # Classify intent
    intent = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "Classify the intent: billing, technical, general"},
            {"role": "user", "content": user_message}
        ]
    )

    # Generate response
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": f"You are a {intent.choices[0].message.content} support agent"},
            {"role": "user", "content": user_message}
        ]
    )

    return response.choices[0].message.content
```

## Embeddings

Embedding calls are also traced:

```python theme={null}
embeddings = client.embeddings.create(
    model="text-embedding-3-small",
    input=["Hello world", "Goodbye world"]
)

# Token usage and latency are logged
```

## Using with Gateway

You can use the SDK wrapper with the gateway:

```python theme={null}
from muxx import Muxx
from openai import OpenAI

muxx = Muxx()

# Point to gateway for caching/rate limiting
# SDK still adds tracing
client = muxx.wrap(OpenAI(
    base_url="https://gateway.muxx.dev/v1",
    default_headers={"X-Muxx-Api-Key": muxx.api_key}
))
```
