Skip to content

OpenRouter (Node)

Why OpenRouter

  • OpenRouter provides usage.total_cost in responses. RunForge treats this as provider‑authoritative (costSource="provider"). Zero configuration for pricing.

Install

pnpm add openai

Where to install wrapper

  • Server side where you make OpenRouter calls (e.g., API routes, server actions).

Minimal example

import OpenAI from 'openai'
import { RunForge } from '../../sdk-ts/index'

const client = new OpenAI({ baseURL: 'https://openrouter.ai/api/v1', apiKey: process.env.OPENROUTER_API_KEY! })
const rf = new RunForge({ apiKey: process.env.RUNFORGE_API_KEY!, endpoint: process.env.RUNFORGE_ENDPOINT, projectId: process.env.RUNFORGE_PROJECT_ID })

const res = await rf.track({ model: 'openai/gpt-4o-mini', experiment: 'openrouter-demo' }, () =>
  client.chat.completions.create({ model: 'openai/gpt-4o-mini', messages: [{ role: 'user', content: 'hi' }] })
)

Privacy

  • Do not send prompts/outputs to RunForge. Only usage metadata is transmitted.

Streaming

  • Use the OpenAI‑compatible streaming API. Final usage.total_cost arrives on the last chunk.

Retries & idempotency

  • If you retry application calls, set a stable runId and include it in metadata; RunForge is idempotent by run id.

Troubleshooting

  • Keys: OPENROUTER_API_KEY, RUNFORGE_API_KEY, RUNFORGE_PROJECT_ID.
  • CORS: Make calls server‑side; do not expose keys client‑side.
  • Missing usage: Ensure you are using OpenRouter’s v1 API; some proxies may strip usage fields.
  • Clock skew: Metrics use client timestamps; skew only affects ordering in charts slightly.