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

# OpenRouter (Python)

> Trace OpenRouter API calls in Python

[OpenRouter](https://openrouter.ai) provides access to 200+ LLM models through a single API. Use the Fallom SDK for full tracing with Model A/B Testing and Prompt Management.

## Installation

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

## Quick Start

OpenRouter uses the OpenAI-compatible API:

<Warning>
  **Import order matters!** Initialize Fallom **before** importing OpenAI.
</Warning>

```python theme={null}
import os

# Import and initialize Fallom FIRST
import fallom
fallom.init(api_key=os.environ["FALLOM_API_KEY"])

# NOW import OpenAI
from openai import OpenAI

# Create client with OpenRouter base URL
client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=os.environ["OPENROUTER_API_KEY"]
)

# Set session context (config_key, session_id, customer_id)
fallom.trace.set_session("my-app", "session-123", customer_id="user-456")

# Use any OpenRouter model
response = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}]
)

print(response.choices[0].message.content)
```

## Available Models

OpenRouter supports 200+ models:

```python theme={null}
# OpenAI
model="openai/gpt-4o"
model="openai/gpt-4o-mini"

# Anthropic
model="anthropic/claude-sonnet-4-20250514"
model="anthropic/claude-3-opus"

# Google
model="google/gemini-1.5-pro"
model="google/gemini-1.5-flash"

# Meta
model="meta-llama/llama-3.1-70b-instruct"

# And many more...
```

## Model A/B Testing

Test different models from different providers:

```python theme={null}
import fallom
from fallom import models

fallom.init(api_key="your-fallom-api-key")

from openai import OpenAI

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key="your-openrouter-key"
)

# Get assigned model - could be GPT-4o, Claude, Gemini, etc.
model_id = models.get("my-experiment", "session-123", fallback="openai/gpt-4o-mini")

fallom.trace.set_session("my-experiment", "session-123")

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

## Streaming

```python theme={null}
import fallom
fallom.init(api_key="your-fallom-api-key")

from openai import OpenAI

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key="your-openrouter-key"
)

fallom.trace.set_session("my-agent", "session-123")

stream = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Write a poem."}],
    stream=True
)

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

## What Gets Traced

| Field       | Description                             |
| ----------- | --------------------------------------- |
| Model       | Full model path (e.g., `openai/gpt-4o`) |
| Duration    | Total request time (ms)                 |
| Tokens      | Prompt, completion tokens               |
| Cost        | Calculated from token usage             |
| Prompts     | All input messages                      |
| Completions | Model response                          |
| Session     | Your config key + session ID            |

## SDK vs Broadcast

| Feature           | SDK (this page) | Broadcast |
| ----------------- | --------------- | --------- |
| Tracing           | ✅               | ✅         |
| Token tracking    | ✅               | ✅         |
| Cost tracking     | ✅               | ✅         |
| Session grouping  | ✅               | ✅         |
| Model A/B Testing | ✅               | ❌         |
| Prompt Management | ✅               | ❌         |

## Next Steps

<CardGroup cols={2}>
  <Card title="OpenRouter Broadcast" icon="broadcast-tower" href="/integrations/openrouter-broadcast">
    SDK-free tracing via OpenRouter.
  </Card>

  <Card title="Model A/B Testing" icon="flask" href="/model-testing">
    Test models across providers.
  </Card>
</CardGroup>
