Error types
- Throw
TerminalError(from@restatedev/restate-sdk) when the model should handle a failure, for example by trying another approach. - Throw
ToolError(from@restatedev/harness) to also give the model an error code, such asnot_found. - Throw
AgentTerminalErrorwhen the run must fail, for example because the request it serves is no longer valid. - Let other errors propagate. In an
asyncTool, they’re retried under the tool’sretrypolicy, three attempts by default, and the model receivestool_failedonce the attempts run out. In atool, a guardrail, or another hook, the error fails the invocation attempt, and Restate retries the invocation under the policy configured at the service or handler level, or otherwise the Restate server’s default policy. The retried run continues from its journal.
What the model receives
ATerminalError from a tool call fails that call, not the run. The model receives the error as the tool result:
ToolError’s code, or tool_failed for any other TerminalError. The other tool calls in the same batch keep running.
A tool call that a guardrail or an approver rejects returns {"status": "denied", "message": "..."} instead.
A TerminalError from elsewhere in the run, such as a beforeRun guardrail, is passed to the model as a note before its next model call.
These error notes aren’t saved to the conversation. Each note emits an error-note progress event with the error’s origin, code, and message.
Some failures fail the run instead of reaching the model: a failed model call, a failure to load or save the conversation or to list skills, an afterRun rejection or error for a stopped or interrupted run, and an error in onProgress for a run event or a tool-end event.
A TerminalError in onProgress for another tool event fails only that tool call. A TerminalError in beforeSteering drops the steering message instead of reaching the model.
Common codes:
Failing a run
The model never receives anAgentTerminalError. Wherever it’s thrown, it fails the run:
- The other tool calls in the batch are interrupted. The conversation keeps the failed turn, with a note on the failure, and
messagesOf(error)returns the same history. See Failed runs. IfbeforeRunthrows the error, the run saves nothing, not even its input. - Every enclosing agent run fails too, including a parent that called this agent through
agentTool, and the agent whose tool called the handler that threw the error. - The handler fails with the error, unless it catches it.
isAgentTerminal to turn a refusal into an answer:
isAgentTerminal(error) instead of instanceof, because the error might have crossed a journaled step or a service call, which return failures as plain TerminalErrors.
Restate Harness stores the error type in the error metadata, so a ToolError keeps its code and an AgentTerminalError stays recognizable across service calls and on replay. Error metadata requires Restate Server 1.6 or later.
Pass {errorCode} to AgentTerminalError to set the status code that the handler’s caller receives. The default is 500.
Model call failures
A model response that the model can fix goes back to the model: Restate Harness discards the response, adds a note with the reason, and starts the next step. This covers an empty reply, a reply that ended before it was complete, for example at a content filter, a reply that announces tool calls without making any or contains an invalid batch of calls, and text longer thanmaxOutputChars.
For typed output that doesn’t match the schema, the model receives its answer back with a note that asks for an answer that matches the schema.
Each such step counts toward maxSteps.
A reply cut off at the output token limit fails the run with an AgentTerminalError, because a new attempt would most likely be cut off too. Raise maxOutputTokens, or check whether the model is looping. Before the run fails, it records the cut-off reply in its history:
- The reply’s reasoning and partial text, up to
maxOutputChars, are kept in a message markedcutOff: true. - Each tool call in the reply with complete arguments gets a failed result with the code
cut_off, without running. A call whose arguments were cut off is left out. - The conversation keeps the turn, with a note on the failure, like any failed run.
aiModel and maxOutputTokens set, a reply that uses the whole limit counts as cut off whatever finish reason the provider reports, because some providers report a cut-off reply as complete. This check needs the provider to report token usage.
A model call that fails is retried under the model’s retry policy. If the attempts run out, or the failure can’t be fixed by retrying, run throws a TerminalError.
A context overflow is handled separately: see Context window.
Cancellation
- Invocation cancellation: When you cancel the invocation, the run stops at its next durable operation. Unfinished tool calls are interrupted, child invocations are cancelled, and the handler fails with a
CancelledError. The conversation keeps the turn, with arun-cancellednote. The run makes no more model calls, so the note reaches the model only in the next run of the conversation. - Interruption: An interrupted run isn’t an error. It returns the status
interrupted, and unfinished tool calls are recorded as cancelled. - Background tool cancellation: When the model cancels a background tool call with
cancelOperationTool, the call’s result is{status: "cancelled", message}, with the model’s reason as the message, orCancelled by the agentif the model gives no reason.