Skip to main content
This guide shows you how to use Weave to automatically trace calls to LLMs accessed through OpenRouter, so you can monitor, debug, and evaluate your application’s model usage from a single dashboard. OpenRouter is a unified interface for many LLMs, supporting both foundational models like OpenAI GPT-4, Anthropic Claude, and Google Gemini, as well as open-source models like LLama-3, Mixtral, and more. Some models are offered for free. OpenRouter offers a REST API and OpenAI SDK compatibility that Weave automatically detects and integrates with. For more details, refer to the OpenRouter quick start guide. Because Weave detects the OpenAI SDK, you can reuse existing OpenAI code with a few configuration changes. To switch your OpenAI SDK code to OpenRouter, switch out the API key to your OpenRouter API key, set base_url to https://openrouter.ai/api/v1, and set the model to one of the available chat models. When you call weave.init(), provide a project name for your traces. If you don’t specify one, Weave uses your default entity. To find or update your default entity, refer to User Settings in the W&B Models documentation. After you run the following example, Weave captures your call to OpenRouter as a trace in your Weave project.
import os
import openai
import weave

weave.init('openrouter-weave')

system_content = "You are a travel agent. Be descriptive and helpful."
user_content = "Tell me about San Francisco"

client = openai.OpenAI(
    api_key=os.environ.get("OPENROUTER_API_KEY"),
    base_url="https://openrouter.ai/api/v1",
)
chat_completion = client.chat.completions.create(
    extra_headers={
    "HTTP-Referer": "[YOUR-SITE-URL]", # Optional, for including your app on openrouter.ai rankings.
    "X-Title": "[YOUR-APP-NAME]", # Optional. Shows in rankings on openrouter.ai.
    },
    model="meta-llama/llama-3.1-8b-instruct:free",
    messages=[
        {"role": "system", "content": system_content},
        {"role": "user", "content": user_content},
    ],
    temperature=0.7,
    max_tokens=1024,
)
response = chat_completion.choices[0].message.content
print("Model response:\n", response)
While this is a basic example to get started, see the OpenAI guide for more details on how to integrate Weave with your own functions for more complex use cases.