How to block a bad tool call in LangGraph
LangGraph tool call guardrails drawn from your agent's own traces. Draft a rule from an issue, run it in Shadow, then block the call in-process.
LangGraph tool call guardrails drawn from your agent's own traces. Draft a rule from an issue, run it in Shadow, then block the call in-process.
Blocking a tool call in LangGraph takes about ten lines, and they're in the next section. The trouble with LangGraph tool call guardrails starts once the block works, when somebody has to decide what number goes in it. Amiril's answer is to take that number from the agent's own traces and let you test the rule before it blocks anything, which is the second half of this post. The example agent here is a treasury assistant with a transfer_funds tool, and the bad call is a transfer far larger than anything it normally sends.
The nearest place to stop a call is the tool itself. Check the arguments at the top of the function and return an error string for the model to read. (Return it, don't raise. A hand-built ToolNode re-raises an exception thrown inside a tool unless you tell it not to, and that ends the run.) One level up, LangChain's create_agent takes a list of middleware, and a wrap_tool_call hook sees each call before the tool does. It can hand back a ToolMessage without running anything:
from langchain.agents.middleware import wrap_tool_call
from langchain_core.messages import ToolMessage
@wrap_tool_call
def transfer_limit(request, handler):
call = request.tool_call
if call["name"] == "transfer_funds" and call["args"]["amount"] > 1000:
return ToolMessage(
content="Not performed: transfers over 1000 are blocked. "
"Do not retry with a different amount. Tell the user.",
tool_call_id=call["id"],
status="error",
)
return handler(request)Pass it as middleware=[transfer_limit] and a 25,000 transfer comes back to the model as an error, while a 500 one runs. LangChain ships some middleware ready-made as well. HumanInTheLoopMiddleware pauses the tools you name until a person approves, edits or rejects the call, and ToolCallLimitMiddleware caps how many calls a run may make. On a StateGraph you built by hand, with no create_agent, the same check goes in a node between the model and ToolNode, and interrupt() in that node gets you a person's approval (it needs a checkpointer).
There are libraries too. NeMo Guardrails has execution rails that validate tool calls and their arguments, set up in YAML and Colang, and Guardrails AI is aimed more at what goes into and comes out of the model. For a fixed check, such as an amount that can't be negative, the if in the tool is all you need.
That 1000 was a guess. If the agent's transfers normally run from a few dozen up to nearly five thousand, a limit of 1000 blocks ordinary work on the first morning, and a limit of 50,000 never fires. Whichever you pick, it now lives in your repo, and moving it means a deploy.
Amiril gets the number from the agent. It reads the agent's traces, which for LangGraph means OpenInference's LangChain instrumentation and an OTLP exporter, a few lines at startup (the LangGraph traces guide has them). From those it learns what is normal for that agent, using only that agent's own history. A transfer far outside it shows up on the run as a rarity factor, in a line like this one:
args.amount was 25,000 on a transfer_funds call, the largest of 640 observations
Runs that share a problem are grouped into an issue, and an AI investigator reads a sample of them and writes up what they have in common. When a rule could have stopped those runs, the issue's page carries a rule suggestion with the rule already drafted. Under it is a line saying what the rule would have done to the very runs that prompted it, such as "Would have blocked 4 of 5 runs".
Selecting Create rule opens the editor with the draft in it:
@id("transfer-limit")
@sentence("A transfer of {amount} is above the single-transfer limit of 5000")
block (
agent == "treasury",
tool == "transfer_funds"
)
when {
call.args.amount > 5000
};In this example the draft's 5000 sits just above the top of the agent's normal range. It's a starting point, and if your finance policy says 3000 you type 3000. The agent name is the name you pass to create_agent, and the sentence is what both the model and a person reading the app are told when the rule fires. You can change any of it before saving. A rule can also look beyond one call's arguments. It can require an earlier call in the same run, or cap a running total across a session (the rule language tour has both).
A saved rule is a draft and does nothing until you publish it. Once published it starts in Shadow, which means it checks every call, records what it would have done and lets the call through anyway.
The rule's page counts the calls it would have fired on in the last 24 hours. That count comes from traces, so it works before the SDK is anywhere near your agent. There is also Test on past calls, which replays any version of the rule, an unpublished draft included, over the last 24 hours, 7 days or 30 days, and lists each call it would have fired on with the agent, the tool and the sentence.
If that list contains calls you think were fine, change the rule and test again. If it's empty when you expected it not to be, check that the rule spells the tool name the way the traces do.
The block itself happens inside the agent. On LangGraph the SDK is middleware for create_agent:
pip install "amiril[langgraph]"
import amiril
import amiril.langgraph
from langchain.agents import create_agent
amiril.init()
agent = create_agent(
model,
[lookup_account, transfer_funds],
middleware=[amiril.langgraph.middleware()],
name="treasury",
)
amiril.instrument(agent)amiril.init() reads AMIRIL_API_KEY and AMIRIL_ENDPOINT from the environment. Amiril's middleware goes first in the list because the first middleware is the outermost, and instrument raises if it's missing or anywhere but first. All of this belongs where you build the agent, since LangGraph can't add a hook to one that already exists. A StateGraph you built by hand in Python takes Amiril's ToolNode in place of LangGraph's. TypeScript works the same way and is in the SDK guide.
Switch the rule to Enforce on its page. The next time the model asks for an oversized transfer, it gets this back instead of the tool's result:
The call to transfer_funds was not performed. Rule transfer-limit blocked it: A transfer of 25000 is above the single-transfer limit of 5000. Do not retry this call with different arguments or achieve the same outcome another way; tell the user it was not performed.
The "do not retry" wording is there because a model that gets a bare tool error tends to have another go, often with the amount nudged down a little. The check is per call, so if the model asks for two tool calls in one turn and only one breaks a rule, the other still runs.
When a call is blocked the tool never runs, and LangGraph records no span for a tool that never ran, so a blocked call is invisible in the trace. Amiril gets the decision from the SDK instead. The rule's page counts what the SDK blocked, flagged and allowed, and the run's page opens with "1 call was stopped before it ran".
The rules live in Amiril, with a copy inside the agent's process that the SDK refreshes about once a minute. Checks run against that copy, so they don't wait on the network, and tightening the limit later is an edit and a publish. If Amiril can't be reached, the SDK carries on enforcing the last rules it had, and it keeps them on disk so a restart doesn't lose them. The one wait is on a cold start, when the first tool call holds for up to 2 seconds while the rules arrive. Each rule also has a setting for what happens if a fault inside the SDK stops it checking a call, which is either to let the call run or to stop it.
Working from traces also means the first oversized transfer has already happened before there is anything to learn from. Keep the hand-written ceiling from the first section on a tool like this while the agent builds up history.
Amiril goes on reading the agent's traces. If the rule came from an issue and nothing new joins that issue for two days, Amiril resolves it, and reopens it if the problem comes back. We've written separately about why detection and blocking are one product for us. For the detection half on a graph, see LangGraph monitoring, and LangSmith can stay on while you try it, with both exporters on one graph, as the LangSmith comparison sets up. Every framework Amiril supports is on the AI agent guardrails page.
None of this happens without traces, so if you want to try it, connect a trace source before anything else.