$ cat ~/map/pi-loop-guard.md
pi-loop-guard
Stops pi when the model loops — repeated tool-call cycles and repetitive streamed output cut off before they burn tokens.
extensions✓ Nov 8
Facts
- repository
- github.com/huytd/pi-loop-guard ↗
- GitHub Stars
- ★ 0
- Forks
- 0
- license
- MIT
- npm / mo
- —
- last push
- Nov 7, 2026
install
ln -s ~/code/pi-loop-guard ~/.pi/agent/extensions/pi-loop-guard (auto-discovered globally, hot-reloads with /reload; or try one session with pi -e ~/code/pi-loop-guard/src/index.ts — pi loads the TypeScript source directly, so there is no build step).## TL;DR
A pi extension that stops the agent when the model gets stuck in a loop: tool-call cycles of one to four calls repeated three times are blocked at the completing call, and repetitive streamed output — a unit repeated at least three times over at least 300 chars, or a line appearing eight times within sixty — aborts the stream so local servers stop generating too.
## Key points
- $Catches two loop kinds the README pins on small or heavily quantized local models: tool-call loops and repetitive output in text or thinking.
- $Tool-call detection reduces each call to its tool name plus its arguments with keys sorted; when the most recent calls form one cycle of 1-4 calls repeated 3 times, the call that completes the third repeat is blocked before it runs and the agent is stopped.
- $Output detection fires when the end of the stream is one unit (1-1000 chars) repeated at least 3 times over at least 300 chars, or when any line of 12+ chars appears 8 times within 60 lines; the stream is aborted, which closes the HTTP request so llama.cpp and similar servers stop generating too.
- $Streamed tool-call arguments are scanned too — a file being written, for example — but with 4x the character threshold and no line check, because file contents often repeat lines legitimately.
- $The tool-call history resets on every new user prompt, and each trigger is recorded in the session as a loop-guard custom entry that is not sent to the model.
- $/loop-guard [status|on|off|reload] shows the current settings and the last trigger, turns the guard on or off for the session, or reloads the config.
- $Optional loop-guard.json lives in ~/.pi/agent/ (a <project>/.pi/ file overrides it for trusted projects) and only needs the keys you want to change: nudges sets how many times the looping call is blocked and the model told it is looping before the agent is stopped, minRepeats rises for legitimate repeats like a polling bash call, and lineRepeats 0 turns off the line check.
- $When the guard stops a text loop the repeated text stays in the session — the README suggests /tree or /fork to branch from before the loop, since just saying continue may start the loop again.
## FAQ
Q When does a loop guard matter most?
The README pins it on small or heavily quantized local models, where the agent getting stuck repeating the same tool call or the same phrase happens often.
Q What happens when a text loop is stopped?
The stream is aborted and the repeated text stays in the session; saying continue may start the loop again, so the README suggests /tree or /fork to branch from before the loop or telling the model what to do differently.
Q Are triggers visible to the model?
No — each trigger is recorded in the session as a loop-guard custom entry, which is not sent to the model.
Q pi keeps polling a service and the guard fires — what do I change?
Raise tool.minRepeats in loop-guard.json; the README names a legitimate repeated polling bash call as the case to tune for.
Q Does the guard check arguments of streamed file writes?
Yes, streamed tool-call arguments are scanned, but with 4x the character threshold and no line check because file contents often repeat lines legitimately.
## References
## Related entries
$ pi-kit
Community grab-bag of guardrails, workflow tools, and integrations for the pi coding agent.
$ big-pi
Model-driven compression that summarizes completed phases into tiered blocks while Pi's JSONL transcript stays authoritative and untouched.
$ inception
A curator LLM replaces pi's compaction, rewriting old history every few turns to keep the context dense.
## Guides
Last updated: Nov 7, 2026