refactor

refactor is a command for coding agents from halflength-ampleness75/claude-code-recipes. It costs 14 tokens per session (762 once invoked), scanned A, original, MIT.

A structured process for improving existing code without changing what it is meant to do. It examines the target code, its callers, dependencies, and tests before proposing small changes.

In plain words
What is it for?
Use it to reduce complexity, remove duplication, improve names, separate responsibilities, or move code while checking the affected tests.
Why use it?
Refactoring can accidentally break other parts of an application when connected code and test coverage are overlooked. This process makes those connections and checks visible before changes are made.

Command

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/halflength-ampleness75/claude-code-recipes/refactor
Clone the repo
git clone --depth 1 https://github.com/halflength-ampleness75/claude-code-recipes

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for refactor

README.md
[![agentmods](https://agentmods.dev/badge/commands/halflength-ampleness75/claude-code-recipes/refactor.svg)](https://agentmods.dev/commands/halflength-ampleness75/claude-code-recipes/refactor)
Your own site
<a href="https://agentmods.dev/commands/halflength-ampleness75/claude-code-recipes/refactor"><img src="https://agentmods.dev/badge/commands/halflength-ampleness75/claude-code-recipes/refactor.svg" alt="Measured on agentmods" height="20"></a>
Per session 14 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 762 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00014 $0.00762
Opus 5 $0.00007 $0.00381
Sonnet 5 $0.00003 $0.00152
Haiku 4.5 $0.00001 $0.00076

Measured 3d ago against content hash 03d29110e39e, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

refactor scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

commands/refactor.md · 80 lines

How it starts

The opening of the file, as written. The whole thing — 80 lines — stays where its author put it; the contents beside it link to each section on GitHub.

You are a senior software engineer performing a structured refactoring. The user will describe what they want refactored via $ARGUMENTS (a file, function, module, or pattern).

Phase 1 — Understand the Target

  1. Locate the code the user wants refactored. Use glob and grep to find relevant files.
  2. Read the target code and all direct dependents (callers, importers, subclasses).
  3. Check for existing tests that cover the target code.
  4. Map out the dependency graph: what does this code depend on, and what depends on it?

Produce a brief summary:

  • Target: file(s) and function(s)/class(es) being refactored
  • Dependents: list of files/modules that import or call the target
  • Test coverage: which tests exist, what they cover
  • Current issues: why refactoring is needed (complexity, duplication, coupling, naming, etc.)

Phase 2 — Propose a Refactoring Plan

Propose a numbered plan. Each step should be a single, atomic change. Common refactoring patterns to consider:

  • Extract Function/Method — pull a block into its own function
  • Inline Function — replace a trivial wrapper with its body
  • Rename — improve naming for clarity
  • Move — relocate to a more appropriate module
  • Extract Interface/Type — decouple via an abstraction
  • Replace Conditional with Polymorphism — simplify complex switch/if chains
  • Introduce Parameter Object — group related parameters
  • Remove Dead Code — delete unreachable or unused code
  • Decompose Module — split a large file into focused modules

For each step, state:

  1. What changes
  2. Which files are affected
  3. Why it improves the code

Present the plan and ask the user to confirm before proceeding. Wait for their response.

Phase 3 — Execute the Refactoring

After the user confirms, implement the plan step by step:

  1. Make one atomic change at a time.
  2. After each change, verify the code is syntactically valid. If a build command is available (check package.json scripts, Makefile, Cargo.toml, etc.), run it.
  3. Keep all existing tests passing throughout. Run the test suite after each significant step.
  4. Update imports, references, and type signatures in all dependent files.
  5. Update any documentation or comments that reference renamed/moved entities.

Read the full file on GitHub · 80 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 80 lines · 14 tokens per session scan A 03d29110e39e

Subscribe to this mod's changes

refactor is a command published in the GitHub repository halflength-ampleness75/claude-code-recipes (2 stars, last pushed 4d ago), licensed MIT. It adds 14 tokens to every session and 762 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.