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
openaipackage (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.
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 theprovider/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-haikuis recorded asclaude-3-haiku). - Node.js: the model is recorded as sent and returned, keeping the
provider/modelformat.
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 theopenai 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 theopenai client, wrap the call with Moda.withLLMCall() (Node.js only) to capture it:
Node.js
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 callmoda.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
withLLMCallreference. - Python SDK — init options and context managers.
- HTTP API — send events directly when no SDK path fits.