Documentation
Guides an AI agent to produce accurate, maintainable documentation by first grounding itself in the actual codebase.
Purpose
Create documentation that is true to the code, easy to navigate, and cheap to keep updated. Avoids the common failure of writing plausible but incorrect docs from memory or assumptions.
When to Use
- User asks to write, update, or improve docs (
README,CONTRIBUTING, API reference, guides,docs/pages) - User asks to document a module, endpoint, function, or workflow
- User says "explain this codebase" or "onboard me to this repo"
- User requests ADRs, architecture docs, or runbooks
- Keywords:
documentation,docs,README,guide,API docs,explain,onboard
Workflow
1. Discover before writing
- Inventory the codebase: entry points, top-level folders, package manifests (
package.json,go.mod,pyproject.toml, etc.), routing/config files - Check for existing docs:
README.md,docs/,CONTRIBUTING.md,CHANGELOG.md, inline comments - Identify the primary audience: new contributor, end user, operator, or API consumer — scope and tone follow from this
- Do not write yet. Grounding first prevents hallucinated endpoints, env vars, or commands.
2. Read the source of truth
- For each doc section you plan to write, open the corresponding source file(s) and extract facts:
- Commands actually in
package.jsonscripts /Makefile/justfile - Routes/handlers actually registered, not guessed
- Env vars actually read (
process.env,os.Getenv, etc.) - Dependencies and versions from lockfiles/manifests
- Commands actually in
- Note any contradictions between existing docs and code — surface them to the user
3. Design the doc structure
Choose the minimal set needed; avoid duplicating information:
- README.md — what it is, why it matters, quick start, links to deeper docs (keep under ~150 lines; details live in
docs/) - API reference — endpoints/operations, auth, request/response examples, error codes (derive from OpenAPI/spec or handler code, not memory)
- Guides / How-tos — task-oriented ("Deploy to Fly.io", "Add a new skill")
- Architecture / ADRs — why the system is shaped this way (link to
docs/architecture.md) - Reference — configuration, env vars, CLI flags (table format)
Include a table of contents when the doc exceeds ~80 lines.
4. Write with accuracy and navigability
- Use absolute file references with line numbers where helpful:
`src/auth/login.ts:42` - Provide copy-pasteable commands verified in step 2; never invent flags
- Show real examples: actual request/response payloads, not placeholder
foo/barunless clearly labeled as schematic - Keep language concise, active, and consistent (present tense for behavior, imperative for instructions)
- Add diagrams (Mermaid) only when they clarify a non-obvious flow
Templates:
README quick-start:
## Quick Start
\`\`\`bash
git clone <repo>
cd <repo>
npm ci # verified from package.json:12
npm run dev # starts http://localhost:3000
\`\`\`
API endpoint:
### POST /api/habits
Auth: Bearer token
Request: { "title": "string", "frequency": "daily|weekly" }
Response 201: { "id": "uuid", "title": "..." }
Errors: 400 validation, 401 unauthorized
5. Verify and maintain
- Click every link; run every command sequence in a clean checkout when feasible
- Remove or fix stale sections you discovered in step 2
- Leave a maintenance note where docs are likely to drift ("Source:
src/routes.ts:18-45— update here if routes change") - If the repo has a docs linter or link checker, run it
- Optional Jev gate (skipped without
JEV_API_KEY): send the doc diff + source facts tojev_review. Full protocol: contribute Jev gates. Never send secrets.
Instructions
- Never document from memory. Every command, env var, flag, and endpoint must be traced to a file:line you read.
- Prefer discovery via tools. Use glob/grep to inventory files, read to extract facts, bash to verify commands.
- Minimize duplication. If
package.jsonalready declares the dev command, the README should call it out once and point to the file — don't copy-paste a second source of truth. - Match the project's voice. Mirror tone and formatting from existing docs; don't introduce a new style guide mid-repo.
- Keep diffs focused. When updating docs, preserve surrounding formatting and TOC; only change what is inaccurate or missing.
- Flag drift explicitly. If code and existing docs disagree, say so: "> Note:
READMEsaysnpm startbutpackage.json:8definesnpm run dev— docs updated to match."
Constraints
- Do NOT invent APIs, env vars, commands, or file paths not found in the codebase
- Do NOT write documentation before reading relevant source files
- Do NOT duplicate large code blocks into docs when a file reference suffices
- Do NOT use placeholder examples that could be mistaken for real usage without a clear label
- Do NOT over-document: skip internal helpers that are not part of the public surface unless requested
Examples
Example 1 — Create a README from scratch
User: "Document this repo — there's no README yet."
Agent:
- Globs
**/*.{json,md,toml,yaml}, readspackage.json,src/index.ts:1-30,CONTRIBUTING.md - Drafts
README.mdwith: description frompackage.json:3, quick start frompackage.json:12-18, project structure from directory listing - Verifies
npm run devactually starts the server before committing the example
Example 2 — Update API docs
User: "Update docs/api.md — we added a new endpoint
POST /teams."
Agent:
- Reads
src/routes/teams.ts:10-55, extracts auth, validation, response code - Updates
docs/api.mdwith thePOST /teamssection using the exact schema fromsrc/schemas/team.ts:5-20 - Runs Markdown link check, confirms no broken anchors
Example 3 — Fix stale documentation
User: "The README says
yarn startbut we migrated to pnpm."
Agent:
- Reads
README.md:24,package.json:10(pnpm dev),pnpm-lock.yamlexistence - Patches
README.md:24topnpm dev, adds note aboutpnpm installvsnpm ci - Greps for other occurrences of
yarn startacrossdocs/and fixes them in the same pass
References
- Works alongside
project-architecture(architecture docs) andcode-review(flags missing docs) - For skill docs, see
CONTRIBUTING.md→ "How to Create a New Skill" forSKILL.mdstructure