---
title: "Maintaining Documentation at Scale"
description: "A comprehensive guide to automating documentation maintenance, keeping docs synchronized with code changes, and building sustainable documentation practices across your engineering organization."
sidebar:
  label: "Maintaining Documentation"
seo:
  description: "Complete guide to automating documentation maintenance, keeping docs in sync with code changes, and building a sustainable documentation culture with Autohand."
---

## The documentation challenge [#overview]

Documentation debt is one of the most pervasive problems in software engineering. Code evolves faster than documentation, leading to outdated guides, incorrect API references, and frustrated developers who stop trusting the docs altogether.

Autohand Code CLI has no dedicated `docs` command. You keep docs current with the same tools you use for code: headless prompts (`-p`), piped input, read-only runs (`--restricted`), reviewable patches (`--patch`), git worktrees, and project instructions in `AGENTS.md`. This guide covers strategies for:

- Detecting documentation drift from a git diff
- Generating documentation from code analysis
- Maintaining consistency across large documentation sets
- Building workflows that keep docs current with every release
- Measuring documentation health over time

## Auditing existing documentation [#documentation-audit]

Before improving your documentation, you need to understand its current state. Run a read-only audit with `--restricted`, which denies dangerous operations, so Autohand reads files without changing them:

```bash
autohand -p "Audit the Markdown files in docs/ against the code in src/. List
broken internal links, references to APIs that no longer exist, code examples
that would not compile, public APIs with no documentation, and outdated
version numbers. Give file and line for each issue and rate it critical,
warning or suggestion." --restricted
```

To get a report a script can read, describe its shape in a JSON Schema and pass `--output-schema`. The final answer must then be one JSON document that matches it:

```json
{
  "type": "object",
  "required": ["issues"],
  "properties": {
    "issues": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["file", "line", "severity", "message"],
        "properties": {
          "file": {"type": "string"},
          "line": {"type": "integer"},
          "severity": {"enum": ["critical", "warning", "suggestion"]},
          "message": {"type": "string"}
        }
      }
    }
  }
}
```

```bash
autohand -p "Audit docs/ against src/ and report every issue." \
  --restricted --output-schema docs-audit.schema.json --json local \
  | jq -r .content > docs-audit.json

# Count critical issues
jq '[.issues[] | select(.severity == "critical")] | length' docs-audit.json
```

On success, `content` holds the JSON document as a string, so it is parsed a second time. If the answer does not match the schema after one repair attempt, the command exits with status 1.

## Detecting documentation drift [#drift-detection]

Documentation drift occurs when code changes without corresponding documentation updates. Pipe the code diff into Autohand and ask which docs it affects. Piped input is added to the prompt as context:

```bash
# Changes since the last release tag
git diff "$(git describe --tags --abbrev=0)"..HEAD -- src/ \
  | autohand -p "Which files in docs/ describe behaviour this diff changes? For each, say what is now wrong." --restricted

# Changes between two commits
git diff abc123 def456 -- src/ \
  | autohand -p "List documentation in docs/ that is out of date after this diff." --restricted
```

Put the mapping between code and docs in `AGENTS.md` so every run uses it. Autohand reads `AGENTS.md` automatically, except with `--bare`:

