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

# OpenRouter

> Using Moda with OpenRouter

## Overview

[OpenRouter](https://openrouter.ai) provides access to multiple LLM providers through a unified, OpenAI-compatible API. Since OpenRouter uses the same interface as OpenAI, Moda automatically tracks your OpenRouter calls with no extra configuration.

## Setup with Moda SDK

<CodeGroup>
  ```python Python theme={"dark"}
  import moda
  from openai import OpenAI

  moda.init("YOUR_MODA_API_KEY")

  openrouter = OpenAI(
      base_url="https://openrouter.ai/api/v1",
      api_key="YOUR_OPENROUTER_API_KEY",
      default_headers={
          "HTTP-Referer": "https://your-app.com",
          "X-Title": "Your App Name",
      },
  )

  moda.conversation_id = "session_123"

  response = openrouter.chat.completions.create(
      model="anthropic/claude-3.5-sonnet",
      messages=[{"role": "user", "content": "Hello!"}]
  )

  moda.flush()
  ```

  ```typescript Node.js theme={"dark"}
  import { Moda } from 'moda-ai';
  import OpenAI from 'openai';

  await Moda.init('YOUR_MODA_API_KEY');

  const openrouter = new OpenAI({
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: 'YOUR_OPENROUTER_API_KEY',
    defaultHeaders: {
      'HTTP-Referer': 'https://your-app.com',
      'X-Title': 'Your App Name',
    },
  });

  Moda.conversationId = 'session_123';

  const response = await openrouter.chat.completions.create({
    model: 'anthropic/claude-3.5-sonnet',
    messages: [{ role: 'user', content: 'Hello!' }]
  });

  await Moda.flush();
  ```
</CodeGroup>

<Tip>
  OpenRouter model names use the format `provider/model-name`. See the [OpenRouter models page](https://openrouter.ai/models) for all available models.
</Tip>

## Direct API with Manual Tracing

For cases where you want to call the OpenRouter API directly (without the OpenAI client), use the Node.js SDK's manual tracing:

```typescript theme={"dark"}
import { Moda } from 'moda-ai';

await Moda.init('YOUR_MODA_API_KEY');
Moda.conversationId = 'session_123';

const messages = [{ role: 'user', content: 'Hello!' }];

const result = await Moda.withLLMCall(
  { vendor: 'openrouter', type: 'chat' },
  async ({ span }) => {
    span.reportRequest({ model: 'anthropic/claude-3-sonnet', messages });

    const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${process.env.OPENROUTER_API_KEY}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ model: 'anthropic/claude-3-sonnet', messages }),
    });
    const data = await response.json();

    span.reportResponse({
      model: data.model,
      usage: data.usage,
      completions: data.choices,
    });

    return data;
  }
);
```

<Note>
  `Moda.withLLMCall()` is available in the Node.js SDK. See [Manual Tracing](/ingestion/moda-sdk-node#manual-tracing) for details.
</Note>

## Supported Features

| Feature          | Captured                                    |
| ---------------- | ------------------------------------------- |
| Chat completions | Yes                                         |
| Streaming        | Yes                                         |
| Token usage      | Yes                                         |
| Model name       | Yes (includes provider prefix)              |
| Tool use         | Yes (when the underlying model supports it) |

## Troubleshooting

**Model name shows OpenRouter prefix?**

* This is expected. OpenRouter model names include the provider prefix (e.g., `anthropic/claude-3.5-sonnet`). Moda captures the exact model name returned by OpenRouter.

**Token usage missing?**

* Some models on OpenRouter may not return token usage data. This depends on the underlying provider.

<Info>
  For full SDK documentation, see the [Python SDK](/ingestion/moda-sdk) or [Node.js SDK](/ingestion/moda-sdk-node) guides.
</Info>
