Skip to main content
A handler can run agents as steps of a workflow, next to your own code and other durable operations, such as service calls, timers, and journaled calls to external APIs. Your code decides the control flow, and each run journals its model calls and the durable steps of its tools, so after a failure, the handler replays its journal and continues where it stopped. To let the model decide when another agent takes part in a run, see Sub-agents, handoffs, and forks. If a handler starts more than one run, give each run a distinct 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.run journals 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 with restate.spawn, and wait for all of them with restate.all:
The runs execute concurrently in the handler’s invocation, and each journals its own model calls and tool steps. To give each agent its own invocation, make it a worker service. Runs that save to a conversation need a conversation each. See Parallel runs.

Worker services

Make each worker a service, and fan out typed calls from an orchestrator:
Each worker invocation has its own journal, retries, and entry in the Restate UI. Cancelling the orchestrator cancels the unfinished workers. If the workers are Virtual Objects, call each one under its own key, because Restate runs one exclusive handler per key at a time. The peer review example calls each reviewer under the key <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:
Each input to the writer above repeats the earlier draft. Alternatively, run the loop in a Virtual Object and give each agent its own conversation, with its own 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

A Conversation 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 with stateKey:
Each run loads only its own conversation. The object preloads the metadata and summary keys of both conversations, so each run fetches only the chunks that it reads. See Several conversations for the keys that each conversation uses.

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 uses answerWith and firstSuccess from Racing:
  • load returns the messages and the version. append saves the new messages if the version is unchanged.
  • The runs receive messages instead of the conversation, so the losing runs save nothing.
  • A run that receives messages doesn’t store its summaries in the conversation. If other runs store summaries in it, load returns only the messages after the last summary, and the summary itself in summary. The runs in this example receive only messages, so their model calls don’t include that summary.