Skip to main content
OpenRouter provides a unified API to access LLMs from many providers. LangWatch traces calls made through OpenRouter’s OpenAI-compatible endpoint by reusing the OpenAI otelopenai middleware.

Setup

You will need an OpenRouter API key from your OpenRouter settings.

Installation

Usage

Set LANGWATCH_API_KEY and OPENROUTER_API_KEY environment variables before running.
The key difference with OpenRouter is the model name, which is prefixed with the provider (e.g., anthropic/claude-sonnet-4-20250514). OpenRouter has no registered gen_ai.provider.name, so pass a custom value:
The middleware captures input and output content by default. Pass otelopenai.WithDataCapture(langwatch.DataCaptureNone) to opt out.
The defer tp.Shutdown(ctx) call is essential. Without it, traces buffered in memory will be lost when your application exits.
Using OpenRouter is a great way to experiment with different models without changing your core instrumentation logic. All calls will be traced by LangWatch, regardless of the underlying model you choose.

Check it worked

Run the program with go run .. The deferred tp.Shutdown(ctx) flushes the spans when main returns. Then open your project in LangWatch: a new trace appears in the traces list within a few seconds, with the OpenRouter call as an LLM span carrying its input, output and token counts. If no trace appears, check that LANGWATCH_API_KEY is set in the environment the program runs in.
Last modified on October 29, 2026