Shoutout & update: Fixing agent retry loops in CrewAI using turn-scoped sliding window hashes

Reddit r/AI_Agents Tools

Summary

TokenShield, an open-source FastAPI gateway proxy, now uses per-turn sliding window hashes to prevent agent retry loops in CrewAI, reducing token waste and cost.

A few days ago, I posted a discussion here asking how people catch agents that get stuck in retry loops before they burn through their entire API budget. A massive shoutout to everyone who chimed in with great insights. Based on that feedback, I refactored TokenShield (my open-source FastAPI gateway proxy that sits between LLM clients and providers). To test the update, I ran it through a notoriously stubborn local CrewAI test script where an agent gets stuck looping on a failing database tool (max_iter=10). Without the shield, the agent blindly hammered the tool 10 times in a row. Because every retry appends chat history back into the prompt context, prompt tokens ballooned from ~139 tokens on Turn #1 up to nearly 600+ tokens per turn—wasting money on a dead-end execution. Here is how the updated gateway flow handles it now at the network layer: Per-Turn Hash Window: It tracks normalized signatures (stripping out timestamps and UUID noise) strictly within the current turn, which completely stops false positives from blocking legitimate retries later. Tier 1 Soft Steering: If it spots stagnation, it injects system re-planning instructions before killing the request. Tier 2 Hard Stop: If the agent still persists, it trips a clean 429 cutoff to stop the token bleed instantly. For anyone running multi-agent workflows in CrewAI or other frameworks, scoping hashes per-turn keeps the circuit breaker razor-sharp while cutting off runaway token counts before the bill lands. If you want to check out the source code, logs, or test it out yourself, I've dropped the GitHub link down in the comments to keep this post clean. Would love to hear if anyone else has run into other edge cases with sliding windows on complex agent graphs!
Original Article

Similar Articles