Skip to content

Add OpenRouter samples: prompt batch and budget gate - #366

Open
DABH wants to merge 1 commit into
mainfrom
dabh/openrouter
Open

Add OpenRouter samples: prompt batch and budget gate#366
DABH wants to merge 1 commit into
mainfrom
dabh/openrouter

Conversation

@DABH

@DABH DABH commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Adds an openrouter sample with two scenarios, plus OpenRouter as a model provider for the OpenAI Agents SDK sample.

prompt_batch fans one Activity out per prompt with OpenRouter's Auto Router and returns answer, model, and reported cost per prompt. The Activity uses the openai client pointed at OpenRouter with max_retries=0, classifies 4xx as non-retryable and 408/429/5xx as retryable, passes Retry-After through as next_retry_delay, heartbeats, and sends X-OpenRouter-Cache: true so a retried identical request is served from OpenRouter's cache at $0. A --fail-once flag demonstrates that.

budget_gate is the same batch, but it pauses on a soft budget or on OpenRouter's 402 and resumes on a raise_budget Update, with a spend_report Query.

Tests use httpx.MockTransport and a fake Activity; no API key needed. Verified live against OpenRouter, including a cache hit at $0 on retry.

Docs page: temporalio/documentation#5307. TypeScript port: temporalio/samples-typescript#520.

Call OpenRouter from Activities with Temporal-owned retries, error
classification, Retry-After handling, heartbeats, and OpenRouter response
caching so a retried request is billed at zero. The budget_gate sample pauses
the batch on a soft budget or on OpenRouter's 402 and resumes on a
raise_budget Update. Also adds OpenRouter as a model provider for the OpenAI
Agents SDK plugin sample.
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