Skip to main content

Respan OpenRouter instrumentation

Trace OpenRouter calls made through the OpenAI-compatible Python client with Respan.

OpenRouter's Python usage is OpenAI-compatible, so this package delegates SDK patching to respan-instrumentation-openai and adds a package-local processor that normalizes those spans as OpenRouter spans before export.

The processor emits gen_ai.system=openrouter and gen_ai.provider.name=openrouter, canonical request tool definitions and current-turn completion tool calls, modern and legacy usage fields, precise OTEL error status, and both current and Traceloop streaming flags. It removes legacy tool aliases rather than duplicating the canonical fields.

Install

pip install respan-ai respan-instrumentation-openrouter openai

Version 0.1.0 is validated with OpenAI Python >=3.0.0,<4.0.0 and the respan-instrumentation-openai >=1.2.1,<2.0.0 delegate surface. These bounds are intentional because OpenRouter's stream bridge integrates with that delegate's current lifecycle hooks.

Environment

Variable Required Description
RESPAN_API_KEY Yes Respan API key for trace export.
RESPAN_BASE_URL No Defaults to https://api.respan.ai/api.
OPENROUTER_API_KEY Yes OpenRouter API key for direct OpenRouter calls.
OPENROUTER_BASE_URL No Defaults to https://openrouter.ai/api/v1.
OPENROUTER_MODEL No Defaults to openai/gpt-4o-mini.

The examples can also run through a Respan gateway configuration by providing OPENROUTER_USE_RESPAN_GATEWAY=true, RESPAN_GATEWAY_API_KEY, RESPAN_GATEWAY_BASE_URL, and RESPAN_MODEL. Without direct OpenRouter or explicit gateway configuration, the example set uses a local OpenAI-compatible mock so instrumentation and Respan export can still be verified.

Usage

import os

from openai import OpenAI
from respan import Respan, workflow
from respan_instrumentation_openrouter import OpenRouterInstrumentor

respan = Respan(
    api_key=os.environ["RESPAN_API_KEY"],
    base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
    instrumentations=[OpenRouterInstrumentor()],
)

client = OpenAI(
    api_key=os.environ["OPENROUTER_API_KEY"],
    base_url=os.getenv("OPENROUTER_BASE_URL", "https://openrouter.ai/api/v1"),
)


@workflow(name="openrouter_chat_example")
def run_chat() -> str:
    response = client.chat.completions.create(
        model=os.getenv("OPENROUTER_MODEL", "openai/gpt-4o-mini"),
        messages=[
            {
                "role": "user",
                "content": "Reply with one concise sentence about observability.",
            }
        ],
    )
    return response.choices[0].message.content or ""


try:
    print(run_chat())
finally:
    respan.flush()
    respan.shutdown()

OpenRouterInstrumentor(normalize_all_openai_spans=True) is the default because the package is intended for OpenRouter-specific OpenAI-compatible clients. Set normalize_all_openai_spans=False if the same process also emits regular OpenAI spans and only spans with OpenRouter URL markers should be rewritten.

Set capture_content=False to retain provider, model, usage, stream, lifecycle, and error data while removing prompt, completion, tool-definition, and tool-call content. Captured chat content is secret-redacted and bounded by UTF-8 bytes; embedding vectors remain intact under the shared span contract.

Metadata

Release files for respan-instrumentation-openrouter 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for respan-instrumentation-openrouter 0.1.1
File Size Uploaded
respan_instrumentation_openrouter-0.1.1.tar.gz 18.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for respan-instrumentation-openrouter 0.1.1
File Interpreter ABI Platform
respan_instrumentation_openrouter-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 37.4 kB

Release files / respan_instrumentation_openrouter-0.1.1.tar.gz

Download URL respan_instrumentation_openrouter-0.1.1.tar.gz
Size 18.3 kB
Tags Source
SHA-256 checksum
How to use checksums
96f423bc07f0d807f5e8972e622389c3aa34289e5baef6dd9e2a174455c3412b
BLAKE2b-256 checksum
How to use checksums
462e013885ad9aa98cf6c7a935b0e0c0d68c39cd93fe6b041178b9b95cc45776
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / respan_instrumentation_openrouter-0.1.1-py3-none-any.whl

Download URL respan_instrumentation_openrouter-0.1.1-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72e289f86343ba8cbb55ed4ce2cfc14807c8d8657d83dab1ca086f77af1da9b6
BLAKE2b-256 checksum
How to use checksums
be6d4b0e43a426d8981a26de269d784debeaa6dfd33eb7f0c9940035974d9224
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page