Skip to main content
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.

Setup

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",
)

Usage

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)

What Gets Captured

AttributeDescription
ModelAny OpenRouter model string, such as openai/gpt-4o-mini, anthropic/claude-sonnet-4, or meta-llama/llama-4-maverick
MessagesInput messages array
ResponseCompletion content
Tool callsFunction/tool call inputs and outputs when using the OpenAI SDK tool-calling API
TokensPrompt, completion, and total tokens when returned by the provider
LatencyRequest duration

Run the example

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

Python

Run the Python OpenRouter tool-agent example

TypeScript

Run the TypeScript OpenRouter example