Skip to content

Generate and verify documentation with Cursor's agent

Cursor’s agent writes documentation by reading the code that the documentation describes: TSDoc for every public export, a getting-started guide lifted from passing tests, Mermaid architecture diagrams, and a migration guide built from git diff. The agent does the extraction. Mechanical gates — TypeDoc validation, compiled examples, rendered diagrams, and link checks — prove the result, so humans edit only the “why”.

This page is for developers and tech leads who own a library or service whose documentation lags the code. Version 2.0 ships next week with 40 breaking changes, the README still shows a v1 example, and the architecture lives in one senior engineer’s head. You cannot write four documents by hand before the release, and you cannot proofread four generated ones line by line either.

  • A read-only audit prompt that ranks undocumented exports by how often your own code imports them.
  • Prompts for TSDoc, a getting-started guide, an architecture overview, and a migration guide, each tied to a source of truth the agent must read.
  • A CI job that fails when an export loses its documentation, an example stops compiling, or a link breaks.
  • A one-line check that proves a “documentation-only” change touched no code.
  • The failure modes of generated docs and how to recover from each.

Which documentation should the agent write, and from what?

Section titled “Which documentation should the agent write, and from what?”

Generated documentation is only as trustworthy as the source it was generated from. Before you prompt, decide which artifact is the source of truth for each document and which gate proves the document still matches it.

DocumentSource of truth the agent readsGate that proves itWhat a human still writes
TSDoc on public exportsImplementation and its testsTypeDoc --validation.notDocumentedIntent: when to use it, and when not to
Getting-started guidePassing testsExtracted examples type-checkThe order of concepts for a newcomer
Architecture overviewImport graph and entry pointsDiagrams render; every claim cites a fileDesign decisions and rejected options
Migration guidegit diff between release tagsThe v1 test suite run against v2; each failure is a documented breakBehaviour changes with an unchanged signature

The last column is where your review time goes. The agent cannot know why a module exists or why you rejected the obvious design, so do not ask it to guess. Ask it to leave a marked gap instead, and fill the gap yourself.

Generate documentation with Cursor’s agent, step by step

Section titled “Generate documentation with Cursor’s agent, step by step”

Work on a branch, one document per agent conversation. A fresh conversation per document keeps the context small and makes each diff reviewable on its own.

Audit documentation gaps before you generate anything

Section titled “Audit documentation gaps before you generate anything”

Start in Plan Mode, which Cursor describes as creating “detailed implementation plans before writing any code” (Cursor docs, checked 2026-08-28). The audit reads and reports without changing code, so a wrong assumption costs you a re-run rather than a revert.

The import count is a proxy for user impact that the agent can compute from the code. The “cite a file path” rule makes every finding checkable: open the cited file, and the claim is either there or it is not.

Set a documentation rule so every conversation follows the same standard

Section titled “Set a documentation rule so every conversation follows the same standard”

Before the generation passes, capture your conventions as a project rule, so that the second and tenth conversation write the same way the first one did. See project rules in Cursor for where rules live and how they are applied, and custom rules and templates for team-wide versions.

# Documentation standard
- Document exported symbols only. Never document private helpers.
- One-sentence summary in the present tense, then @param, @returns, @throws.
- Every @example imports from the package name, not from a relative path,
and must compile as written.
- Never change executable code while documenting. If the code looks wrong,
add a line to docs/doc-questions.md and move on.
- Where intent cannot be read from code, write TODO(docs-why) instead of guessing.

Save it as a project rule from Cursor’s rules settings; the project rules page gives the file location.

The last two lines matter most. The first stops “documentation” commits from hiding behaviour changes. The second turns the agent’s blind spot into a searchable list for the human pass.

Switch to the agent for the write pass, and give it the prioritized list from the audit.

TypeDoc 0.28 (checked 2026-09-26) warns on each undocumented export when you pass --validation.notDocumented, and --treatValidationWarningsAsErrors turns those warnings into a non-zero exit code. --emit none skips writing the HTML site, so the command works as a gate.

Then check that the pass changed only comments. Run this in the terminal on the agent’s branch, after it commits:

Terminal window
git diff -U0 main...HEAD -- src | grep -E '^[+-][^+-]' | grep -vE '^[+-][[:space:]]*(\*|/\*\*|\*/|//)' || echo "comments only"

Any line it prints is a changed line of code inside a “documentation” commit. Revert that hunk, or move it to its own reviewed change. The check is a quick filter, not proof: it skips changed lines that start with + or - (such as ++i;), so still skim the diff.

Turn passing tests into a getting-started guide

Section titled “Turn passing tests into a getting-started guide”

Your test suite already contains working calls with realistic inputs. That makes it a better source for tutorial examples than the agent’s imagination.

The source comment gives the reviewer a way to check a sample without reading it: open the named test and confirm the call matches. The code-fence rule is what the CI example check in the next section extracts.

Generate an architecture overview that cites its evidence

Section titled “Generate an architecture overview that cites its evidence”

