Skip to content

Add OpenRouter sample: prompt batch with cached retries - #520

Open
DABH wants to merge 2 commits into
mainfrom
dabh/openrouter
Open

Add OpenRouter sample: prompt batch with cached retries#520
DABH wants to merge 2 commits into
mainfrom
dabh/openrouter

Conversation

@DABH

@DABH DABH commented Sep 11, 2026

Copy link
Copy Markdown

Adds an openrouter sample: a Workflow that fans one Activity out per prompt through OpenRouter's Auto Router and returns answer, model, and reported cost per prompt.

The Activity uses the openai client pointed at OpenRouter with maxRetries: 0, classifies 4xx as non-retryable and 408/429/5xx as retryable, passes Retry-After through as nextRetryDelay, heartbeats, checks for OpenRouter's 200-with-error body, and sends X-OpenRouter-Cache: true so a retried identical request is served from OpenRouter's cache at $0. A --fail-once flag on the client demonstrates that; verified live with a HIT at $0 on attempt 2.

Tests use a fake fetch and a fake Activity; no API key needed. Registered in the CI project list and the sample list. This is the TypeScript port of temporalio/samples-python#366 (prompt_batch); the budget_gate scenario stays Python-only for now.

Call OpenRouter from an Activity with the openai client pointed at OpenRouter,
client retries off so Temporal owns every attempt, error classification with
Retry-After as the next retry delay, heartbeats, and OpenRouter response
caching so a retried identical request is billed at zero. The Workflow fans a
prompt batch out under bounded concurrency and reports skipped prompts instead
of failing the batch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant