name. See Run names.
Chains
Run agents one after the other, with your own code and durable steps in between. Typed output passes structured data from one step to the next:- Your code, not the model, applies the refund policy to the extracted amount.
restate.runjournals the result of the refund, so a replay returns it instead of calling the payment API again. Make the call idempotent, because a crash before Restate journals the result runs it again.- To wait for a person between steps, for example before a large refund, wait for a signal or an awakeable. To let a person approve a tool call, see Human approval.
Parallel agents
Start each run withrestate.spawn, and wait for all of them with restate.all:
Worker services
Make each worker a service, and fan out typed calls from an orchestrator:<review key>/<focus>.
Routing
A router agent with typed output picks the specialist, and your code runs it:Review loop
Run a writer and a reviewer in a bounded loop:stateKey, such as new Conversation({stateKey: "writer"}). The writer’s input for a revision is then only the reviewer’s feedback, and the writer’s conversation history already contains its earlier drafts, as in the evaluator-optimizer example. The same works across handlers: in the peer review example, PeerAuthor.revise passes only the reviews to the author agent.
Racing
Run agents concurrently, keep the first successful answer, and cancel the other runs:restate.any returns the first successful result and ignores failed runs. If every run fails, it throws an AggregateError, which Restate retries. When every run failed with a TerminalError, a retry would fail the same way, so firstSuccess throws a TerminalError instead.
Interrupting the losing task cancels its model call and tools, and restate.allSettled waits until the cleanup finishes.
Conversations
AConversation accepts the messages of one run at a time. Two runs that use one conversation at the same time in one handler, for example in restate.all, load the same version: the first one to save succeeds, and the other one throws ConversationConflictError at its next save, which fails the handler. The messages that the first run saved stay in the conversation.
Parallel runs
Give each run its own conversation withstateKey:
Racing runs
Racing runs can’t share a conversation either: the first run to save, for example at its first tool call or because it stopped early, makes the other runs fail at their next save, even if it doesn’t produce the answer. To keep one history that contains the winning answer, load the conversation once, pass its messages to every run, and save only the winner’s messages. The handler usesanswerWith and firstSuccess from Racing:
loadreturns the messages and the version.appendsaves the new messages if the version is unchanged.- The runs receive
messagesinstead of the conversation, so the losing runs save nothing. - A run that receives
messagesdoesn’t store its summaries in the conversation. If other runs store summaries in it,loadreturns only the messages after the last summary, and the summary itself insummary. The runs in this example receive onlymessages, so their model calls don’t include that summary.