Skip to main content

Overview

OpenRouter 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

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()
OpenRouter model names use the format provider/model-name. See the OpenRouter models page for all available models.

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:
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;
  }
);
Moda.withLLMCall() is available in the Node.js SDK. See Manual Tracing for details.

Supported Features

FeatureCaptured
Chat completionsYes
StreamingYes
Token usageYes
Model nameYes (includes provider prefix)
Tool useYes (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.
For full SDK documentation, see the Python SDK or Node.js SDK guides.