Skip to main content
Use this guide when you want to test a candidate model on the prompts your app already sends, comparing answer quality, generation time, and cost before you switch.

What you’re building

Terminal output of replay.ts comparing 6 generation IDs: each row prints generation time before and after, candidate cost as a multiple of production, and a blind judge verdict with a one-sentence reason, ending with candidate 3, production 2, tie 1, and total cost 0.12x production

Six prompts from an app log, rerun on a candidate model. Each entry shows generation time, cost relative to production, and which answer a judge model preferred.

With Input & Output Logging turned on, OpenRouter privately stores the full prompt and completion for each request. You can fetch them later by generation ID through GET /api/v1/generation/content without storing prompt bodies in your own logs. By the end, you will have a replay.ts script that:
  1. Reads generation IDs from your app’s log file.
  2. Fetches the stored prompt and production answer for each ID.
  3. Reruns the same prompt on a candidate model.
  4. Compares generation time and cost as a multiple of production.
  5. Asks a judge model from a third lab to pick the better answer blind.
Building with a coding agent? Paste this page’s URL into Claude Code, Codex, or Cursor and ask it to find the generation IDs in your app logs and run this script against them.

Before you start

You need:
  • A user-owned OpenRouter API key in OPENROUTER_API_KEY (org-level keys without a creator cannot read stored content).
  • Input & Output Logging turned on under Observability settings before your app sends the requests you want to replay.
  • An app that writes each OpenRouter response id to its logs.
  • Bun, or Node.js 20 or later with a TypeScript runner.
Input & Output Logging limits:
  • Only requests sent while the setting is on have stored content.
  • Requests sent to eu.openrouter.ai or us.openrouter.ai are not stored.
  • In an organization, org admins can read stored content for all requests. Other members can read only requests made with API keys they created.
  • Multimodal attachments (images, audio, video, and files) are redacted before storage. This script skips multimodal prompts; to replay them, supply the original media from your app.
  • Stored records do not carry over per-request provider routing or retention settings (such as zdr or data_collection: "deny"), or inference parameters like response_format and tools.
The example app summarizes support tickets in one sentence and assigns a severity from P1 to P4: See each model’s page on OpenRouter for pricing.

Step 1: Log generation IDs in your app

Every OpenRouter response includes an id that starts with gen-. OpenRouter has no endpoint that lists your generations, so write each response id to your app logs. You do not need to log full prompts yourself. A plain text log with one gen- ID per request is enough:
logs/app.log

Step 2: Set up the script and API helpers

Create replay.ts and add the API helpers along with readGenerationIds(), which pulls every unique gen- ID from your log file. Metadata for a request that just finished can take a few seconds to appear on /generation?id=..., so get() retries 404 responses on /generation? before throwing an error. get() throws an ApiError with the HTTP status so the caller can skip 404 rows for unstored prompts while still failing fast on auth or server errors. If production enforces per-request retention or routing rules (such as provider.zdr or provider.data_collection: "deny"), set them in PROVIDER_PREFERENCES or enforce them at the workspace level with Guardrails before running the script so neither the candidate nor the judge sends production data to providers you excluded.
replay.ts

Step 3: Fetch stored prompts and rerun on the candidate

Call Get stored content for a generation (GET /api/v1/generation/content?id=<generation id>) to fetch what production sent and returned. The response looks like this:
Add fetchStoredRun() to read the stored chat messages and production completion, skipping requests that cannot be replayed fairly through /chat/completions:
  • 404 responses (prompts sent before logging was turned on or excluded from logging).
  • Failed generations or records without a string output.completion.
  • Non-chat /completions records (no input.messages array).
  • Multimodal prompts whose image, audio, video, or file inputs were redacted before storage.
Then add rerunPrompt() to send the plain-text messages to CANDIDATE. This example assumes plain-text prompts; if production relies on settings like response_format, tools, or temperature, pass those same parameters in rerunPrompt() so the candidate is judged under the same constraints.
replay.ts

Step 4: Compare generation time and cost

Call Get generation (GET /api/v1/generation?id=<generation id>) for both the production ID and the candidate ID to read generation_time (in milliseconds) and total_cost. For BYOK requests (is_byok: true), the provider bills inference separately from OpenRouter’s charge, so fetchMetrics() adds upstream_inference_cost (or treats cost as unknown when that estimate is missing) and flags those rows so the report labels them as estimates.
replay.ts

Step 5: Judge both answers blind

Send the original prompt and both answers to JUDGE using structured outputs. judgeAnswers() uses the same chat() helper as rerunPrompt(), so any PROVIDER_PREFERENCES you configure apply to the judge call as well. Before replaying sensitive production rows, make sure those preferences or your workspace guardrails cover both models, or exclude those IDs from the log. LLM judges can favor whichever answer appears first when two answers are close. Passing candidateIsA and flipping it on each prompt alternates the A/B order, then maps "A" or "B" back to "candidate" or "production".
replay.ts

Step 6: Run the replay and read the report

Add main() to loop through the generation IDs, print one block per prompt, and print a summary at the end:
replay.ts
Run the script against your log file:
Six prompts show that the script works, not whether to switch models. Judge verdicts also vary between runs: two runs over these six prompts gave the candidate 3 wins, 2 losses, and 1 tie, then 5 wins, 1 loss, and 0 ties. Replay a few hundred IDs and read the prompts where production wins before changing your production model.

Troubleshooting

Check your work

Run bun replay.ts logs/app.log and confirm it prints one entry per generation ID with time, cost, and better lines, followed by a summary. Inspect a few prompts where production wins to see where the candidate falls short before you switch models.