```markdown
## Documentation

- `src/api/**` is documented in `docs/api/`.
- `src/components/**` is documented in `docs/components/`.
- `src/config/*.ts` is documented in `docs/configuration.md`.
- Changes to `*.test.ts`, `*.spec.ts` and `__mocks__/` never need doc updates.
```

## Automated documentation generation [#auto-generation]

Autohand can write documentation from your codebase. Generate it as a patch first, so you review the change before it lands.

### API reference generation

```bash
# Write the API reference as a patch, without changing files
autohand -p "Write Markdown reference docs in docs/api/ for every exported
function in src/api/, from the TypeScript types and JSDoc. Include one example
per function, taken from the tests where possible." \
  --patch --output api-docs.patch

git apply --check api-docs.patch && git apply api-docs.patch
```

### Configuration reference

```bash
autohand -p "Update docs/reference/configuration.md so it lists every option
in src/config/schema.ts with its type, default and description." --yes
```

### Component documentation

For larger jobs, run the session in its own git worktree so the main checkout stays clean:

```bash
autohand --worktree component-docs
```

Then ask, for example: "For each component in `src/components/`, document its props, events and slots in `docs/components/`, using the examples in the matching `.stories.ts` file."

## Maintaining code examples [#code-examples]

Code examples are the most likely documentation to become outdated.

### Executable examples

Ask Autohand to check examples against the current code. `--restricted` keeps the check read-only:

```bash
# Report examples that no longer match the code (read-only)
autohand -p "Check every TypeScript code block in docs/ against the current
exports in src/. List each example that would fail and why." --restricted

# Fix them as a reviewable patch
autohand -p "Fix the TypeScript code blocks in docs/ that no longer match src/." \
  --patch --output fix-examples.patch
```

### Extracted examples

Instead of maintaining examples in Markdown, keep them in tested source files and copy them into the docs:

```typescript
// src/examples/authentication.example.ts
// @doc-example: authentication/basic-login

import { auth } from './auth';

async function basicLogin() {
  const session = await auth.login({
    email: 'user@example.com',
    password: 'secure-password'
  });

  console.log('Logged in:', session.user.id);
}
```

```bash
autohand -p "For each file in src/examples/ with a @doc-example tag, replace
the matching code block in docs/ with the file's current contents." --yes
```

## Documentation versioning [#versioning]

When docs for each version live on their own branch, compare and backport with git, and use Autohand for the judgement calls:

```bash
# What changed in the docs between two release branches
git diff release-3.x main -- docs/ \
  | autohand -p "Summarise the documentation changes between v3 and v4 and list fixes that should be backported to v3." --restricted

# Backport on the release branch, as a patch you review
git switch release-3.x
autohand -p "Apply the fix from commit abc123 to docs/ on this branch, adjusting for v3 APIs." \
  --patch --output backport.patch
```

## Enforcing style consistency [#style-consistency]

Consistent documentation is easier to read and maintain. Write your style guide in Markdown and keep it in the repository:

```markdown
# docs/STYLE.md

- Headings use sentence case and go no deeper than level 4.
- Every code block names its language: bash, typescript, yaml or json.
- Prefer relative links between pages.
- Keep sentences under 35 words. Avoid "simply", "just", "obviously" and "easy".
- Write "repository", "configuration" and "authentication" in full.
```

Reference it from `AGENTS.md` for interactive sessions, or pass it with `--append-system-prompt-file`, which also works with `--bare`:

```bash
# Report style problems
autohand -p "Check docs/ against the style guide and list every violation." \
  --append-system-prompt-file docs/STYLE.md --restricted

# Fix them as a patch
autohand -p "Fix style guide violations in docs/." \
  --append-system-prompt-file docs/STYLE.md --patch --output style.patch
```

For mechanical rules such as broken links or heading levels, a dedicated linter (markdownlint, Vale, lychee) is faster and repeatable. Use Autohand for the rules a linter cannot check.

## CI/CD integration [#ci-integration]

Run a read-only docs check on pull requests. With `--bare`, the CLI authenticates from environment variables and skips `AGENTS.md`, so the style guide is passed explicitly:

```yaml
# .github/workflows/docs.yml
name: Documentation

on:
  pull_request:
    paths:
      - 'docs/**'
      - 'src/**'

jobs:
  docs-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Autohand
        run: curl -fsSL https://autohand.ai/install.sh | sh

      - name: Check docs against the change
        env:
          AUTOHAND_PROVIDER: autohandai
          AUTOHAND_API_KEY: ${{ secrets.AUTOHAND_API_KEY }}
          AUTOHAND_AI_API_KEY: ${{ secrets.AUTOHAND_API_KEY }}
        run: |
          git diff "origin/${{ github.base_ref }}"...HEAD \
            | autohand --bare -p "List documentation in docs/ that this diff makes wrong, with file and line." \
                --append-system-prompt-file docs/STYLE.md \
                --restricted --output-schema docs-audit.schema.json --json local \
            | jq -r .content > docs-report.json
          jq -e '[.issues[] | select(.severity == "critical")] | length == 0' docs-report.json

      - name: Upload report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: docs-report
          path: docs-report.json
```

## Measuring documentation health [#metrics]

Use the same `--output-schema` approach to produce numbers you can track over time. Save each run's JSON and compare it with the last one in your own scripts or dashboards.

```bash
autohand -p "Measure the documentation in docs/ against src/: count public
APIs with and without docs, code examples that match the code and those that
do not, and pages not updated since the code they describe last changed." \
  --restricted --output-schema docs-metrics.schema.json --json local \
  | jq -r .content > metrics-$(date +%F).json
```

Key metrics to track:

- **Coverage**: Percentage of public APIs with documentation
- **Freshness**: Time since documentation was last updated relative to code changes
- **Accuracy**: Number of valid vs broken code examples
- **Completeness**: Sections present vs expected based on templates
- **Readability**: Sentence length and complexity

Freshness is cheaper to measure with git than with a model, for example `git log -1 --format=%cs -- docs/api.md` compared with the same command on the code it describes.

## Documentation workflows [#workflows]

Establish workflows that make documentation a natural part of development rather than an afterthought.

### Pull request integration

Ask Autohand whether a pull request needs doc updates and post the answer as a comment. This step uses the GitHub CLI:

```yaml
      - name: Check whether docs are needed
        env:
          AUTOHAND_PROVIDER: autohandai
          AUTOHAND_API_KEY: ${{ secrets.AUTOHAND_API_KEY }}
          AUTOHAND_AI_API_KEY: ${{ secrets.AUTOHAND_API_KEY }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git diff "origin/${{ github.base_ref }}"...HEAD \
            | autohand --bare -p "This diff changes src/api/, src/public/ or config/schema.ts. Does it need documentation updates? If so, list the files and what to change. Reply in Markdown." \
                --restricted --json local \
            | jq -r .content > docs-comment.md
          gh pr comment "${{ github.event.pull_request.number }}" --body-file docs-comment.md
```

The job needs `pull-requests: write` permission to comment.

### Release documentation

```bash
# Release notes from commits
git log v3.0.0..v3.1.0 --oneline \
  | autohand -p "Write release notes for v3.1.0 from these commits, grouped by feature, fix and breaking change." --json local \
  | jq -r .content > release-notes.md

# Update version references across the docs, as a patch
autohand -p "Update version references in docs/ from 3.0 to 3.1 where they describe the current release." \
  --patch --output version-bump.patch
```

## Best practices

Recommendations from teams that have successfully scaled their documentation practices:

- **Start with coverage**: Focus on documenting public APIs first, then expand to internal documentation.
- **Automate ruthlessly**: Any documentation that can be generated from code should be generated from code.
- **Make it easy**: Reduce friction for contributors by providing templates, linting, and clear guidelines.
- **Review docs like code**: Include documentation in code review and apply the same quality standards.
- **Measure and iterate**: Track metrics and continuously improve based on data.
- **Own the process**: Assign documentation ownership to teams, not individuals.
