Overview
Run a long Claude Code refactor non-interactively on its own branch with a narrow tool allowlist, record every step to a log file, and review the log, tests and diff before anything is merged.
Problem or Objective
More in Coding and Code Review
AI Agents and Models
Tools and Integrations
- Tools
- Claude Code
- Prerequisites
- Claude Code with a Claude subscription or API key, a Git repository with a working test suite, and permission to add a hook to the project's Claude Code settings.
How It Works
Create a new branch or a separate clone for the refactor and make sure the test suite passes before you start. Add a hook in the project's Claude Code settings that appends each tool call and shell command to an audit log file. Then start the run with claude -p, giving a written goal and a stopping rule, an --allowedTools list that covers file edits and the test command but not deployment, pushing or network tools, and --output-format stream-json with --verbose redirected to a second log file. When it finishes, read the audit log for anything unexpected, run the tests yourself, and review the full diff before you merge.
Implementation Workflow
Steps assembled from the agents, tools and prompt of this entry. Adapt them to your own setup.
- Choose the agentSet up Claude Code.
- Connect the toolsConnect the tools this workflow relies on: Claude Code.
- Check the prerequisitesClaude Code with a Claude subscription or API key, a Git repository with a working test suite, and permission to add a hook to the project's Claude Code settings.
- Gather your detailsCollect what the prompt asks for: Module or folder to refactor, What the refactor should achieve, Paths the run may edit, Command that runs the tests, Retry limit for one failure.
- Run the starter promptPaste the starter prompt below into Claude Code and adapt it to your case.
- Check against the documentationCompare what you get with the documentation at code.claude.com before you rely on it.
Practical Starter Prompt: Refactor this module unattended and log every step
Fill the fields to adapt the prompt. Empty fields stay as visible [placeholders].
Refactor [Module or folder to refactor] to [What the refactor should achieve] without changing its public behaviour. Work only inside [Paths the run may edit]. After each logical step, run [Command that runs the tests] and fix failures before continuing. Do not push, deploy, install new dependencies or touch files outside the allowed paths. Stop when the tests pass and the goal is met, or after [Retry limit for one failure] attempts at the same failure, and finish with a summary of every file changed and why, so a person can review it before merging.Start on a separate branch with a narrow --allowedTools list and the logging hook in place. Afterwards read both logs, rerun the tests and review the diff before merging.
Expected Results and Benefits
This is a suggested workflow, not a reported result. The points below are what the cited documentation says the tools can do.
This is a suggested workflow, not a reported outcome. The Claude Code docs support non-interactive runs with claude -p, an --allowedTools flag that lets listed tools run without prompting, stream-json output, and hooks that run shell commands before or after Claude Code actions. How long a refactor takes and whether it stays on track depend on the codebase and the goal you write.
Limitations and Considerations
A -p run shows no trust dialog, so anything outside your --allowedTools list will simply not be allowed; keep the list narrow and leave out push and deploy commands. Run it on a branch or a copy so a bad result costs nothing. A passing test suite is not a full review: read the diff and the log before merging. Check your plan's limits or API budget before a long run. Results depend on your setup and were not independently reproduced by Top 200 Repos.
GitHub Repositories
AI Agent Repository
Source code of the agent that does the work.
Official Website
The official site of the main tool or project in this use case.
Visit Website (opens in a new tab)code.claude.com
Related Videos
About anthropics/claude-code
The SECRET Claude Code Upgrade That Has Everyone Going Nuts (Full Guide) (opens in a new tab)
The title names the project, the description links the project website.
Watch on YouTube (opens in a new tab)About anthropics/claude-code
The only Claude Code guide you'll ever need (Opus 4.5) (opens in a new tab)
The title names the project, the description links the project website.
Watch on YouTube (opens in a new tab)Independent creators. Listing a video implies no endorsement, sponsorship or partnership.
Sources
- Run Claude Code programmatically - Claude Code Docs (opens in a new tab)
Project documentation · code.claude.com · Undated · Source checked 26 Nov 2026, 17:27 UTC
What this source supports
- Use --allowedTools to let Claude use certain tools without prompting.
- Use --output-format stream-json with --verbose and --include-partial-messages to receive tokens as they’re generated.
- A -p session shows no workspace trust dialog and no per-server approval prompt.
- Overview - Claude Code Docs (opens in a new tab)
Project documentation · code.claude.com · Undated · Source checked 25 Nov 2026, 11:57 UTC
What this source supports
- Hooks let you run shell commands before or after Claude Code actions, like auto-formatting after every file edit or running lint before a commit.

