For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Copy to LLMGithubGo to App
DocumentationIntegrationsAgent OptimizationSelf-hosting OpikSDK & API referenceOpik University
DocumentationIntegrationsAgent OptimizationSelf-hosting OpikSDK & API referenceOpik University
    • Overview
  • TypeScript
    • Opik TypeScript SDK
  • Python
      • Opik LLM Gateway
      • Kong AI Gateway
      • AISuite
      • Anannas AI
      • Helicone
      • LiteLLM
      • OpenRouter
      • Portkey
      • TrueFoundry
      • Vercel AI Gateway
    • Opik Python SDK
    • OpenTelemetry Python SDK
  • Java
    • Spring AI
    • Quarkus LangChain4j
  • .NET
  • Ruby
    • OpenTelemetry Ruby SDK
  • No-Code
    • Cursor
    • Dify
    • Flowise
    • Langflow
    • n8n
    • OpenClaw
    • OpenAI Codex
    • OpenWebUI
    • Prompt Flow
  • Multi-Language
    • OpenTelemetry
LogoLogo
Copy to LLMGithubGo to App
On this page
  • Getting started
  • Tracking OpenRouter API calls
  • Available Models
  • Supported Methods
  • Chat Completions
  • Structured Outputs
PythonGateways

Observability for OpenRouter with Opik

Was this page helpful?
Previous

Observability for Portkey with Opik

Next
Built with

This guide explains how to integrate Opik with OpenRouter using the OpenAI SDK. OpenRouter provides a unified API for accessing hundreds of AI models through a single OpenAI-compatible interface.

Getting started

First, ensure you have both opik and openai packages installed:

$pip install opik openai

You’ll also need an OpenRouter API key which you can get from OpenRouter.

Tracking OpenRouter API calls

1from opik.integrations.openai import track_openai
2from openai import OpenAI
3
4# Initialize the OpenAI client with OpenRouter base URL
5client = OpenAI(
6 base_url="https://openrouter.ai/api/v1",
7 api_key="YOUR_OPENROUTER_API_KEY"
8)
9client = track_openai(client)
10
11# Optional headers for OpenRouter leaderboard
12headers = {
13 "HTTP-Referer": "YOUR_SITE_URL", # Optional. Site URL for rankings
14 "X-Title": "YOUR_SITE_NAME" # Optional. Site title for rankings
15}
16
17response = client.chat.completions.create(
18 model="openai/gpt-4", # You can use any model available on OpenRouter
19 extra_headers=headers,
20 messages=[
21 {"role": "user", "content": "Hello, world!"}
22 ],
23 temperature=0.7,
24 max_tokens=100
25)
26
27print(response.choices[0].message.content)

Available Models

OpenRouter provides access to a wide variety of models, including many open source models from different providers.

  • OpenAI models (GPT-4o, o1, o3-mini)
  • Anthropic models (Opus, Sonnet, Haiku)
  • Google models (Gemini Pro, Flash, Flash Thinking)
  • And many open source models

You can find the complete list of available models in the OpenRouter documentation.

Supported Methods

OpenRouter supports the following methods:

Chat Completions

  • client.chat.completions.create(): Works with all models
  • Provides standard chat completion functionality
  • Compatible with the OpenAI SDK interface

Structured Outputs

  • client.beta.chat.completions.parse(): Only compatible with OpenAI models
  • For non-OpenAI models, see OpenRouter’s Structured Outputs documentation

For detailed information about available methods, parameters, and best practices, refer to the OpenRouter API documentation.