> ## Documentation Index
> Fetch the complete documentation index at: https://langwatch.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# OpenRouter Integration

> Instrument OpenRouter model calls in Go with LangWatch to compare models, track quality, and run AI agent evaluations.

[OpenRouter](https://openrouter.ai) provides a unified API to access LLMs from many providers. LangWatch traces calls made through OpenRouter's OpenAI-compatible endpoint by reusing the [OpenAI](https://langwatch.ai/docs/integration/go/integrations/open-ai.md) `otelopenai` middleware.

## Setup

You will need an OpenRouter API key from your [OpenRouter settings](https://openrouter.ai/keys).

```bash theme={null}
export LANGWATCH_API_KEY="your-langwatch-api-key"
export OPENROUTER_API_KEY="your-openrouter-api-key"
```

## Installation

```bash theme={null}
go get github.com/langwatch/langwatch/sdks/go
go get github.com/langwatch/langwatch/sdks/go/instrumentation/openai
go get github.com/openai/openai-go/v3
```

## Usage

> [!NOTE]
>
> 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:

```go theme={null}
package main

import (
	"context"
	"log"
	"os"

	langwatch "github.com/langwatch/langwatch/sdks/go"
	otelopenai "github.com/langwatch/langwatch/sdks/go/instrumentation/openai"

	"github.com/openai/openai-go/v3"
	"github.com/openai/openai-go/v3/option"
	"go.opentelemetry.io/otel"
	semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

func main() {
	ctx := context.Background()

	// Set up the LangWatch exporter (reads LANGWATCH_API_KEY from env).
	exporter, err := langwatch.NewExporter(ctx)
	if err != nil {
		log.Fatalf("failed to create exporter: %v", err)
	}
	tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
	otel.SetTracerProvider(tp)
	defer tp.Shutdown(ctx) // Critical: ensures traces are flushed.

	// Create an OpenRouter client via the OpenAI-compatible API.
	client := openai.NewClient(
		option.WithBaseURL("https://openrouter.ai/api/v1"),
		option.WithAPIKey(os.Getenv("OPENROUTER_API_KEY")),
		option.WithMiddleware(otelopenai.Middleware("my-app",
			otelopenai.WithGenAIProvider(semconv.GenAIProviderNameKey.String("openrouter")),
		)),
	)

	response, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
		Model: "anthropic/claude-sonnet-5",
		Messages: []openai.ChatCompletionMessageParamUnion{
			openai.UserMessage("Hello via OpenRouter!"),
		},
	})
	if err != nil {
		log.Fatalf("OpenRouter API call failed: %v", err)
	}

	log.Printf("Response: %s", response.Choices[0].Message.Content)
}
```

> [!NOTE]
>
> The middleware captures input and output content **by default**. Pass `otelopenai.WithDataCapture(langwatch.DataCaptureNone)` to opt out.

> [!WARNING]
>
> The `defer tp.Shutdown(ctx)` call is essential. Without it, traces buffered in memory will be lost
> when your application exits.

> [!NOTE]
>
> 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.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.