Search for AI Courses, Tech News and, Blogs

How To Write Documentation For Your Codebase With AI

by Jon Weatherhead | 5 days ago | 10 min read

The repo was not a toy. Around 60,000 lines of Django and React, three dead feature flags, and a payments module nobody wanted to touch. The first request was a README, and the model returned a clean draft in about ninety seconds. It also confidently listed a configuration flag that has never existed in that project. That pattern held for weeks. Fast and tidy on the "what", shaky on the "why", and perfectly willing to make things up when the code did not say enough.

To write documentation for your codebase with AI and have it survive a real reader, the trick is knowing exactly where the model earns its keep and where it quietly lies. Below is the split, a workflow that holds up, the prompts worth copying, and an honest verdict.

Short version

AI writes excellent first drafts of README files, docstrings, and changelogs. It cannot tell you why the code is shaped the way it is, and it will invent flags, endpoints, and commands that look right. Use it for the grunt work, run every command it gives you, and write every "why" yourself.

README first draft

~90 sec

Config options real on pass one

4 of 11

Code samples safe to use unchecked

0

What AI is genuinely good at (and where it falls over)

After enough passes, the strengths and the failures sorted themselves into two clear lists. Nothing here is theoretical. Each item cost time or saved it on the actual repo.

Does this well

  • Turning a function body into a clear, accurate docstring
  • Drafting a README skeleton so there is never a blank page
  • Explaining unfamiliar code in plain English
  • Generating usage examples straight from a signature
  • Standardizing tone across hundreds of old comments
  • Summarizing what a module does from its imports

Struggles with

  • Knowing why a decision was made in the first place
  • Telling live code from dead code without being told
  • Resisting the urge to invent config options and flags
  • Cross-referencing files it was never actually shown
  • Security caveats and the sharp edges that bite
  • Staying correct a week later once the code moves

Match the doc type to the method

Treating every document the same way is the fastest route to bad output. Some kinds are almost fully in the model's wheelhouse. Others only look finished until a reader tries to follow them. This is roughly how the common types sorted out.

Doc typeHow much AI helpsWhat stays on you
README and getting startedStrong first draftRun every command, cut the filler
Inline docstringsVery strongSpot-check edge cases and error paths
API referenceStrong when types or a spec existFix anything built from untyped code
Tutorials and how-to guidesStructures the steps wellPerform the steps yourself, every one
Architecture notes and ADRsWeak on reasoningSupply the why, the tradeoffs, rejected options
Changelog and release notesStrong from clean git historyDecide what the reader actually cares about
Runbooks and troubleshootingWeak without incident historyAdd the real failure modes you have hit

A workflow that holds up

Dropping a request into a chat window and pasting the result gets you confident, wrong documentation. These six steps, in this order, cut revision time by more than half on the test repo.

1

Feed it real context, not a description

Give the model the actual files: the folder tree, package.json or pyproject.toml, and the modules to cover. Describing your architecture in a sentence and asking it to document "a typical service like this" is how you get generic text that fits no one. The more real code it sees, the less it has to invent.

2

Ask for structure before prose

Request an outline first: headings only, no body. Missing sections and bad ordering get caught in seconds, before the model has written three paragraphs under the wrong heading. Approve the skeleton, then let it fill each section. This single habit prevents the most wasted output.

3

Generate docstrings in a focused pass

Do docstrings file by file, with one fixed format (Google style, NumPy, or JSDoc) stated in the prompt. Tell it not to change any logic and to flag anything it cannot infer rather than guess. This was the clearest win of the whole project, close to publishable on the first try.

4

Run every command and sample it gives you

This is the step with no shortcut. Copy each install line, each code sample, each curl request, and actually execute it. On the test repo the model mixed up package managers and used a flag from an older version of a tool, both of which read perfectly on the page. If only one rule here gets adopted, adopt this one.

5

Add the "why" yourself

The model handles "what this does". The team owns "why it is like this". Why the payments module retries three times, why a flag exists, which approach was rejected and what it cost. None of that lives in the code, so the model cannot read it, and a new hire needs it more than anything else.

6

Standardize voice in a final pass

