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

# Migrating from Direct API Calls

> How to migrate your existing LLM application to use Muxx.

This guide shows how to add Muxx to an existing application with minimal changes.

## Before You Start

You'll need:

* A Muxx account
* Your Muxx API key
* 5 minutes

## Gateway Migration (Fastest)

The gateway approach requires just a base URL change.

### Before

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

client = OpenAI()  # Uses api.openai.com

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

### After

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

client = OpenAI(
    base_url="https://gateway.muxx.dev/v1",
    default_headers={
        "X-Muxx-Api-Key": "muxx_sk_live_xxxxxxxxxxxx"
    }
)

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

**Changes required:** 2 lines

## SDK Migration

For deeper tracing, use the SDK wrapper.

### Before

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

client = OpenAI()

def summarize(text: str) -> str:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": f"Summarize: {text}"}]
    )
    return response.choices[0].message.content
```

### After

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

muxx = Muxx()
client = muxx.wrap(OpenAI())  # Wrap the client

def summarize(text: str) -> str:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": f"Summarize: {text}"}]
    )
    return response.choices[0].message.content
```

**Changes required:** 3 lines (import, init, wrap)

## Adding Traces

Once migrated, optionally add traces for better organization:

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

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

@trace("document-processing")
def process_document(doc: str) -> dict:
    summary = summarize(doc)
    entities = extract_entities(doc)
    return {"summary": summary, "entities": entities}

def summarize(text: str) -> str:
    response = client.chat.completions.create(...)
    return response.choices[0].message.content

def extract_entities(text: str) -> list:
    response = client.chat.completions.create(...)
    return parse_entities(response)
```

## Environment Variables

Store keys in environment variables:

```bash theme={null}
# .env
OPENAI_API_KEY=sk-...
MUXX_API_KEY=muxx_sk_live_...
```

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

# Both read from environment automatically
muxx = Muxx()
client = muxx.wrap(OpenAI())
```

## Rollback Plan

If you need to rollback:

1. **Gateway:** Remove base\_url and headers
2. **SDK:** Remove muxx.wrap() call

Your application will work as before, just without Muxx logging.

## Gradual Migration

Migrate gradually by project or feature:

1. Start with non-critical features
2. Verify logs appear in dashboard
3. Expand to more features
4. Enable for production

## Troubleshooting

### Logs not appearing

1. Check API key is correct
2. Verify project has provider keys configured
3. Check network allows gateway.muxx.dev

### Increased latency

1. Gateway adds minimal overhead
2. Enable caching for repeated queries
3. Check if rate limited
