AI agent guardrails belong in code that runs before every tool call
A guardrail that only lives in your system prompt is a polite request. For AI agents that can delete, pay or deploy, put the guardrail in code that sees every tool call before it runs. The strongest pushback is that mod
A guardrail that only lives in your system prompt is a polite request. For AI agents that can delete, pay or deploy, put the guardrail in code that sees every tool call before it runs.
The strongest pushback is that models follow instructions better with every release. They do, but prompt injection only needs a convinced model. A check in code looks at the call the model actually made, whatever talked it into it.
In Dapr Agents, that check is a before_tool_call hook. It gets the tool name, its source (such as local, MCP or OpenAPI) and the exact arguments, then returns a decision: Proceed, Skip, Mutate, Deny or RequireApproval.
from dapr_agents import DurableAgent, tool
from dapr_agents.hooks import (
Hooks, ToolHookContext, HookDecision,
Proceed, Deny, RequireApproval,
)
@tool
def delete_repo(name: str) -> str:
"""Delete a repository by name."""
...
def guard(ctx: ToolHookContext) -> HookDecision:
if ctx.step_name == delete_repo.name:
return RequireApproval(
timeout_seconds=3600,
instructions=f"Delete repo {ctx.payload.get('name')}?",
)
if ctx.source == "mcp" and ctx.step_name.startswith("drop_"):
return Deny(reason="schema changes go through review")
return Proceed()
agent = DurableAgent(
name="repo-janitor",
role="Cleans up stale repositories",
tools=[delete_repo],
hooks=Hooks(before_tool_call=[guard]),
# LLM and state config as in the getting-started guide
)
A design choice I love as a maintainer is that before_tool_call runs inside the durable workflow itself. It has to be deterministic, so no network calls in there. In return, a RequireApproval wait is part of the workflow's history. Restart the agent mid-wait and it's still waiting when it comes back. If nobody answers before the timeout, the call is denied. The LLM hooks run inside an activity, so they can't pause for a human.
Pro tip: the reason and details on a Deny go into workflow history, so keep secrets out of them!
Pick the one tool you'd least like an agent to call on its own, put a hook in front of it, and tell me what yours checks π€
For running approval waits in production, this walkthrough helps: https://www.diagrid.io/blog/human-in-the-loop-workflow-operations-catalyst
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.