Using Documentation as Effective AI Context
Documentation as context means encoding project-specific knowledge — build commands, code style, architectural decisions — into files like CLAUDE.md, .cursor/rules, and AGENTS.md that the AI reads automatically at the start of every session, instead of rediscovering it by reading code. Effective versions stay specific, brief, and current, since a bloated or stale file gets ignored or actively misleads the assistant.
You just onboarded a new developer. They spend their first week asking the same questions: “How do I run the tests?” “What’s the deployment process?” “Why do we use this pattern instead of that one?” Now imagine that developer asks those same questions every single morning because they forget overnight.
That is what working with an AI coding assistant feels like without documentation-as-context. Every new session, the AI starts from zero. It does not know your build commands, your team’s conventions, or your architectural decisions. It rediscovers them by reading files — burning context tokens on information you could have told it in 10 lines.
What you’ll walk away with from documentation as context
Section titled “What you’ll walk away with from documentation as context”- A template for each tool’s configuration file (CLAUDE.md, .cursor/rules, AGENTS.md)
- Guidelines for what to include and what to leave out
- Prompts for bootstrapping documentation from an existing codebase
- A strategy for keeping documentation current as the project evolves
The Three Instruction Systems
Section titled “The Three Instruction Systems”Each tool has its own mechanism for persistent, session-level documentation. Despite different names, they serve the same purpose: giving the AI project-specific knowledge it cannot infer from code alone.
Project Rules live in .cursor/rules/ as markdown files. They can be scoped by file pattern, applied always, or invoked manually. Cursor supports both .md (plain, always-applied text) and .mdc (carries frontmatter for description, globs, and alwaysApply). Only .mdc files honor that metadata — in a plain .md rule the frontmatter is ignored and the file is applied as-is.
.cursor/rules/ code-style.mdc # alwaysApply: true in frontmatter testing.mdc # Applied to test files (via globs) api-conventions.mdc # Agent-decided (via description) deployment.md # Plain rule, @-mention to invoke manuallyCursor also supports User Rules (global preferences in Cursor Settings) and AGENTS.md as a simpler alternative to .cursor/rules. Cursor reads AGENTS.md natively in the project root and in any subdirectory, applying it automatically when you work with files in that directory or its children.
Key capabilities:
- Glob-scoped rules: Apply only when working with matching files
- Agent-decided rules: Applied when Cursor determines they are relevant
- Team Rules: Organization-wide rules managed from the dashboard (Team/Enterprise plans)
- Remote rules: Import rules from GitHub repositories that stay synced
CLAUDE.md files are the primary instruction mechanism. Claude reads them at the start of every session.
project/ CLAUDE.md # Team-shared project instructions CLAUDE.local.md # Your personal preferences (gitignored) .claude/ CLAUDE.md # Alternative location rules/ code-style.md # Modular rules testing.md # Topic-specific guidelines api/ conventions.md # Path-specific rulesKey capabilities:
- Hierarchical loading: CLAUDE.md files above your working directory load in full at launch; files in subdirectories below it load on demand when Claude reads a file there
- Path-specific rules: Use YAML frontmatter with
pathsfield for conditional rules — these are the only rules that stay out of context until a matching file is touched - Imports: Reference other files with
@path/to/filesyntax. Note that imports are expanded and loaded at launch, so they help organization but do not reduce context - Auto memory: Claude writes its own notes to
~/.claude/projects/<project>/memory/ - User-level rules:
~/.claude/CLAUDE.mdapplies to all projects
AGENTS.md files provide instructions at global and project levels. Codex reads them at the start of every session.
~/.codex/ AGENTS.md # Global defaults for all repos AGENTS.override.md # Temporary global override
project/ AGENTS.md # Project-level instructions services/ payments/ AGENTS.override.md # Service-specific overridesKey capabilities:
- Precedence chain: Global, then root, then nested — closer files override earlier ones
- Override files:
AGENTS.override.mdtakes priority overAGENTS.mdin the same directory - Fallback filenames: Configure custom instruction file names (e.g.,
TEAM_GUIDE.md) - Size limit: 32 KiB by default, configurable via
project_doc_max_bytes. It covers the project files only — the global~/.codex/AGENTS.mddoes not count against it. The file that crosses the limit is prefix-truncated and everything after it is skipped, and because files load root-first the ones nearest your startup directory are the ones cut. The loader warns, but never in the TUI
What to Include
Section titled “What to Include”The golden rule: if removing this line would cause the AI to make a mistake, keep it. If the AI already does this correctly without the line, delete it.
Always Include
Section titled “Always Include”| Category | Example |
|---|---|
| Build commands | npm run build, make test, docker compose up |
| Test commands | npm test -- --testPathPattern=auth, pytest -x |
| Code style rules that differ from defaults | ”Use single quotes”, “2-space indentation” |
| Architectural patterns | ”Repository pattern for data access”, “All API routes in src/pages/api/“ |
| Non-obvious constraints | ”Redis must be running for integration tests”, “Use pnpm, not npm” |
| Environment setup | ”Run cp .env.example .env before first build” |
Never Include
Section titled “Never Include”| Category | Why |
|---|---|
| Standard language conventions | The AI already knows them |
| File-by-file descriptions | The AI can read the files |
| Long tutorials or explanations | Too much text causes the AI to ignore important rules |
| Information that changes frequently | It will become stale and mislead the AI |
| Self-evident practices | ”Write clean code” adds nothing |
Writing Effective Rules
Section titled “Writing Effective Rules”The difference between documentation that works and documentation the AI ignores comes down to specificity and brevity.
Bad: Vague and Verbose
Section titled “Bad: Vague and Verbose”# Code QualityWe care deeply about code quality. Always write clean, maintainable,well-documented code that follows best practices. Make sure to handleerrors properly and write tests for your code.Good: Specific and Actionable
Section titled “Good: Specific and Actionable”# Code Style- Use ES modules (import/export), not CommonJS (require)- Prefer async/await over .then() chains- Error responses: { error: string, code: number } shape
# Testing- Run single tests with: npm test -- --testPathPattern=<name>- Never mock the database in integration tests- Test file location: src/**/__tests__/<name>.test.ts
# Workflow- Run npm run type-check after making code changes- NEVER commit to main directly. Always create a branch.Split rules into focused files. To control when a rule applies via description or globs, the file must use the .mdc extension (e.g. api-conventions.mdc) — frontmatter in a plain .md rule is ignored:
---description: "API endpoint conventions"globs: - "src/api/**/*.ts" - "src/routes/**/*.ts"---
# API Conventions- All endpoints return { data: T } on success, { error: string } on failure- Use Zod for request validation- Include rate limiting middleware on all public endpoints- Reference @src/api/users.ts as the canonical exampleKeep your root CLAUDE.md concise. Use imports for detailed docs:
# Project: Acme APISee @README.md for project overview.See @package.json for available commands.
# Commands- Build: npm run build- Test: npm test -- --testPathPattern=<name>- Lint: npm run lint
# Conventions- TypeScript strict mode, no @ts-ignore- All API routes in src/pages/api/- Database queries use Drizzle ORM (see @src/lib/db/schema.ts)Use .claude/rules/ for modular, topic-specific rules. Path-specific rules use YAML frontmatter:
---paths: - "src/api/**/*.ts"---
# API Rules- Validate all input with Zod schemas- Return consistent error shapesKeep AGENTS.md focused on the most critical information:
# Acme API
## Commands- Build: npm run build- Test: npm test -- --testPathPattern=<name>- Lint: npm run lint
## Working Agreements- Always run tests after modifying code- Use pnpm for dependency management- TypeScript strict mode, no any types- API routes follow RESTful conventions in src/routes/Use nested AGENTS.md for service-specific overrides that should not apply globally.
Keeping Documentation Current
Section titled “Keeping Documentation Current”Documentation that falls out of date is worse than no documentation — it actively misleads the AI.
- Review monthly, and re-review whenever you change models. The model switch is the trigger that actually matters — a stronger model needs less scaffolding than the one you wrote the file for, and a cheaper one needs more.
- Treat it like code. Check instruction files into git. Review changes in PRs. Let the team contribute.
- Add a rule only after the same mistake happens twice. After a difficult debugging session it is tempting to ask the AI to write a rule immediately, but a single failure is noise. Every line is read on every turn forever, so a speculative rule costs you permanently for a problem you may not have.
- Delete more than you add. Prune on a schedule rather than only when something breaks. Anthropic’s own context engineering post for the Claude 5 generation describes removing over 80% of Claude Code’s system prompt with no measurable loss, and members of the team have been reported as recommending you periodically delete your instruction files entirely and add back only what demonstrably breaks. Treat that stronger version as a reported recommendation rather than documented policy — the official docs still frame it as adding a rule after the same mistake happens twice. Pruning CLAUDE.md and AGENTS.md walks through the full protocol either way.
- Watch for ignored rules. If the AI keeps violating a rule, the file is probably too long and the rule is getting lost. Prune aggressively.
When documentation as context breaks down
Section titled “When documentation as context breaks down”The file is too long and rules get ignored. This is the most common failure, and the fix is counterintuitive: shorten the file, do not add emphasis. Rewriting the rule in bold with “IMPORTANT” in front of it is the instinctive move and it rarely works, because adding instructions degrades adherence to all of them, not just the new ones. The actual fix is usually to delete four other rules. Move detailed guidelines to topic-specific files (.claude/rules/ with a paths glob, scoped .cursor/rules/), and if a rule genuinely must be obeyed rather than considered, make it a hook instead of a sentence.
Different team members add contradictory rules. Treat instruction files like code: review changes, resolve conflicts, keep one source of truth. In Claude Code, use the root CLAUDE.md for team-shared rules and CLAUDE.local.md for personal preferences.
The AI follows outdated rules. If your testing framework changed but your instruction file still references the old one, the AI will use the wrong commands and get confused. Audit regularly.
Too many rules files in a monorepo. With nested instruction files across packages, the combined context can exceed limits. In Codex, project_doc_max_bytes (32 KiB default) caps the total across the project files — the crossing file is prefix-truncated and every file after it is skipped, with no warning anywhere in the TUI. Because files are concatenated root-first, the ones cut are those nearest your startup directory, so you lose your most specific instructions while the general root-level ones survive. In Claude Code, only the first 200 lines of auto-memory MEMORY.md are loaded. Keep each file focused.