What you’re building

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.
GET /api/v1/generation/content without storing prompt bodies in your own logs.
By the end, you will have a replay.ts script that:
- Reads generation IDs from your app’s log file.
- Fetches the stored prompt and production answer for each ID.
- Reruns the same prompt on a candidate model.
- Compares generation time and cost as a multiple of production.
- Asks a judge model from a third lab to pick the better answer blind.
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
idto 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.aiorus.openrouter.aiare 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
providerrouting or retention settings (such aszdrordata_collection: "deny"), or inference parameters likeresponse_formatandtools.
- Production:
google/gemini-4-hydrogen - Candidate:
openai/gpt-6.2-sol - Judge:
anthropic/claude-sonnet-5.5
Step 1: Log generation IDs in your app
Every OpenRouter response includes anid 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
Createreplay.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:
fetchStoredRun() to read the stored chat messages and production completion, skipping requests that cannot be replayed fairly through /chat/completions:
404responses (prompts sent before logging was turned on or excluded from logging).- Failed generations or records without a string
output.completion. - Non-chat
/completionsrecords (noinput.messagesarray). - Multimodal prompts whose image, audio, video, or file inputs were redacted before storage.
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 toJUDGE 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
Addmain() to loop through the generation IDs, print one block per prompt, and print a summary at the end:
replay.ts
production wins before changing your production model.
Troubleshooting
Check your work
Runbun 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.