callModel loop works for all of them once you wire up four primitives.
Outcome: A long-horizon agent that:
- Caps total cost and step count so it always terminates.
- Persists conversation state so it can be resumed after a crash, deploy, or human approval.
- Streams progress events so dashboards and UIs stay live during the run.
- Runs a self-ask loop — research, adversarial review, repeat — until the agent emits a
[DONE]sentinel. - Optionally accepts voice input via OpenRouter’s Speech-to-Text endpoint and replies with Text-to-Speech.
Prerequisites
- Node.js 20+ or Bun
- An OpenRouter API key in
OPENROUTER_API_KEY - A project with
@openrouter/agentinstalled - A place to persist state — a database, Redis, S3, or the local filesystem
- Optional: a microphone or audio file for the voice section
pip
1. Set hard ceilings on every run
Long-horizon agents must terminate. Combine multiple stop conditions so the loop ends as soon as the first one fires. The most useful for long runs aremaxCost, stepCountIs, and maxTokensUsed.
stepCountIs, hasToolCall, maxTokensUsed, maxCost, finishReasonIs) and how to compose custom predicates.
2. Persist state for resumability
A multi-hour run must survive restarts, deploys, and human approvals.callModel accepts a StateAccessor that loads and saves ConversationState between steps. Back it with whatever storage your app already uses.
callModel again with the same StateAccessor. Pass input: [] to signal “no new user turn — continue from saved state”; the SDK loads the checkpoint and keeps going.
3. Stream progress instead of waiting
A run that lasts an hour should not block your UI for an hour.callModel returns a result object with several streams you can consume independently:
result.getTextStream()— token deltas for the user-facing response.result.getToolCallsStream()— tool calls as they complete.result.getFullResponsesStream()— the full event stream, including tool preliminary results.result.getResponse()— the final, fully-resolved response with usage data.
publishToDashboard to whatever transport you already use — Server-Sent Events, WebSockets, a database table, or a pubsub channel.
4. Loop with adversarial self-review
A single pass throughcallModel often leaves gaps — unverified citations, missing edge cases, or stale data. Wrap the run in an outer self-ask loop: research, adversarial review, repeat until the agent emits a [DONE] sentinel. Each iteration appends a new user turn to the persisted StateAccessor, so the agent builds on its prior work instead of starting over.
[DONE] sentinel is intentionally cheap: any model can produce it, and a plain String.includes check keeps the control flow obvious. Swap the review prompt or the reviewer model (for example a faster ~anthropic/claude-sonnet-latest critiquing an Opus researcher) without changing the loop. Three layers of ceilings keep cost bounded: SELF_ASK_MAX_ITERATIONS caps the number of review rounds, and each round inherits its own stepCountIs + maxCost budget.
5. Add voice input
Drive the same agent loop from a voice memo, phone call, or push-to-talk app. OpenRouter exposes a dedicated/api/v1/audio/transcriptions endpoint with a single STT model parameter. Hand the transcript to callModel exactly like a text prompt.
Output
callModel applies in the TypeScript and Python tabs.
For a streaming microphone, capture audio chunks on the client, send them to your server, and call createTranscription once silence is detected. Use the STT cookbook for the full request and response shape.
6. Speak the response back (optional)
For voice-out, pipe the agent’s reply through/api/v1/audio/speech and write the resulting bytes to a file or stream them to the caller.
7. Notify on completion
Long-horizon jobs usually run somewhere the user is not watching. Notify them when the run terminates — by webhook, email, Slack message, or whatever your stack uses. Trigger the notification oncegetResponse() resolves so the agent has fully completed and ceilings have been honored.
Check your work
A correct long-horizon implementation should pass all of the following:- A run with a low
maxCost(for example,maxCost(0.10)) returns fromcallModelonce the ceiling is hit, even if the agent has more work queued. - Killing the process mid-run and starting a new
callModelinvocation with the sameStateAccessorresumes from the savedConversationState. The message history grows rather than starting over. getToolCallsStream()andgetTextStream()yield events while the agent is still running, not only at the end.- Sending a voice file through
client.stt.createTranscriptionreturns the expected text, and feeding that text intocallModelproduces a response that references the spoken request. - A webhook (or other notification) fires after
getResponse()resolves.