Skip to main content
Guardrails are functions that check a run’s input, steering messages, tool calls, tool output, and final answer. The model can’t bypass them, because they run in your code.

Tool guardrails

Add before to a tool. Return a string to deny the call with that reason, false to deny it with a generic reason, and nothing or true to allow it:
A denied call never runs execute. The model receives a denied result with the reason. The price comes from the run context, not from the tool input, so the model can’t change it.

Agent guardrails and hooks

Agent-level hooks check every run of the agent: Each hook except afterTool returns its decision: For example, beforeTool can restrict a tool by name. It also checks the tools that you pass to run or load from an MCP server:
To ask a person instead of rejecting, call yield* ctx.approve(input, {question, details}) in a tool’s before or in beforeTool, where ctx is the hook’s second argument and input is the tool input to show the approver. Return a rejection if it returns false. See Human approval. beforeStep(request, ctx) runs before the model call of each step. Return {messages} to send other messages in that one request, for example with retrieved documents, or reject to skip the step’s model call: the model receives the reason as a note before the next step. Unlike the other hooks, it doesn’t change what the run records. Summary calls, such as the summary of a stopped run and compaction summaries, don’t run beforeStep, so don’t use it to remove sensitive data.

Sensitive data

A rejection doesn’t remove data: an input that beforeRun rejects stays in the run’s history, and so do the arguments of a denied tool call. To keep an input out of the conversation entirely, throw AgentTerminalError from beforeRun: the run then fails without saving anything. To keep data away from the model and out of the saved conversation, replace it where it enters the run:
Later model calls, summaries, result.messages, and the saved conversation contain only the values that these hooks return. afterTool receives only successful output. When a tool fails, the run records the error message unchanged, so keep sensitive data out of the errors that your tools throw. The Restate journal is separate from the run’s history. It stores the handler’s input, the payload of every signal, such as a steering message, and the result of every journaled step, such as an asyncTool’s output before afterTool runs. To keep data out of the journal, remove it inside the tool before the tool returns.

Writing hooks

  • Return the decision. A rejection or replacement that the hook builds but doesn’t return has no effect.
  • Reject by returning, not by throwing. A thrown TerminalError reports that the check itself failed, and the model usually receives it as an error. See What the model receives. Throw AgentTerminalError to fail the run.
  • Return the same decision on replay. Hooks run again when Restate replays the handler. Compute decisions deterministically, or inside restate.run, which journals the result.
  • Don’t modify what a hook receives. Hooks receive the run’s messages without a copy. Return a replacement instead.
beforeRun, beforeSteering, and afterRun belong to the agent that starts the run. After a handoff, the target agent adds its own beforeStep, beforeTool, and afterTool, and the original agent’s checks still run. A sub-agent that runs through agentTool uses only its own guardrails, so set them on the sub-agent.