LIVEReading: Auto-document your code with AiderTotal time: 12 minSteps: 5Worked first time: 70% LIVEReading: Auto-document your code with AiderTotal time: 12 minSteps: 5Worked first time: 70%
CBW
Mediumgithub.com/Aider-AI/aider2026-05-20

Auto-document your code with Aider

Aider is a terminal-based AI pair programmer that edits your code in place. Point it at a function and tell it to add comments — it reads your existing style and matches it. Git-aware, every edit is a commit you can revert.

// Build stats

  • Total time12 min
  • Number of steps5
  • DifficultyMedium
  • Worked first time70%
// Before you start

What you need

  • Python 3.10 or newer
  • An LLM API key — Claude (Anthropic) is the recommended default, OpenAI/DeepSeek also work
  • A git repository to point Aider at (it refuses to edit uncommitted work)
  • Comfort with a terminal — Aider has no GUI
  • A specific function or file you want commented (works better than 'comment the whole repo')
01
Step 1 of 5

Install Aider via pipx

2 min

pipx installs Python CLI tools in isolated environments so you can run `aider` from anywhere without polluting your project venvs. If you don't have pipx, install it once and it pays back forever. Plain `pip install aider-chat` also works inside an existing venv if you prefer.

Terminal · mac
$ # Install pipx if you don't already have it
$ python3 -m pip install --user pipx
$ python3 -m pipx ensurepath
$
$ # Open a new terminal so PATH refreshes, then:
$ pipx install aider-chat
$
$ # Verify
$ aider --version
What you should see
`aider --version` prints a version number (e.g. 'aider 0.X.Y'). Running `aider --help` shows the full options list.
This might happen

`aider: command not found` after install.

pipx put it on a PATH that's not active in your current shell. Either start a new terminal, or run `python3 -m pipx ensurepath` and re-source your shell config (`source ~/.zshrc` or `~/.bashrc`).

02
Step 2 of 5

Set your LLM API key

1 min

Aider needs an API key for its model. Claude Sonnet is the documented default for best code quality. Aider reads ANTHROPIC_API_KEY, OPENAI_API_KEY, or DEEPSEEK_API_KEY from the environment — set whichever matches the model you'll use.

Terminal · mac
$ # Anthropic (recommended for code):
$ export ANTHROPIC_API_KEY=sk-ant-...
$
$ # Or OpenAI:
$ export OPENAI_API_KEY=sk-...
$
$ # Persist by adding to ~/.zshrc or ~/.bashrc.
$ # Optional: put it in ~/.aider.conf.yml so aider auto-loads:
$ echo 'anthropic-api-key: sk-ant-...' > ~/.aider.conf.yml
What you should see
`echo $ANTHROPIC_API_KEY` returns your real key. Subsequent `aider` runs no longer prompt for credentials.
This might happen

Aider says 'API key not set' even though you exported it.

You exported in one terminal but ran aider in another. Either source your shell rc, or use ~/.aider.conf.yml so the key is picked up everywhere.

03
Step 3 of 5

Start Aider in your git repo

1 min

cd into the project you want to edit. Aider needs a clean (or at least committed) git state — it makes a new commit for every change so you always have an undo. Pass the file you want it to focus on as an argument; Aider treats those as the 'in-chat' files it's allowed to read and write.

Terminal · mac
$ cd path/to/your/repo
$
$ # Make sure your work is committed first
$ git status
$
$ # Start aider, scoped to one file
$ aider src/utils/parsers.py
$
$ # Or multiple files
$ aider src/utils/parsers.py src/utils/formatters.py
What you should see
Aider prints a banner with the model in use, the repo it detected, and which files are 'in the chat'. You drop into a prompt that starts with `>` or `aider>`.
This might happen

Aider warns 'repo has uncommitted changes — won't run'.

Commit (or stash) first. Aider's git-aware safety net assumes a clean baseline. If you really want to skip the check, pass `--no-git`, but you lose the auto-undo benefit.

04
Step 4 of 5

Ask Aider to add comments matching your existing style

1-3 min

Inside Aider's prompt, type a natural instruction. The trick for 'comment this code' is telling it to match the style already in the file — otherwise it produces generic JSDoc/Javadoc no matter the language. Aider responds with a diff, asks for confirmation, and on yes commits the change. The commit message is auto-generated.

Terminal · mac
$ # Inside aider's prompt:
$ > Add docstring comments to every function in parsers.py. Match the existing comment style — look at how the top of the file is commented and follow that exactly. Don't change any code logic.
$
$ # After Aider shows the diff:
$ > y
$
$ # Now check what it did
$ /exit
$ git log -1 --stat
What you should see
Aider prints a diff with new comment lines only (no functional code changes). After `y`, it creates a git commit titled something like 'docs: add docstrings to parsers.py'. `git log -1 --stat` shows the commit.
This might happen

Aider rewrites code logic in addition to adding comments.

Use the /undo command (which reverts the last Aider commit) and re-prompt with stronger constraints: 'Only add comments above existing code. Do not change indentation, order, or any code character.' For surgical edits, `--edit-format whole` or `--edit-format diff` change how aggressively it reshapes files.

05
Step 5 of 5

Review the commit, refine, and repeat

open-ended

Every Aider change is a real git commit on your real branch. Treat it exactly like a coworker's PR — read the diff before merging upstream. If something is off, either /undo inside Aider or `git revert <sha>` outside. To do another file, just /add it into the chat and run another instruction. Aider's whole UX is: small chats, small commits, easy revert.

Terminal · mac
$ # Useful in-chat commands:
$ # /add src/utils/another.py add a file to the chat
$ # /drop src/utils/parsers.py remove from chat
$ # /undo revert the last aider commit
$ # /diff show what aider proposes before y/n
$ # /tokens see token usage so far
$ # /clear reset chat history (saves tokens)
$ # /exit
$
$ # Outside aider, regular git also works:
$ git log --oneline -10
$ git revert <sha-to-undo>
What you should see
Your codebase has a clean trail of small, reviewable commits — each one a focused comment-only change. You can ship them as a single PR or split them by file.
This might happen

Aider's commits are large and lumpy.

Run one file (or one function) at a time. Use `/clear` between unrelated tasks so the chat context doesn't bleed across changes. Smaller chats = smaller commits.

// Status

cooked. baked. worked.

A git-clean repo where Aider has added comments to your chosen file in your existing style, each change a separate revertible commit you can review before pushing.

// the honest bit

The honest part

Heads up — drafted from Aider's docs, not a CBW hands-on run. Aider is excellent but model-quality-dependent: with Claude Sonnet or GPT-4 you'll match this guide's 70% workedPct; with cheaper models it drops fast. Also: 'match the existing comment style' works better in well-commented files. If a file has only a few comments to learn from, Aider will invent a style. For larger refactors than commenting, plan for token spend — long sessions can run $5-20 in API costs. Cursor (the IDE) does similar work inside a UI and is worth comparing if you prefer that workflow over a terminal.