Skip to main content
OpenRouter exposes many models behind an OpenAI-compatible API. Moda captures OpenRouter traffic through the standard openai client pointed at https://openrouter.ai/api/v1 — there is no OpenRouter-specific setup beyond the base URL.

Prerequisites

  • A Moda API key (created in the dashboard at Settings → Ingestion keys; shown once at creation)
  • An OpenRouter API key
  • Python 3.10 or later, or Node.js 18 or later
  • The openai package (version 4.0.0 or later in Node.js)

Setup

1

Install the packages

2

Point the OpenAI client at OpenRouter

Initialize Moda first, then construct the client with the OpenRouter base URL. Flush before your process exits so buffered data is sent.
In Node.js, Moda.init() is async and the API key argument is required (it is not read from MODA_API_KEY). If Moda.init() is not awaited, calls made before initialization completes are not captured.
3

Verify in the dashboard

Run the script, then open Conversations in the dashboard. You should see a conversation with ID session_123 containing the user message and the assistant reply, with token counts when OpenRouter returns usage data.

Model naming

OpenRouter model IDs use the provider/model format, for example anthropic/claude-3-haiku or google/gemini-2.0-flash-001. How the model is recorded differs by SDK:
  • Python: the SDK detects OpenRouter from the base URL and records the model without the provider prefix (anthropic/claude-3-haiku is recorded as claude-3-haiku).
  • Node.js: the model is recorded as sent and returned, keeping the provider/model format.
You can switch models between calls in the same session; calls stay grouped as long as the conversation ID is the same.

Streaming

Streaming works the same as with OpenAI. The SDK records the complete response after the stream finishes, so consume the entire stream.

Tool use

Tool calls made through the openai client go through the same chat completions instrumentation as OpenAI and are captured the same way, when the underlying model supports tools. See the OpenAI tool use example — only the base_url and model name change.

Direct API calls with manual tracing (Node.js)

If you call the OpenRouter HTTP API directly instead of using the openai client, wrap the call with Moda.withLLMCall() (Node.js only) to capture it:
Node.js
There is no Python equivalent of withLLMCall; from Python, use the HTTP API or OpenTelemetry for direct API calls.

Support matrix

Troubleshooting

No conversations appear in the dashboard. Initialize Moda before constructing the client, and call moda.flush() (Python) or await Moda.flush() (Node.js) before the process exits. In Node.js, confirm Moda.init() is awaited. Token usage is missing. Moda records token counts only when the OpenRouter response includes a usage object. Whether usage is returned depends on OpenRouter and the upstream provider, not on Moda. Model names look different between Python and Node.js apps. Expected — the Python SDK strips the provider/ prefix, the Node.js SDK does not. See Model naming.

Next steps

  • OpenAI — the underlying client instrumentation, including tool use.
  • Node.js SDK — init options, scoped context, and withLLMCall reference.
  • Python SDK — init options and context managers.
  • HTTP API — send events directly when no SDK path fits.