r/LangChain
How I handled runaway tool-retry loops in LangChain agents without external SaaS (built an open-source local circuit breaker)
- upvotes
- 2
- comments
- 7
Post
Highlighted: the lines this signal was extracted from
A couple of weeks ago I set up an autonomous agent pipeline before going to bed. Woke up in the morning, opened my dashboard, and saw that the idiot spent nearly $30 in API calls. What was it doing? It tried to read a local config file that didn't exist. Instead of stopping, it just kept hitting `read_file` in a loop with microscopic prompt variations and retries for hours. The exact prompt hash changed slightly every time because of timestamps, so standard caching or basic deduplication did absolutely nothing. Most people talk about prompt injection or jailbreaks, but for anyone actually building with Claude / LangChain / CrewAI, the real silent killer is just dumb, recursive tool-call ping-pong. So I spent the last few days hacking together a local-first proxy called TokenGuard. How it actually works under the hood It runs locally on your machine (`127.0.0.1:8080`) as a drop-in proxy between your code and the LLM provider. You just change `base_url` to point to localhost. Instead of just checking if the prompt text is identical (which almost never works in real life): Tool-call ping-pong detection: It extracts tool names and normalizes JSON arguments on the fly. If an agent repeats the same tool call with the same arguments 3 times in a row after an error, the proxy cuts the connection immediately and returns a synthetic fail-closed error. State and loop tracking...
Keep reading with a free account
The rest of this post, and every signal for LangChain, is in your free account.