How do you keep an eye on long agent runs and recover when one stalls?
19 posts
Playbook
Practitioners monitor agent state via external watchdogs or file tails and recover using persistent checkpoints or state files to avoid restarting long runs.
- Use an external watchdog for loop detection
Run a separate read-only process to monitor the event stream. If the agent retries the same fix, first prompt it for a falsifiable root-cause hypothesis. If stuck, swap in a fresh agent with a handoff note. If still stuck, move the task to a human queue. 3
- Persist state in files to survive resets
Maintain three files: state/current.md, backlog.md, and decisions.md. Add a pre-commit hook that rejects any commit without a checkpoint. This prevents re-doing merged tasks or re-investigating failed tests after context resets. 2
- Save steps as database commits
Save every agent step to a database before continuing, treating each save as a commit. If a run crashes, resume from the last commit. A typical run contains about 12 commits, allowing recovery without starting from scratch. 8
- Pause after two stalled iterations
Define goals in files with command-checkable completion conditions. If two iterations pass without progress on the same stage, pause the agent and send a notice. Other stops include blocked merges, protected paths, or schema changes. 4
- Monitor reasoning for improvised workarounds
Tail session files and send content to a model like Groq or Cerebras to check against custom rules. Alert if the agent chooses to change code and report later instead of stopping to ask about missing environment pieces. 1
Choosing between approaches
Some approaches rely on external tools like ThoughtCop or pi-durable, while others use native features or simple file structures. Manual compaction thresholds vary by model, with one practitioner setting auto-compaction at 400k and manual checks between 200k and 400k 7, while another uses three thresholds to manage cost before pricing doubles 6.
Not covered yet
The material does not specify how to handle side effects from external API calls that Git cannot revert, beyond general strategies to capture state and verify results independently 10.
Posts
- Watch Claude Code's reasoning in real time with ThoughtCop and flag improvised workaroundsThe author monitors Claude Code's reasoning with ThoughtCop, an open-source macOS menu bar app.

- Survive nightly context resets with three state files and a pre-commit hookThe author's fully autonomous Claude Code system hit 4 to 5 context resets a night.

- Run an external watchdog that detects agent loops and escalates in three levelsThe most painful failure in the author's autonomous implementation system was an agent retrying the same wrong fix.

- Run a coding agent overnight with goals in files and a stop after two stalled iterationsThe author splits the goal into stages written as files in a .harness/goals directory rather than in the chat, with completion conditions that a command can check.
- Hand an agent a prompt to speed up CI: time each job in three parts, cancel stale runsTw93 spent an afternoon overhauling the CI of three projects, Mole, Kaku and Pake, and published the prompt for handing to an agent.
- Add a compaction tool to a Pi agent so it decides when to compact and what to keepThe author gave a self-built Pi agent a dedicated compaction tool, so the agent judges when to compact and what to keep instead of relying on a fixed-percentage auto-compact.

- Set auto-compaction at 400k and keep sub-agent nesting to three layersdex notes that with better compaction there is less need to explicitly clear or externalize context to keep a model focused, though it still helps when working with people or handing off between agents and machines.
- Save every agent step to a database as a commit, and resume from the last commit after a crashMario Zechner describes the persistence mechanism of pi-durable: each step of an agent run is saved to a database before it continues, and one save is one commit.

- Type ultrathink mid-task in Claude Code when the work turns out harder than expectedIf a task in Claude Code proves harder than expected, type ultrathink straight into the terminal.
- Capture original state before agent actions, then retry, restore, compensate or escalate, and verifyTanmay Sah and Dolly Sah point out that Git cannot cover side effects such as external API calls, so coding agents struggle to undo mistakes.

- Have Claude run daily maintenance routines from a Slack channel and open pull requests
- Let an agent triage alerts and open fix pull requests, escalating to a person only for new problems
- Track down a missing-background bug in a Unity build from Rider with TeamCity CLI and MCP
9 more posts to see