{"slug": "how-to-keep-claude-code-working-until-a-task-is-done-with-a-stop-hook-loop", "title": "How to Keep Claude Code Working Until a Task Is Done With a Stop Hook Loop", "summary": "Anthropic's Claude Code runs a Stop hook before returning control to the user, and a hook that returns a blocking JSON decision with a required `reason` field can keep the agent working until verifiable checks such as passing tests or a successful build succeed. Anthropic's `ralph-loop` plugin automates this loop with a completion phrase and a `--max-iterations` limit, as in its documented example `/ralph-loop \"Build a REST API for todos...\" --completion-promise \"COMPLETE\" --max-iterations 50`, with `/cancel-ralph` to end an active loop. The guidance stresses that a completion phrase is Claude's own signal rather than independent evidence, so a separate check should inspect test or build results before the task is allowed to end.", "body_md": "### How to Hide or Shorten MCP Tool Output in Claude Code\n\nWhen a tool from a Model Context Protocol (MCP) server returns data to Claude Code, you might want to see less tool…\n\nClaude Code can end its response before its code change meets your requirements. A **[Stop hook](https://code.claude.com/docs/en/hooks)** is an automated action that runs when Claude Code is about to end its turn. The hook can check whether the task is complete and, if it is not, send Claude a reason to continue.\n\nA Stop-hook loop repeats the cycle of Claude doing work and the hook checking it. Define “done” with checks Claude can verify, such as passing tests or a successful build. Set a limit too: the hook can ask Claude to keep working, but it cannot prove the result is correct or guarantee that Claude will finish.\n\nAfter Claude Code’s agent finishes a response, Claude Code runs a Stop hook before returning control to you. If the hook returns a blocking decision, Claude continues instead of ending the turn. The hook’s reason tells Claude what remains to do.\n\nA command-based hook returns its decision as JSON on standard output, the stream where a command prints its results. A response can look like this:\n\n```\n{\n  \"decision\": \"block\",\n  \"reason\": \"The required checks have not passed. Run the failing tests, fix the issues, and verify again.\"\n}\n```\n\nThe [`reason` field is required](https://code.claude.com/docs/en/hooks) when the hook blocks a stop. The hook can also provide guidance through `hookSpecificOutput.additionalContext`. Both approaches give Claude feedback to act on; neither one verifies that Claude’s next attempt will succeed. The [Claude Code Hooks reference](https://code.claude.com/docs/en/hooks) documents the Stop event and its response format.\n\nA useful loop has three parts: a specific goal, a check that produces observable evidence, and a stopping rule. For a code change, that check might be a project test command or a build. The hook should allow Claude to stop when the required checks pass. If they keep failing, a limit should end the loop and leave Claude to report what remains unfinished.\n\n`/goal`, a plugin, or a custom hook\nUse `/goal` when you want Claude Code’s built-in way to keep working toward a goal for the current session. Anthropic describes it as a [prompt-based Stop hook](https://code.claude.com/docs/en/hooks). Its loop guidance recommends goals with verifiable exit criteria, such as naming the tests that must pass, rather than asking Claude to decide whether the work simply feels complete. The [loop engineering guidance](https://claude.com/resources/articles/getting-started-with-loops) recommends starting with the simplest approach that fits the task.\n\nAnthropic’s `ralph-loop` plugin runs a Stop-hook loop for work that repeats within one session, with a set limit on the number of iterations. Its documented example is:\n\n```\n/ralph-loop \"Build a REST API for todos. Requirements: CRUD operations, input validation, tests. Output COMPLETE when done.\" --completion-promise \"COMPLETE\" --max-iterations 50\n```\n\nThe plugin uses the [completion phrase](https://github.com/anthropics/claude-plugins-official/blob/main/plugins/ralph-loop/README.md) to signal that Claude believes it has met the stated condition. Its [`--max-iterations` option](https://github.com/anthropics/claude-plugins-official/blob/main/plugins/ralph-loop/README.md) sets a limit, and `/cancel-ralph` cancels an active loop. These are plugin commands, not general Claude Code Stop-hook syntax. The [plugin README](https://github.com/anthropics/claude-plugins-official/blob/main/plugins/ralph-loop/README.md) advises stating concrete requirements, asking Claude to respond to test results, and telling it what to report if it reaches the limit.\n\nA completion phrase is still a signal from Claude, not independent evidence. For stronger verification, make the hook or a separate check inspect the test or build result before allowing the task to end.\n\nWrite a custom command hook when your completion rule needs project-specific checks, such as requiring both passing tests and a build artifact. The exact implementation depends on how the repository runs those checks and handles failures. Claude Code also supports prompt- and agent-based hooks; the official documentation describes [agent hooks](https://code.claude.com/docs/en/hooks) as an option when verification requires inspecting files or test output. Test the hook’s success and failure paths before relying on it.\n\nClaude Code documents a cap of **[eight consecutive Stop-hook continuations](https://code.claude.com/docs/en/hooks)**. The count resets when Claude calls a tool. This cap limits consecutive hook blocks; it does not mean Claude will finish in eight iterations. The documentation says you can raise the cap with `CLAUDE_CODE_STOP_HOOK_BLOCK_CAP`, but a higher cap does not make the task more likely to succeed. A plugin’s `--max-iterations` setting is a separate limit.\n\nIf you interrupt Claude, the loop stops running hooks. [Stop hooks do not run after a user interrupt](https://code.claude.com/docs/en/hooks), and API errors use the separate `StopFailure` event. Treat those as reasons to inspect the current state, rather than assuming the loop recovered or completed.\n\nKeep the task limited in scope; when Claude reaches that limit, have it summarize the failed check and remaining work if the checks are still broken or it cannot proceed. The plugin [keeps work in the current session](https://github.com/anthropics/claude-plugins-official/blob/main/plugins/ralph-loop/README.md), so previous changes remain available for later iterations. If you run parallel sessions, check how the installed plugin scopes its loop state: [a repository issue](https://github.com/anthropics/claude-plugins-official/issues/370) reports state affecting concurrent sessions. That report is a warning to verify the current setup, not evidence that every installation has the same problem.\n\nGive Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.\n\nTake a look at vroni.com", "url": "https://wpnews.pro/news/how-to-keep-claude-code-working-until-a-task-is-done-with-a-stop-hook-loop", "canonical_source": "https://www.vincentschmalbach.com/how-to-keep-claude-code-working-until-a-task-is-done-with-a-stop-hook-loop/", "published_at": "2026-12-02 14:15:04+00:00", "updated_at": "2026-12-02 14:42:56.573561+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "agent-protocols"], "entities": ["Anthropic", "Claude Code", "ralph-loop", "/goal", "Model Context Protocol"], "also_reported_by": [], "takeaway": "A Stop hook that returns {\"decision\": \"block\", \"reason\": \"...\"} keeps Claude Code working past an early stop, but the loop needs a verifiable check like a test command plus an iteration limit, since a completion phrase is Claude's own signal and not proof.", "recommendation": {"advice": "Gate Claude Code's Stop hook on real test or build output and cap iterations (ralph-loop's --max-iterations), rather than trusting a completion phrase as evidence the task is done.", "playbook": "coding-agents", "playbook_url": "https://wpnews.pro/playbooks/coding-agents"}, "alternates": {"html": "https://wpnews.pro/news/how-to-keep-claude-code-working-until-a-task-is-done-with-a-stop-hook-loop", "markdown": "https://wpnews.pro/news/how-to-keep-claude-code-working-until-a-task-is-done-with-a-stop-hook-loop.md", "text": "https://wpnews.pro/news/how-to-keep-claude-code-working-until-a-task-is-done-with-a-stop-hook-loop.txt", "jsonld": "https://wpnews.pro/news/how-to-keep-claude-code-working-until-a-task-is-done-with-a-stop-hook-loop.jsonld"}}