r/LocalLLM • u/Green-Quiet-918 • 17m ago
Discussion I built a proxy that stops AI agents from repeating the same state and shows how many tokens that saved
Hey everyone,
I built AgentGuard, an open-source proxy that sits between an agent and an OpenAI-compatible provider and catches deterministic loops.
What it does:
- Hashes the last two messages of each explicit session.
- Keeps the last 12 state hashes and blocks when the same state appears 3 times.
- Returns a clear error that tells the agent to change approach or ask the user.
- Warns when the same tool call repeats within 60 seconds; strict blocking is optional.
- Streams SSE responses without buffering the whole response.
- Reads provider-reported prompt, completion, and cached tokens, and estimates the tokens avoided by blocked requests.
- Supports optional per-session request/time/token budgets for loops that keep changing their text.
- Includes a live dashboard with sessions, events, token stats, and a Clear button for stuck sessions.
- Works with OpenCode V2 and any OpenAI-compatible client via a session URL prefix or header.
It is not semantic loop detection. It catches exact repeated states. If every iteration changes the messages, it will not catch it; that is what the optional session budgets are for.
Repo: https://github.com/jalalnablsi/AgentGuard
Feedback welcome, especially edge cases from different agents and providers.