Agent Docs
Write documentation that AI agents can efficiently consume. Based on Vercel benchmarks and industry standards (AGENTS.md, llms.txt, CLAUDE.md).
The Hybrid Context Hierarchy
Three-layer architecture for optimal agent performance:
Layer 1: Constitution (Inline)
Always in context. 2,000–4,000 tokens max.
CODEBLOCK0
Include:
- - Security rules, architecture constraints
- Build/test/lint commands (top for primacy bias)
- Documentation map (where to find more)
Layer 2: Reference Library (Local Retrieval)
Fetched on demand. 1K–5K token chunks.
- - Framework-specific guides
- Detailed style guides
- API schemas
Layer 3: Research Assistant (External)
Gated by allow-lists. Edge cases only.
- - Latest library updates
- Stack Overflow for obscure errors
- Third-party llms.txt
Why This Works
Vercel Benchmark (2026):
| Approach | Pass Rate |
|---|
| Tool-based retrieval | 53% |
| Retrieval + prompting |
79% |
|
Inline AGENTS.md |
100% |
Root cause: Meta-cognitive failure. Agents don't know what they don't know—they assume training data is sufficient. Inline docs bypass this entirely.
Core Principles
1. Compressed Index > Full Docs
An 8KB compressed index outperforms a 40KB full dump.
Compress to:
- - File paths (where code lives)
- Function signatures (names + types only)
- Negative constraints ("Do NOT use X")
2. Structure for Chunking
RAG systems split at headers. Each section must be self-contained:
CODEBLOCK1
Rules:
- - Front-load key info (chunkers truncate)
- Descriptive headers (agents search by header text)
3. Inline Over Links
Agents can't autonomously browse. Each link = tool call + latency + potential failure.
| Approach | Token Load | Agent Success |
|---|
| Full inline | ~12K | ✅ High |
| Links only |
~2K | ❌ Requires fetching |
| Hybrid | ~4K base | ✅ Best of both |
4. The "Lost in the Middle" Problem
LLMs have U-shaped attention:
- - Strong: Start of context (primacy)
- Strong: End of context (recency)
- Weak: Middle of context
Solution: Put critical rules at TOP of AGENTS.md. Governance first, details later.
5. Signal-to-Noise Ratio
Strip everything that isn't essential:
- - No "Welcome to..." preambles
- No marketing text
- No changelogs in core docs
Formats like llms.txt and AGENTS.md mechanically increase SNR.
llms.txt Standard
Machine-readable doc index for agents:
CODEBLOCK2
Location: /llms.txt at domain root
Companion: /llms-full.txt — full concatenated docs, HTML stripped
Security Considerations
Inline = Trusted
AGENTS.md is part of your codebase. Controlled, version-pinned.
External = Attack Surface
- - Indirect prompt injection via hidden text
- SSRF risks if agents can browse freely
- Dependency on external uptime
Mitigation: Domain allow-lists, human-in-the-loop for external retrieval.
Anti-Patterns
- 1. Pasting 50 pages — triggers "Lost in the Middle"
- "See external docs" — agents can't browse autonomously
- Generic advice — "Write clean code" (use specific constraints)
- TOC-only docs — indexes without content
- Trusting retrieval alone — 53% vs 100% pass rate
Advanced Patterns
For detailed guidance on RAG optimization, multi-framework docs, and API templates, see references/advanced-patterns.md.
Validation Checklist
- - [ ] Critical governance at TOP of doc
- [ ] Total inline context under 4K tokens
- [ ] Each H2 section self-contained
- [ ] No external links without inline summary
- [ ] Negative constraints explicit ("Do NOT...")
- [ ] File paths and signatures, not full code
Agent Docs
编写AI代理能够高效消费的文档。基于Vercel基准测试和行业标准(AGENTS.md、llms.txt、CLAUDE.md)。
混合上下文层级结构
为优化代理性能而设计的三层架构:
第一层:基本准则(内联)
始终在上下文中。 最多2,000–4,000个token。
markdown
AGENTS.md
上下文:Next.js 16 | Tailwind | Supabase
🚨 关键规则
📚 文档索引(使用read_file)
- - 认证:docs/auth/llms.txt
- 数据库:docs/db/schema.md
包含内容:
- - 安全规则、架构约束
- 构建/测试/代码检查命令(置于顶部以利用首因效应)
- 文档地图(在哪里找到更多信息)
第二层:参考库(本地检索)
按需获取。 1K–5K token的片段。
第三层:研究助手(外部)
通过白名单控制。 仅处理边缘情况。
- - 最新库更新
- Stack Overflow上的晦涩错误
- 第三方llms.txt
为何有效
Vercel基准测试(2026年):
79% |
|
内联AGENTS.md |
100% |
根本原因: 元认知失败。代理不知道它们不知道什么——它们假设训练数据足够。内联文档完全绕过了这个问题。
核心原则
1. 压缩索引优于完整文档
8KB的压缩索引优于40KB的完整转储。
压缩为:
- - 文件路径(代码所在位置)
- 函数签名(仅名称和类型)
- 负面约束(不要使用X)
2. 为分块而结构化
RAG系统在标题处进行分割。每个部分必须自包含:
markdown
数据库设置 ← 分块边界
先决条件:PostgreSQL 14+
- 1. 创建数据库...
规则:
- - 前置关键信息(分块器会截断)
- 描述性标题(代理按标题文本搜索)
3. 内联优于链接
代理无法自主浏览。每个链接 = 工具调用 + 延迟 + 潜在失败。
| 方法 | Token负载 | 代理成功率 |
|---|
| 完全内联 | ~12K | ✅ 高 |
| 仅链接 |
~2K | ❌ 需要获取 |
| 混合 | ~4K基础 | ✅ 两者最佳 |
4. 中间迷失问题
LLM具有U形注意力:
- - 强: 上下文开头(首因)
- 强: 上下文结尾(近因)
- 弱: 上下文中间
解决方案: 将关键规则放在AGENTS.md的顶部。先治理,后细节。
5. 信噪比
剔除所有非必要内容:
- - 没有欢迎来到...的前言
- 没有营销文本
- 核心文档中没有变更日志
像llms.txt和AGENTS.md这样的格式机械地提高了信噪比。
llms.txt标准
面向代理的机器可读文档索引:
markdown
项目名称
一行项目描述。
认证
- - 设置:环境变量和初始化
- 服务器:Cookie处理
数据库
位置: 域名根目录下的 /llms.txt
配套文件: /llms-full.txt — 完整拼接的文档,已去除HTML
安全考虑
内联 = 可信
AGENTS.md是代码库的一部分。受控、版本锁定。
外部 = 攻击面
- - 通过隐藏文本进行间接提示注入
- 如果代理可以自由浏览,存在SSRF风险
- 依赖外部服务可用性
缓解措施: 域名白名单,外部检索时人工参与。
反模式
- 1. 粘贴50页 — 触发中间迷失
- 参见外部文档 — 代理无法自主浏览
- 泛泛建议 — 编写干净代码(应使用具体约束)
- 仅目录文档 — 没有内容的索引
- 仅依赖检索 — 53%对比100%的通过率
高级模式
关于RAG优化、多框架文档和API模板的详细指南,请参见 references/advanced-patterns.md。
验证清单
- - [ ] 关键治理规则位于文档顶部
- [ ] 总内联上下文不超过4K token
- [ ] 每个H2部分自包含
- [ ] 没有不带内联摘要的外部链接
- [ ] 负面约束明确(不要...)
- [ ] 使用文件路径和签名,而非完整代码