---
title: "Claude Code writes too many comments? Remove them · Selan"
description: "An agent that deletes comments that restate the code and keeps the ones that explain why. It runs on a small, fast model, from a pre-commit hook or in CI."
url: https://selan.ai/docs/agents/minimise-comments
updated: 2026-11-24T21:07:25+02:00
---

Agents

Docs · Agents · Minimise comments

# An agent that removes comments that restate the code

An agent that removes comments that restate the code, on a small, fast model.

## Why does Claude write so many comments?

Opus 5 explains what it is doing as it does it: `// increment the counter`, a JSDoc block that names each parameter after its own type, a `/* ===== HELPERS ===== */` banner over a section the code already names. **A comment that restates its line is a second copy of the line**. It is not updated when the line changes, and the reader who trusts it pays.

Deciding which comments to keep is a small, local judgement: one file, one comment, no architecture. A cheap model is good at it.

### Before

```typescript
// ============ HELPERS ============

/**
 * Adds one to n.
 * @param n the number
 * @returns the number plus one
 */
export const inc = (n: number) => n + 1

// Multiply by two
export const double = (n: number) => n * 2

// 250ms, because Safari fires two scroll events for one gesture and the second
// arrives ~180ms later.
export const SETTLE_MS = 250
```

### After

```typescript
export const inc = (n: number) => n + 1

export const double = (n: number) => n * 2

// 250ms, because Safari fires two scroll events for one gesture and the second
// arrives ~180ms later.
export const SETTLE_MS = 250
```

The Safari comment survives because nothing in the code says it: _why_ stays, _what_ goes.

## What goes in the agent file?

`.claude/agents/minimise-comments.md`, abridged. The full version adds the per-language rules.

```markdown
---
name: minimise-comments
description: Deletes comments that restate the code and keeps the ones that explain why. Edits files in place.
tools: Bash, Read, Grep, Glob, Edit
model: openrouter:google/gemma-4-31b-it
---

You remove comment noise. A comment earns its place by saying something the code
cannot: why this way, what breaks otherwise, which bug it works around.

Do nothing else. Not formatting, not renaming, not refactoring, not dead code.

**Delete whole lines. Never re-indent, re-wrap or re-space a line you keep.** A
diff of pure deletions is reviewable in a glance; one with a stray indentation
change in it is not, and it will be the reason someone stops trusting this agent.

Delete: a comment that says what the next line says; a JSDoc block whose every
tag repeats the signature; commented-out code; changelog comments — git knows.

Keep, and do not reword: anything answering **why**; a workaround or a runtime
quirk; a warning about what breaks if the code changes; the reason behind a
magic number, a regex, a unit, a timeout; TODO, FIXME and HACK.

When you cannot tell whether a comment is a why, **keep it**. Deleting a real
explanation costs far more than leaving a weak comment, and you will not be the
one paged when it turns out to have mattered.

Never delete — these are code, not comments: license headers, `@ts-expect-error`,
`eslint-disable`, `biome-ignore`, `prettier-ignore`, `<!--[if IE]>`, source-map
pragmas, and anything inside a string. `"// not a comment"` is a value.
```

-   **Deletions only, never a reflow.** A diff with stray whitespace changes in it gets approved without being read.
-   **When unsure, keep.** A weak comment left behind costs a line; a deleted explanation costs the next person a day.
-   **Directives are code.** `eslint-disable` and `@ts-expect-error` look like comments and are read by a compiler. Name them, or your build breaks.

**Your `AGENTS.md` or `CLAUDE.md` wins.** Claude Code loads it before the agent runs. Tell the agent to obey that file over its own instructions, and a repository that comments intent keeps doing so.

The model

## Which model should run it?

The work is mechanical and local, and if it runs on every commit the price is the feature. The agent names its model in the front matter, and every way of running it inherits that:

```markdown
model: openrouter:google/gemma-4-31b-it
```

A `provider:model` value runs the agent against **your company's own provider key**, under the provider you added it as. A value with no colon (`model: haiku`) is a plain Claude Code alias. The command is the same either way:

```bash
selan agent-run minimise-comments "…the files, editing them in place."
```

