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

# OpenRouter

> Trace OpenRouter calls through the OpenAI-compatible SDK

OpenRouter is OpenAI-compatible: use the standard `openai` SDK with the OpenRouter base URL (`https://openrouter.ai/api/v1`). traceroot captures these calls through the existing OpenAI instrumentation — no new OpenRouter-specific instrumentor or enum is required. The same approach works for xAI, Moonshot, Z.AI and other [OpenAI-compatible providers](https://traceroot.ai/docs/integrations/openai-compatible.md).

## Install

> [tab] Python
>
> ```bash theme={null}
> pip install traceroot openai
> ```

> [tab] TypeScript
>
> ```bash theme={null}
> npm install @traceroot-ai/traceroot openai
> ```

## Set up

> [tab] Python
>
> ```python theme={null}
> import os
> import openai
> import traceroot
> from traceroot import Integration
>
> traceroot.initialize(integrations=[Integration.OPENAI])
>
> client = openai.OpenAI(
>     api_key=os.environ["OPENROUTER_API_KEY"],
>     base_url="https://openrouter.ai/api/v1",
> )
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import OpenAI from 'openai';
> import { TraceRoot } from '@traceroot-ai/traceroot';
>
> TraceRoot.initialize({
>   instrumentModules: { openAI: OpenAI },
> });
>
> const openai = new OpenAI({
>   apiKey: process.env.OPENROUTER_API_KEY,
>   baseURL: 'https://openrouter.ai/api/v1',
> });
> ```

## Usage

> [tab] Python
>
> ```python theme={null}
> import os
> import openai
>
> client = openai.OpenAI(
>     api_key=os.environ["OPENROUTER_API_KEY"],
>     base_url="https://openrouter.ai/api/v1",
> )
>
> # This OpenRouter call is traced by traceroot's OpenAI integration
> response = client.chat.completions.create(
>     model="openai/gpt-4o-mini",
>     messages=[
>         {"role": "system", "content": "You are a helpful assistant."},
>         {"role": "user", "content": "What is the capital of France?"},
>     ],
> )
>
> print(response.choices[0].message.content)
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import OpenAI from 'openai';
>
> const openai = new OpenAI({
>   apiKey: process.env.OPENROUTER_API_KEY,
>   baseURL: 'https://openrouter.ai/api/v1',
> });
>
> // This OpenRouter call is traced by traceroot's OpenAI integration
> const response = await openai.chat.completions.create({
>   model: 'openai/gpt-4o-mini',
>   messages: [
>     { role: 'system', content: 'You are a helpful assistant.' },
>     { role: 'user', content: 'What is the capital of France?' },
>   ],
> });
>
> console.log(response.choices[0].message.content);
> ```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `ChatCompletion` (`OpenAI Chat Completions` on TypeScript).
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Model | Any OpenRouter model string, such as `openai/gpt-4o-mini`, `anthropic/claude-sonnet-4`, or `meta-llama/llama-4-maverick` |
| Messages | Input messages array |
| Response | Completion content |
| Tool calls | Function/tool call inputs and outputs when using the OpenAI SDK tool-calling API |
| Tokens | Prompt, completion, and total tokens when returned by the provider |
| Cost | Calculated when the routed model ID matches traceroot's pricing table (for example, supported `openai/...`, `anthropic/...`, or `google/...` IDs); otherwise tokens are captured without cost |
| Latency | Request duration |

## Limitations

* OpenRouter requests are traced through the standard OpenAI integration; token pricing requires provider-returned usage headers or recognized model mappings.

## Run the example

Clone the repo and run a complete OpenRouter agent end-to-end.

> [card] [Python](https://github.com/traceroot-ai/traceroot/tree/main/examples/python/openrouter-tool-agent)
>
> Run the Python OpenRouter tool-agent example

> [card] [TypeScript](https://github.com/traceroot-ai/traceroot/tree/main/examples/typescript/openrouter)
>
> Run the TypeScript OpenRouter example

## Next steps

> [card] [Python SDK Reference](https://traceroot.ai/docs/tracing/python-sdk.md)
>
> Explore the full Python SDK reference and instrumentation options.

> [card] [Cost Tracking](https://traceroot.ai/docs/tracing/cost-tracking.md)
>
> Learn how traceroot calculates token costs and tracks model spend.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.