Home / LangChain

Stop runaway LangChain agents in real time

On LangChain 1.x (create_agent / LangGraph) a callback can watch a runaway loop but cannot stop it — the framework swallows exceptions raised from callbacks. AgentBrake ships LangChainBrakeMiddleware, which runs inside the agent graph and halts the run. For the classic AgentExecutor (0.x), use the LangChainBrake callback.

$pip install agentbrake-sdk
GitHub

One line to wire it in

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)],
)

LangChain runaway patterns it catches

PatternHow it stops
Identical-tool loopsLoop detector trips after N identical tool calls in a row, across both the classic AgentExecutor and 1.x LangGraph agents.
Cost blowoutsLive cost ceiling from real token usage on each model call.
Endless reasoningStep and tool-call caps stop a run that never converges.
Swallowed brakesOn LangChain 1.x, callbacks can only observe a LangGraph run — a raised exception is logged and dropped. AgentBrake uses middleware, which runs inside the graph and actually halts it.

Add the brake to your LangChain agent

One line of code. Open source. Works on LangChain 1.x, CrewAI 1.x, and the classic AgentExecutor.

$pip install agentbrake-sdk
Star on GitHub

Frequently asked questions

Why can't a LangChain callback stop a LangGraph agent?
LangGraph runs callback handlers as fire-and-forget observers. An exception raised from a callback is logged and swallowed, so the agent keeps looping. Middleware runs inside the execution graph, so a raise from it actually unwinds the run.
Which class do I use?
LangChain 1.x (create_agent): LangChainBrakeMiddleware. Classic AgentExecutor (0.x): the LangChainBrake callback via config={'callbacks': [brake]}.
Does it need an API key or proxy?
No. AgentBrake runs in-process. There's a runnable example that brakes a real LangGraph loop with no API key at all.