Architecture documents go stale faster than any other kind, because no single code change looks like it touches them. Generate the overview so that you can regenerate it cheaply, and keep it short enough that people read it.

Render the diagrams before you commit them. The Mermaid CLI treats a Markdown input file as a set of diagrams and renders each fenced mermaid block (@mermaid-js/mermaid-cli 12.0.0, checked 2026-09-26). It drives a headless Chrome through Puppeteer, so run it on a machine or CI image that has one:

Terminal window
npx -p @mermaid-js/mermaid-cli mmdc -i docs/architecture.md -o /tmp/architecture-rendered.md

A diagram that fails to render here is broken for most readers, and you find out before they do. For deeper architecture work, including decision records, see architecture planning with Cursor.

Build the migration guide from the release diff

Section titled “Build the migration guide from the release diff”

Run this in the agent, not in Plan Mode, because the prompt writes docs/migration-v2.md. Replace the two tags with your release tags.

Section 5 is the one that saves you. Diffs expose signature changes reliably; a function that keeps its signature and returns something different is exactly what a generated guide misses, so the prompt makes the agent list the candidates for you to judge. For large upgrades of your own dependencies rather than your users’, see framework and library migrations with Cursor.

How do you verify generated docs without reading every line?

Section titled “How do you verify generated docs without reading every line?”

Review the generated text where judgment is needed, and let machines check everything else. Put these gates in CI so they run on every pull request, not only on the day you generated the docs.

  1. Coverage. TypeDoc validation fails the build when a new export lands without documentation.
  2. Examples compile. A small script extracts every code fence tagged ts from docs/ and README.md and type-checks it against the package source (the package name resolves to src/index.ts).
  3. Links resolve. markdown-link-check fails on a dead link, including links to files and anchors you renamed.
  4. Diagrams render. mmdc renders every Mermaid block (see the architecture-overview section).
  5. Human sign-off. The owner of the module reviews only the TODO(docs-why) gaps, the “Behaviour changes” section, and the audit’s list of symbols that no longer exist. That is a short, scoped review, not a read of every generated page.

Ask the agent to write the example extractor, because it is ordinary code with a clear contract:

Then wire the gates into a workflow. This job reads the repository only, holds no secrets, and runs no agent:

.github/workflows/docs-check.yml
name: docs-check
on: pull_request
permissions:
contents: read
jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: actions/setup-node@v7
with:
node-version: 22
- run: npm ci
- run: npx typedoc --emit none --validation.notDocumented --treatValidationWarningsAsErrors src/index.ts
- run: npx tsx scripts/check-doc-examples.ts
- run: npx markdown-link-check -q -c .markdown-link-check.json README.md
- run: find docs -name "*.md" -print0 | xargs -0 -n1 npx markdown-link-check -q -c .markdown-link-check.json

markdown-link-check (3.15.0) also requests external URLs by default, which makes a pull-request gate flaky. Give it a .markdown-link-check.json with an ignorePatterns entry for ^https?://, so this gate checks only local files and anchors, and check external links in a separate, non-blocking scheduled job.

The job above leaves out the Mermaid gate; add the mmdc command from the architecture-overview section as its own step once Puppeteer’s Chrome runs on your runner. For review on the pull request itself, Bugbot reviews the diff like any other change. It adds to the gates above; it does not replace them.

When agent-generated documentation goes wrong

Section titled “When agent-generated documentation goes wrong”

The agent documents behaviour the code does not have. It inferred a parameter’s meaning from its name, or it described a retry that does not exist. Recovery: require a cited file path per claim, as the prompts above do, and spot-check three citations per document. If one is wrong, discard the section and regenerate it with the implementation file named explicitly in the prompt.

Examples look right but do not compile. Missing imports, relative paths, or a setup step the test had in beforeEach. Recovery: the example checker names the file and line; paste its output back into the agent with “fix the documentation, not the code”.

A documentation commit changed code. The agent “fixed” a bug it noticed while reading. Recovery: the comments-only check above prints the offending lines. Revert them, and move the suspected bug to docs/doc-questions.md for a separate, tested change.

The architecture diagram is an unreadable hairball. A full import graph of a large codebase has hundreds of edges. Recovery: cap the node count in the prompt, as above, and draw one diagram per subsystem rather than one for everything.

The migration guide misses a behaviour change. The signature stayed the same, so the diff looked harmless. Recovery: run the v1 test suite against the v2 build; every failure that is not a documented break belongs in “Behaviour changes”.

Generated docs are long and nobody reads them. Agents write to fill space unless you stop them. Recovery: set a word limit in every prompt, and cut any section a reader would skip before you merge.

The docs drift again three months later. Recovery: keep the CI gates on, and regenerate on a schedule. A Cursor Automation with a Scheduled trigger can rerun the audit prompt weekly; run the TSDoc prompt on what it flags, and send the result through the same gates as a normal pull request.

Where to go next with documentation in Cursor

Section titled “Where to go next with documentation in Cursor”

If your guide covers integrations with third-party libraries, a documentation MCP server gives the agent their current docs instead of what the model remembers.