Once the content is correct, run everything through one more prompt that enforces a consistent heading order, tense, and level of detail. Keeping a short style file (covered below) and pasting it in each time stops ten separate docs from sounding like ten separate people.

One thing that changes the output: your tool

The assistant you point at the repo shapes the result as much as the prompt does, mostly in how much of the codebase it can hold in context at once. If you are choosing one for this, Claude Code and Cursor take noticeably different approaches to multi-file work, and that gap shows up fast when documenting across dozens of files.

Prompts worth copying

Vague prompts produce vague docs. The detail that mattered most across all of these was telling the model to flag uncertainty instead of filling the gap with a guess. Paste these in and adjust the format to your stack.

Docstrings for a file

Add Google-style docstrings to every public function and class in this file. Document each parameter, the return value, and any exception raised. Do not change, reorder, or "improve" any logic. If a parameter's purpose is not clear from the code itself, write "TODO: confirm" instead of guessing. Return the full file.

README from the actual repo

Read package.json, its scripts section, and the src directory. Draft a README with these headings: What this service does, Requirements, Install, Running locally, Configuration, Common tasks. Pull every command and script name directly from package.json. For any section you cannot fill from the files I gave you, write "TODO" with a one-line note on what is missing. Do not invent environment variables or flags.

Changelog from git history

Below is the git log for the last release. Group the commits into Added, Changed, Fixed, and Removed. Write each entry in plain language for someone who uses the product, not for engineers. Skip merge commits, version bumps, and formatting-only changes. If a commit message is unclear, list it under "Needs review" rather than guessing what it did.

English module summary

Explain what this module is responsible for in four sentences, for a developer who has never seen this codebase. Describe what calls it and what it depends on, using only the imports and references in the file. Do not speculate about parts of the system that are not shown here.

Where it breaks, and how to catch it

Every one of these showed up on the test repo. They are easy to miss precisely because the output reads so fluently. Treat fluent as a warning sign, not a stamp of approval.

Invented configuration

It listed flags and environment variables that looked entirely plausible and did not exist. The reason is baked into how generative AI actually works: it predicts the most likely next token, it does not look anything up, so a flag that "should" exist gets written as if it does.

Commands that never run

Install steps with the wrong package manager, or a flag borrowed from an older release of a tool. Perfect on the page, broken in the terminal. The only defense is to run them.

Confident fiction for "why"

Ask why a design choice was made and the model will write a clean, reasonable rationale that is pure invention. It has no access to the meeting where the call was made. Never let it author intent.

Silent staleness

The doc is correct the day it is generated and wrong after the next refactor. A point-in-time snapshot ages badly unless something keeps pulling it back in line.

Keeping the docs from rotting

Generating docs once is the easy part. Keeping them true as the code moves is where most teams quietly give up. Tying documentation to the moments code already changes is what makes it stick.

When this happensDo this
A public function's signature changesRegenerate its docstring in the same pull request
A new environment variable is addedThe PR template asks for a one-line doc entry
A release is cutGenerate the changelog from the tagged range, then edit
Any AI-written doc is mergedA human reviews it like code, not as an afterthought

One small practice tied all of this together: a short DOCS_STYLE.md holding the voice, the heading order, and one worked example. Pasting it into every prompt kept output consistent and prevented the final cleanup pass from drifting into a rewrite.

The honest verdict

Is it worth doing? Yes, with eyes open. The time saved is real and not small. First drafts that used to eat an afternoon take minutes, and docstrings come back close to finished on the first pass. A README still needs trimming, but it beats the blank page every time.

The catch is that the parts a new hire needs most never come from the model. Why the payments module is built the way it is, which flags are safe to flip, what breaks at 2am: all of that has to come from the people who wrote it, because none of it is written anywhere in the code for the model to find. The most useful way to think about the tool is a fast junior writer. It types quickly, never admits uncertainty, and needs an editor who actually knows the system.

So, in one line: AI is worth using to write documentation for your codebase, on one condition. Run every command it hands you, and own every "why" yourself. Skip either and the result is not saved time, it is confident, wrong docs, which is worse than having none at all.