tools: {name: tool}) or on a single run (run(input, {tools})).
The map key is the tool name that the model uses: 1 to 64 letters, digits, _, or -.
Tool inputs are Zod schemas, and Restate Harness validates the model’s arguments before the tool runs. For serviceTool, the called handler validates the input.
For an agent with many tools, see Code mode and tool search.
Async tools
asyncTool runs an async function as a journaled step:
- Restate Harness journals the result once
executesucceeds. On replay, the tool returns the journaled result without runningexecuteagain. The result must be JSON. - A thrown
Erroris retried underretry, which defaults to three attempts. After the last attempt, the model receives the error with the codetool_failed. Aretrypolicy that you pass replaces the default, so setmaxAttemptsin it.retry: {maxAttempts: 1}turns off these retries, but a crash before Restate Harness journals the result still runsexecuteagain, so make external writes idempotent. - A
TerminalError, or aToolErrorwith a code such asnot_found, isn’t retried. The model receives the error as the tool result, and the run continues. AnAgentTerminalErrorfails the run instead:runthrows it. See Error handling. signalaborts when the tool call is cancelled, for example because the run is interrupted, and when the invocation attempt ends. Pass it to APIs such asfetch.callIdis stable across retries. Combine it withinvocationIdand the run’snameinto an idempotency key for external writes, for example${invocationId}:${name}:${callId}.
Generator tools
tool takes a generator execute, so the tool can call other services, sleep, wait for signals, and use state:
restate.run or in an asyncTool.
Wrap service calls in awaitChild, so interrupting the run cancels the call. Without it, the called handler keeps running.
Run context
A tool reads the run context for data that the model must not control, such as customer IDs, prices, and clients:agent<Ctx>({tools: {...}}), the tool’s context is typed from the agent. A tool defined on its own needs the Tool<Ctx> type on its variable.
Background tools
Withbackground: true, the model receives a pending result and continues without waiting for the tool. The tool’s result arrives in a later step:
sleepTool lets the model wait durably, and cancelOperationTool lets it cancel a background tool call.
A run doesn’t complete while background calls are pending. If the model returns a final answer, the run discards it, waits for the next background result or steering message, and calls the model again. If the run reaches maxSteps first, it cancels the pending calls and stops with max_steps.
Tools for one run
run(input, {tools}) adds tools to one run, next to the agent’s own tools. A run tool can’t use the name of an agent tool.
Use this for tools that depend on the request, such as MCP tools, or tools that a client UI provides.
The AG-UI lesson turns the tools of an AG-UI client into run tools.