> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://respan.ai/docs/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://respan.ai/docs/_mcp/server.

# OpenRouter (tracing)

> Trace OpenRouter calls made through the OpenAI-compatible Python client with Respan.

[OpenRouter](https://openrouter.ai/) is OpenAI-compatible. Use `respan-instrumentation-openrouter` to trace OpenRouter API calls from the OpenAI client and normalize them into OpenRouter-aware Respan spans.

#### Set up Respan

Create an account at [platform.respan.ai](https://platform.respan.ai) and grab an [API key](https://platform.respan.ai/platform/gateway/api-keys).

Run `npx @respan/cli setup` to set up with your coding agent.

#### Use Respan Gateway

For LLM routing through Respan, use the [OpenRouter gateway page](/docs/gateway/openrouter).

#### Example projects

* [Python package README](https://github.com/RespanAI/respan/tree/main/python-sdks/instrumentations/respan-instrumentation-openrouter)

## Setup

#### Install packages

```bash
pip install respan-ai respan-instrumentation-openrouter openai
```

**`TypeScript`**

```bash TypeScript
npm install @respan/respan @respan/tracing @respan/instrumentation-openrouter @openrouter/sdk
```

#### Set environment variables

```bash
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
export OPENROUTER_API_KEY="YOUR_OPENROUTER_API_KEY"
```

#### Initialize and run

```python
import os

from openai import OpenAI
from respan import Respan
from respan_instrumentation_openrouter import OpenRouterInstrumentor

respan = Respan(
    api_key=os.environ["RESPAN_API_KEY"],
    app_name="openrouter-tracing",
    instrumentations=[OpenRouterInstrumentor()],
)

client = OpenAI(
    api_key=os.environ["OPENROUTER_API_KEY"],
    base_url=os.getenv("OPENROUTER_BASE_URL", "https://openrouter.ai/api/v1"),
)

response = client.chat.completions.create(
    model=os.getenv("OPENROUTER_MODEL", "openai/gpt-4o-mini"),
    messages=[
        {"role": "user", "content": "Summarize tracing with one sentence."}
    ],
)
print(response.choices[0].message.content)

respan.shutdown()
```

**`TypeScript`**

```typescript TypeScript
import OpenRouter from "@openrouter/sdk";
import { Respan } from "@respan/respan";
import { OpenRouterInstrumentor } from "@respan/instrumentation-openrouter";

const respan = new Respan({
  apiKey: process.env.RESPAN_API_KEY,
  appName: "openrouter-tracing",
  instrumentations: [new OpenRouterInstrumentor()],
});
await respan.initialize();

const client = new OpenRouter({
  apiKey: process.env.OPENROUTER_API_KEY,
});

const response = await client.chat.send({
  model: process.env.OPENROUTER_MODEL ?? "openai/gpt-4o-mini",
  messages: [{ role: "user", content: "Summarize tracing with one sentence." }],
});

console.log(response.choices[0].message.content);
await respan.shutdown();
```

#### View your trace

Open the [Traces page](https://platform.respan.ai/platform/traces) to inspect normalized OpenRouter spans.