Skip to main content
You pass tools as a map, on the agent definition (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 execute succeeds. On replay, the tool returns the journaled result without running execute again. The result must be JSON.
  • A thrown Error is retried under retry, which defaults to three attempts. After the last attempt, the model receives the error with the code tool_failed. A retry policy that you pass replaces the default, so set maxAttempts in it. retry: {maxAttempts: 1} turns off these retries, but a crash before Restate Harness journals the result still runs execute again, so make external writes idempotent.
  • A TerminalError, or a ToolError with a code such as not_found, isn’t retried. The model receives the error as the tool result, and the run continues. An AgentTerminalError fails the run instead: run throws it. See Error handling.
  • signal aborts when the tool call is cancelled, for example because the run is interrupted, and when the invocation attempt ends. Pass it to APIs such as fetch. callId is stable across retries. Combine it with invocationId and the run’s name into 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:
Code in the generator body runs again on replay. Put external calls in 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:
Inside 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

With background: 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.