OpenRouter (tracing)

OpenRouter is OpenAI-compatible. Use respan-instrumentation-openrouter to trace OpenRouter API calls from the OpenAI client and normalize them into OpenRouter-aware Respan spans.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

For LLM routing through Respan, use the OpenRouter gateway page.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-openrouter openai
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$export OPENROUTER_API_KEY="YOUR_OPENROUTER_API_KEY"
3

Initialize and run

1import os
2
3from openai import OpenAI
4from respan import Respan
5from respan_instrumentation_openrouter import OpenRouterInstrumentor
6
7respan = Respan(
8 api_key=os.environ["RESPAN_API_KEY"],
9 app_name="openrouter-tracing",
10 instrumentations=[OpenRouterInstrumentor()],
11)
12
13client = OpenAI(
14 api_key=os.environ["OPENROUTER_API_KEY"],
15 base_url=os.getenv("OPENROUTER_BASE_URL", "https://openrouter.ai/api/v1"),
16)
17
18response = client.chat.completions.create(
19 model=os.getenv("OPENROUTER_MODEL", "openai/gpt-4o-mini"),
20 messages=[
21 {"role": "user", "content": "Summarize tracing with one sentence."}
22 ],
23)
24print(response.choices[0].message.content)
25
26respan.shutdown()
4

View your trace

Open the Traces page to inspect normalized OpenRouter spans.