Stop runaway AI agents before they burn your budget.
AgentBrake is the emergency brake for LangChain & CrewAI. One line of code stops infinite loops, cost blowouts and endless reasoning, in real time, before the next expensive call goes out.
imports as agentbrake· LangChain 1.x & 0.x · CrewAI 1.x

A runaway agent racking up cost, stopped the moment it crosses your ceiling, before the bill grows.
Agents fail expensively, not loudly
This is not rare. An agent calls the same tool 14,000 times with identical arguments. A planner expands one task into dozens of subagent calls. A reasoning loop never terminates and runs all night. Observability tools record it, they don't stop it. The gap between “the alert fired” and “the run stopped” is exactly where the money goes.
One line to wire it in
AgentBrake watches every step in real time and raises a clean exception that halts the agent before the next expensive call.
from agentbrake import LangChainBrakeMiddleware
from langchain.agents import create_agent
agent = create_agent(
model, tools=tools,
middleware=[
LangChainBrakeMiddleware(
max_cost_usd=2.00,
repeat_tool_limit=5,
)
],
)from agentbrake import CrewAIBrake
# right before crew.kickoff()
CrewAIBrake(
max_cost_usd=3.00,
repeat_tool_limit=5,
).install()What it catches
Five runaway patterns, one brake. It warns at 80% of any limit and stops at 100%.
Identical-tool loops
Same call, same args, over and over. Trips after N identical calls in a row.
repeat_tool_limitCost blowouts
The $47k overnight run. A hard ceiling, enforced live as tokens are spent.
max_cost_usdEndless reasoning
A loop that never hits its stopping condition. Caps total reasoning steps.
max_stepsTool-call storms
Runaway tool fan-out. Caps total tool invocations per run.
max_tool_callsHung runs
A run that just keeps going all night. A wall-clock ceiling per run.
max_duration_sProvider spend caps are monthly and account-wide, they fire after the damage. AgentBrake is per-run and in-process: it stops this agent now.
A sprinkler, not a smoke alarm.
In-process by design. Private and fast.
AgentBrake runs inside your process as a library. No proxy, no gateway. Your prompts and data never leave your environment, and the brake adds microseconds, not a network round-trip.
Privacy by design
Your prompts, completions and tokens stay in your process. Nothing is sent to a third party, so there is nothing to leak.
~3 microseconds per step
Measured overhead per agent step, with no network call in the hot path. Over 300,000 steps per second on a single core.
No proxy in the path
Proxy and gateway tools add a round-trip and see your traffic. AgentBrake is just a local library, so latency stays yours.
Frequently asked questions
What is AgentBrake?
AgentBrake is an open-source Python package that stops runaway LangChain and CrewAI agents in real time. You set limits, cost ceiling, identical-tool-loop detection, max steps, tool calls, and duration, and it halts the run before the next expensive call goes out.
How is it different from observability tools?
Observability tools like Langfuse, Helicone, and LangSmith record what an agent did. AgentBrake intercepts and stops it. It runs in-process and per-run, so it halts this agent now, not after the bill arrives.
Does it work with LangChain 1.x and CrewAI 1.x?
Yes. On LangChain 1.x (create_agent / LangGraph) it uses middleware that runs inside the agent graph, because callbacks can only observe a LangGraph run, not stop it. On CrewAI 1.x it patches the provider call path and raises a BaseException the framework's retry loop cannot swallow.
How much code does it take?
One line. You add the middleware (LangChain) or call .install() (CrewAI). No refactor, no proxy, no account. It's free to use and source-available (FSL).
Stop your next runaway agent
One line of code. Free and in-process. Works on LangChain 1.x, CrewAI 1.x, and the classic AgentExecutor.