Add Amiril to a LangGraph agent
Check every tool call of a LangGraph agent built with create_agent against your rules, in Python or TypeScript, and block the calls that break one.
On LangGraph, Amiril is middleware for create_agent, the function in the langchain package that builds a LangGraph agent. It goes first in the agent's middleware list and checks each tool call before the tool runs. A call that breaks a rule gets an error message in place of the tool's result. For a Python StateGraph you build by hand, use Amiril's ToolNode instead.
Install
pip install "amiril[langgraph]"Python needs 3.11 or later, langgraph 1.2 or later and langchain 1.3 or later. TypeScript needs Node 24 or later and langchain 1.4.5 or later.
Save a rules file
Save this file as rules.json beside your agent. Its one rule blocks an issue_payment call for more than 10000. With the rule in a local file, you can try the SDK before you have an API key. The file is JSON because that is the form the SDK runs. Once you connect, you write rules as rule text in Amiril and the SDK fetches them in this form.
{
"schema": "amiril.rules/1",
"ruleset_version": "example-1",
"rules": [
{
"id": "payment-cap",
"version": "1",
"mode": "enforce",
"description": "A payment amount must be above 0 and at most 10000",
"target": { "tools": ["issue_payment"] },
"assert": { "and": [{ ">": [{ "arg": "amount" }, 0] }, { "<=": [{ "arg": "amount" }, 10000] }] },
"action": "block"
}
]
}Add Amiril to your agent
Call init once, then put Amiril's middleware first in the agent's list. The first middleware is the outermost, so nothing else sees a call before Amiril does. Then pass the agent to instrument, which checks that the middleware is there and first.
import amiril
import amiril.langgraph
from langchain.agents import create_agent
from langchain_core.tools import tool
@tool
def issue_payment(payee: str, amount: float) -> dict:
"""Send a payment to a payee."""
return {"payee": payee, "amount": amount, "status": "sent"}
amiril.init(rules_file="rules.json")
agent = create_agent(model, [issue_payment], middleware=[amiril.langgraph.middleware()], name="payments")
amiril.instrument(agent) # checks Amiril's middleware is in place and firstLangGraph cannot add a hook to an agent once it is built, so the middleware goes in when you build it. model is the chat model your agent already uses. Rules see the agent under the name you give it, payments here.
Run the agent
Run the agent the way you already do. LangChain's fake chat model stands in for yours here, and it asks for payments of 500 and 20000 in one turn.
from langchain_core.messages import ToolMessage
result = agent.invoke({"messages": [("user", "Pay Acme Ltd 500 and 20000.")]})
for message in result["messages"]:
if isinstance(message, ToolMessage):
print(message.tool_call_id, message.status, message.content)The payment of 500 runs, and the payment of 20000 is blocked before it reaches the tool. The model gets an error ToolMessage in its place, and that message tells the model why the rule blocked the call.
c1 success {"payee": "Acme Ltd", "amount": 500.0, "status": "sent"}
c2 error The call to issue_payment was not performed. Rule payment-cap blocked it: A payment amount must be above 0 and at most 10000 (amount 20000). Do not retry this call with different arguments or achieve the same outcome another way; tell the user it was not performed.Use Amiril's ToolNode in a StateGraph
In Python, a StateGraph you build by hand takes Amiril's ToolNode in place of LangGraph's. It takes the same arguments, and instrument checks every ToolNode in the graph and its subgraphs. The TypeScript adapter supports createAgent only.
builder = StateGraph(State)
builder.add_node("model", call_model)
builder.add_node("tools", amiril.langgraph.ToolNode([issue_payment], agent="payments"))
builder.add_edge(START, "model")
builder.add_conditional_edges("model", tools_condition)
builder.add_edge("tools", "model")
graph = builder.compile()
amiril.instrument(graph) # checks every ToolNode carries Amiril's hookConnect to your workspace
So far the SDK runs the rules in your file. Connect it to your workspace and it runs the rules you publish in Amiril instead.
- Open the Data page and select Set up a new installation.
- Under API key, choose Create a new key. Name it and select Create key.
- Select Copy key. Amiril shows the key only once.
- Select Copy SDK endpoint.
Where your agent runs, set the key, the endpoint and a name for this service:
export AMIRIL_API_KEY="<your API key>"
export AMIRIL_ENDPOINT="<the SDK endpoint>"
export AMIRIL_SERVICE="<a name for this service>"Remove rules_file from init, or rulesFile in TypeScript, and restart the agent. The SDK now enforces the rules published to the key's environment. A rule blocks calls only once it is published and in Enforce, as Write your first rule shows.
When the agent starts, your service appears on the Installations page under the name in AMIRIL_SERVICE. Its Coverage card lists the agent and the tools Amiril checks. Otherwise the SDK reads the name from pyproject.toml or package.json, and every service in that project is listed as one installation. See SDK configuration for the order it reads names in.
Next steps
Write your first rule
Start from a rule Amiril drafted for an issue, or from a blank rule. Save it, publish it in Shadow, then switch it to Enforce in one environment.
How rules reach your agent
The SDK keeps your published rules in the agent's process and checks each tool call there. It keeps checking when Amiril is unreachable or the agent restarts.
SDK configuration
Every option of the Amiril SDK's init and every environment variable it reads, in Python and TypeScript, with its type, its default and what it does.
Send traces from LangGraph
Send each run of a LangGraph agent to Amiril over OpenTelemetry with OpenInference's LangChain instrumentation, in Python or TypeScript.
Last updated on