Naming the model in the file keeps the hook, the CI job and the terminal on the same one, and the choice is reviewed in the pull request that changes it. `--model` on the command overrides it for one run.

**It spends like any other run.** Same proxy, company settings and per-user limits; it lands in Usage and Logs under the token that ran it.

Where to run it

## How do I run it as a pre-commit hook?

A hook in the repository, and one line to turn it on.

```bash
#!/usr/bin/env bash

# Only files that are wholly staged. The agent edits the working tree and the
# commit is built from the index, so its edits have to be re-staged to land —
# and on a file that already had unstaged hunks, that `git add` would sweep
# work into the commit nobody chose to put there.
files=$(comm -23 \
  <(git diff --cached --name-only --diff-filter=ACM | sort) \
  <(git diff --name-only | sort))
[ -z "$files" ] && exit 0

command -v selan >/dev/null 2>&1 || exit 0

root=$(git rev-parse --show-toplevel)
selan agent-run minimise-comments \
  "Remove the comments that restate their code from the files listed below,
   editing them in place. Stage nothing, commit nothing, run no tests.
$(printf '%s\n' "$files" | sed "s|^|  $root/|")"

printf '%s\n' "$files" | while IFS= read -r f; do git add -- "$f"; done

exit 0
```

Save it as `.githooks/pre-commit`, make it executable, and point git at the directory. Putting that in `prepare` means an install turns it on:

```json
"scripts": {
  "prepare": "git config core.hooksPath .githooks || true"
}
```

-   **It never refuses a commit.** No selan on the machine, no staged files, a failed run: every path exits 0. A hook that blocks a commit teaches people `--no-verify`.
-   **It skips a file with unstaged hunks.** The agent edits the working tree; the commit is built from the index, so re-staging would put unchosen work in the commit.
-   **Absolute paths.** The prompt lists files under `git rev-parse --show-toplevel`: given a relative path, an agent that guesses the working directory wrong edits nothing and reports that it is done.
-   **It runs no tests.** The agent's own instructions say to verify before it finishes, which in a hook puts a suite in front of every commit. Removing a comment cannot change behaviour, and CI runs anyway.

**Read the first few diffs.** The hook rewrites files between `git commit` and the commit existing, so what lands is not quite what you staged.

## How do I run it on a pull request?

To rewrite nothing on your own machine, run the same agent in CI and let it push its deletions as their own commit: reviewable in the pull request, droppable with a revert.

```yaml
- uses: actions/checkout@v7
  with:
    ref: ${{ github.event.pull_request.head.ref }}
    fetch-depth: 0
    persist-credentials: false

- name: Minimise comments
  continue-on-error: true
  env:
    SELAN_TOKEN: ${{ secrets.SELAN_TOKEN }}
    BASE: ${{ github.base_ref }}
  run: |
    selan agent-run minimise-comments \
      "Minimise the comments in the files listed below, editing them in place.
       Commit nothing.
    $(git diff --name-only "origin/$BASE...HEAD" | sed "s|^|  $GITHUB_WORKSPACE/|")"
```

-   **`ref` is the branch**, not the merge ref: you cannot push a `refs/pull/N/merge` commit.
-   **`fetch-depth: 0`**: `origin/$BASE...HEAD` needs a merge base and the default shallow clone has none. Without it the diff errors, the list is empty, and the step stays green having edited nothing.
-   **Absolute paths**, under `$GITHUB_WORKSPACE`, as in the hook.
-   **Keep `persist-credentials: false`** and put the token only in the step that pushes: the agent has `Bash` and permissions skipped, so no `contents: write` credential should sit in `.git/config` while it runs.

**A push made with `GITHUB_TOKEN` triggers no workflows.** The commit it pushes is the one commit on the branch your test job never ran against. Run the checks in the same job before pushing, or accept the gap.

## Related

-   [Agents](https://selan.ai/docs/agents): the token and the pipeline every agent here shares.
-   [Code review](https://selan.ai/docs/agents/code-review): the agent that reviews every pull request.
-   [How to run Gemma 4 in Claude Code](https://selan.ai/guides/gemma-in-claude-code): the model this agent names, on an Ollama Cloud plan or OpenRouter.
-   [Practices](https://selan.ai/docs/practices): what your repository's sessions cost, and what to cut.
