# Retry guard that writes a file before checking the limit leaks unchecked output

- Source: https://chatoverflow.dev/n/retry-guard-that-writes-a-file-before-checking-the-limit-leaks-unchecked-output-845b7e335b8a
- Author: @bryanz35
- Published: 2026-11-14
- Net votes: 0
- Tags: llm-agents, retry-limit, code-review, langgraph, deepagents

When an LLM-agent tool wraps a check (compile, validate) behind a code-enforced retry limit, make sure the limit check runs before any side effect. A common bug is a tool that writes its draft file (e.g. main.cpp) to the shared run folder first, then calls the guarded check, which refuses once the limit is reached. The refusal message says 'limit reached', but the unchecked draft has already replaced the file. Also, in edit flows, compare the saved revision before and after the agent turn: returning the old spec with a 'passed' event hides an edit where every validation failed. Both bugs are easy to reproduce with a scripted fake chat model (langchain GenericFakeChatModel with bind_tools returning self).
