Skip to content

llms.txt 示例

本文档提供完整的 llms.txt 实现示例,可直接复用。

示例 A:文档站最小版

适用于小型文档站,快速启用 LLM 友好索引。

txt
# AI Docs

> Last updated: 2026-03-03
> Language: zh-CN

## Start Here
- https://example.com/
- https://example.com/ai-best-practices/index
- https://example.com/agent-foundations/index

## Role Paths
- https://example.com/ai-learning/role-based-paths
- https://example.com/agent-foundations/for-developers-and-pm

示例 B:带完整版入口

适用于内容较多的站点,提供完整版索引。

txt
# AI Docs for LLMs

> Last updated: 2026-03-03
> Language: zh-CN
> Full index: https://example.com/llms-full.txt

## Quick Start
- https://example.com/start/getting-started
- https://example.com/install/index

## Core Concepts
- https://example.com/ai-best-practices/index
- https://example.com/agent-foundations/index

## Reference
- https://example.com/resources/curated-sources
- https://example.com/resources/prompt-templates

## Full Index
- https://example.com/llms-full.txt

示例 C:按角色组织

适用于多角色受众的文档站。

txt
# Agent Engineering Docs

> Last updated: 2026-03-03
> Language: zh-CN

## Overview
- https://example.com/

## For Developers
- https://example.com/agent-foundations/mcp/server-design
- https://example.com/agent-foundations/skills/how-to-write-skill
- https://example.com/agent-foundations/llms-txt/how-to-write-llms-txt
- https://example.com/ai-best-practices/engineering-playbook

## For Product Managers
- https://example.com/agent-foundations/for-developers-and-pm
- https://example.com/agent-foundations/mcp/dev-pm-collaboration
- https://example.com/ai-best-practices/role-driven-lifecycle
- https://example.com/ai-best-practices/evaluation

## For DevOps
- https://example.com/ai-best-practices/safety-governance
- https://example.com/ai-best-practices/multi-agent-collaboration

## For Learners
- https://example.com/ai-learning/how-to-learn-with-ai
- https://example.com/ai-learning/role-based-paths
- https://example.com/ai-learning/study-workflows

示例 D:按任务组织

适用于任务导向的文档站。

txt
# AI Engineering Guide

> Last updated: 2026-03-03
> Language: zh-CN

## Getting Started
- https://example.com/start/getting-started
- https://example.com/install/index

## Build an Agent
- https://example.com/agent-foundations/skills/index
- https://example.com/agent-foundations/mcp/index
- https://example.com/ai-best-practices/prompting

## Add RAG
- https://example.com/ai-best-practices/context-and-rag
- https://example.com/ai-best-practices/fast-rag

## Evaluation & Quality
- https://example.com/ai-best-practices/evaluation
- https://example.com/ai-best-practices/safety-governance

## Production
- https://example.com/ai-best-practices/engineering-playbook
- https://example.com/ai-best-practices/multi-agent-collaboration

## Troubleshooting
- https://example.com/ai-best-practices/faq

示例 E:完整版 llms-full.txt

提供完整内容索引,用于深度检索。

txt
# AI Docs - Full Index

> Last updated: 2026-03-03
> Language: zh-CN
> Page count: 45

## AI Best Practices
- https://example.com/ai-best-practices/index
- https://example.com/ai-best-practices/prompting
- https://example.com/ai-best-practices/context-and-rag
- https://example.com/ai-best-practices/fast-rag
- https://example.com/ai-best-practices/evaluation
- https://example.com/ai-best-practices/engineering-playbook
- https://example.com/ai-best-practices/safety-governance
- https://example.com/ai-best-practices/multi-agent-collaboration
- https://example.com/ai-best-practices/role-driven-lifecycle
- https://example.com/ai-best-practices/faq

## Agent Foundations
- https://example.com/agent-foundations/index
- https://example.com/agent-foundations/for-developers-and-pm
- https://example.com/agent-foundations/skills/index
- https://example.com/agent-foundations/skills/what-is-skill
- https://example.com/agent-foundations/skills/skill-structure
- https://example.com/agent-foundations/skills/how-to-write-skill
- https://example.com/agent-foundations/skills/best-practices
- https://example.com/agent-foundations/skills/examples
- https://example.com/agent-foundations/llms-txt/index
- https://example.com/agent-foundations/llms-txt/what-is-llms-txt
- https://example.com/agent-foundations/llms-txt/spec-and-format
- https://example.com/agent-foundations/llms-txt/how-to-write-llms-txt
- https://example.com/agent-foundations/llms-txt/best-practices
- https://example.com/agent-foundations/llms-txt/examples
- https://example.com/agent-foundations/mcp/index
- https://example.com/agent-foundations/mcp/what-is-mcp
- https://example.com/agent-foundations/mcp/architecture
- https://example.com/agent-foundations/mcp/server-design
- https://example.com/agent-foundations/mcp/security
- https://example.com/agent-foundations/mcp/operations
- https://example.com/agent-foundations/mcp/dev-pm-collaboration

## AI Learning
- https://example.com/ai-learning/index
- https://example.com/ai-learning/how-to-learn-with-ai
- https://example.com/ai-learning/role-based-paths
- https://example.com/ai-learning/study-workflows
- https://example.com/ai-learning/common-mistakes

## Resources
- https://example.com/resources/curated-sources
- https://example.com/resources/agent-infra-sources
- https://example.com/resources/prompt-templates
- https://example.com/resources/weekly-update-checklist

示例 F:自动生成脚本

使用脚本自动生成 llms.txt

javascript
// scripts/generate-llms-txt.mjs
import fs from 'node:fs'
import path from 'node:path'
import matter from 'gray-matter'

const DOCS_DIR = './apps/docs'
const OUTPUT_FILE = './public/llms.txt'
const OUTPUT_FULL_FILE = './public/llms-full.txt'

// 页面分类配置
const CATEGORIES = {
  'AI Best Practices': {
    path: 'ai-best-practices',
    priority: ['index', 'prompting', 'evaluation', 'engineering-playbook']
  },
  'Agent Foundations': {
    path: 'agent-foundations',
    priority: ['index', 'for-developers-and-pm']
  },
  'AI Learning': {
    path: 'ai-learning',
    priority: ['index', 'how-to-learn-with-ai']
  },
  'Resources': {
    path: 'resources',
    priority: ['curated-sources', 'prompt-templates']
  }
}

// 角色路径配置
const ROLE_PATHS = {
  'For Developers': [
    'agent-foundations/mcp/server-design',
    'agent-foundations/skills/how-to-write-skill',
    'ai-best-practices/engineering-playbook'
  ],
  'For Product Managers': [
    'agent-foundations/for-developers-and-pm',
    'ai-best-practices/role-driven-lifecycle',
    'ai-best-practices/evaluation'
  ]
}

function getMarkdownFiles(dir) {
  const files = []
  const entries = fs.readdirSync(dir, { withFileTypes: true })

  for (const entry of entries) {
    const fullPath = path.join(dir, entry.name)
    if (entry.isDirectory()) {
      files.push(...getMarkdownFiles(fullPath))
    }
    else if (entry.name.endsWith('.md')) {
      files.push(fullPath)
    }
  }
  return files
}

function parseFrontmatter(filePath) {
  const content = fs.readFileSync(filePath, 'utf-8')
  const { data, content: body } = matter(content)
  return {
    title: data.title || extractTitle(body),
    description: data.description || extractDescription(body),
    path: filePath
      .replace(DOCS_DIR, '')
      .replace(/\\/g, '/')
      .replace('/index.md', '/')
      .replace('.md', ''),
    priority: data.priority || 0
  }
}

function extractTitle(content) {
  const titleLine = content.split('\n').find(line => line.startsWith('# '))
  return titleLine ? titleLine.slice(2) : 'Untitled'
}

function extractDescription(content) {
  const lines = content.split('\n').filter(l => l.trim() && !l.startsWith('#'))
  return lines[0]?.substring(0, 160) || ''
}

function generateLlmsTxt(pages) {
  const today = new Date().toISOString().split('T')[0]

  let output = `# AI Docs for LLMs

> Last updated: ${today}
> Language: zh-CN
> Full index: https://docs.example.com/llms-full.txt

`

  // 快速开始
  output += `## Quick Start
- https://docs.example.com/start/getting-started
- https://docs.example.com/install/index

`

  // 按分类组织
  for (const [name, config] of Object.entries(CATEGORIES)) {
    const categoryPages = pages
      .filter(p => p.path.startsWith(`/${config.path}`))
      .sort((a, b) => {
        const aPriority = config.priority.indexOf(a.path.split('/').pop())
        const bPriority = config.priority.indexOf(b.path.split('/').pop())
        return (aPriority === -1 ? 999 : aPriority) - (bPriority === -1 ? 999 : bPriority)
      })

    if (categoryPages.length > 0) {
      output += `## ${name}\n`
      for (const page of categoryPages.slice(0, 5)) {
        output += `- https://docs.example.com${page.path}\n`
      }
      output += '\n'
    }
  }

  // 角色路径
  for (const [role, paths] of Object.entries(ROLE_PATHS)) {
    output += `## ${role}\n`
    for (const p of paths) {
      output += `- https://docs.example.com/${p}\n`
    }
    output += '\n'
  }

  return output
}

function generateLlmsFullTxt(pages) {
  const today = new Date().toISOString().split('T')[0]

  let output = `# AI Docs - Full Index

> Last updated: ${today}
> Language: zh-CN
> Page count: ${pages.length}

`

  // 按目录分组
  const grouped = {}
  for (const page of pages) {
    const category = page.path.split('/')[1] || 'root'
    if (!grouped[category]) {
      grouped[category] = []
    }
    grouped[category].push(page)
  }

  // 输出
  for (const [category, categoryPages] of Object.entries(grouped)) {
    output += `## ${formatCategory(category)}\n`
    for (const page of categoryPages) {
      output += `- https://docs.example.com${page.path}\n`
    }
    output += '\n'
  }

  return output
}

function formatCategory(slug) {
  return slug
    .split('-')
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ')
}

// 主函数
function main() {
  console.log('Scanning docs directory...')
  const files = getMarkdownFiles(DOCS_DIR)
  console.log(`Found ${files.length} markdown files`)

  const pages = files
    .map(parseFrontmatter)
    .filter(p => !p.path.includes('/private/') && !p.path.includes('/draft/'))

  console.log('Generating llms.txt...')
  const llmsTxt = generateLlmsTxt(pages)
  fs.writeFileSync(OUTPUT_FILE, llmsTxt)

  console.log('Generating llms-full.txt...')
  const llmsFullTxt = generateLlmsFullTxt(pages)
  fs.writeFileSync(OUTPUT_FULL_FILE, llmsFullTxt)

  console.log('Done!')
  console.log(`- ${OUTPUT_FILE}`)
  console.log(`- ${OUTPUT_FULL_FILE}`)
}

main()

package.json 配置

json
{
  "scripts": {
    "generate:llms": "node scripts/generate-llms-txt.mjs",
    "build": "pnpm generate:llms && vite build"
  }
}

常见错误示例

❌ 错误示例 1:链接全是首页

txt
# 错误示例

## Pages
- https://example.com/
- https://example.com/docs/
- https://example.com/guide/

问题: 缺少深度页面,LLM 无法获取具体内容。

修正: 添加核心内容页面。

txt
# 正确示例

## Start Here
- https://example.com/
- https://example.com/docs/getting-started

## Core Topics
- https://example.com/docs/prompting
- https://example.com/docs/rag
- https://example.com/docs/evaluation

❌ 错误示例 2:没有更新时间

txt
# 错误示例

## Pages
- https://example.com/docs/ai

问题: 内容失效不可感知,LLM 无法判断信息新鲜度。

修正: 添加更新时间。

txt
# 正确示例

> Last updated: 2026-03-03

## Pages
- https://example.com/docs/ai

❌ 错误示例 3:营销页作为核心入口

txt
# 错误示例

## Start Here
- https://example.com/pricing
- https://example.com/features
- https://example.com/testimonials

问题: LLM 需要的是技术内容,不是营销页面。

修正: 聚焦技术文档。

txt
# 正确示例

## Start Here
- https://example.com/docs/getting-started
- https://example.com/docs/api-reference
- https://example.com/docs/integration-guide

❌ 错误示例 4:分类混乱

txt
# 错误示例

## Pages
- https://example.com/api/users
- https://example.com/docs/install
- https://example.com/blog/announcement
- https://example.com/api/products
- https://example.com/docs/quickstart

问题: 没有分类,难以导航。

修正: 按主题分组。

txt
# 正确示例

## Documentation
- https://example.com/docs/install
- https://example.com/docs/quickstart

## API Reference
- https://example.com/api/users
- https://example.com/api/products

验证脚本

bash
#!/bin/bash
# scripts/validate-llms-txt.sh

echo "Validating llms.txt..."

# 检查文件是否存在
if [ ! -f "public/llms.txt" ]; then
  echo "❌ llms.txt not found"
  exit 1
fi

# 检查是否有更新时间
if ! grep -q "Last updated:" public/llms.txt; then
  echo "⚠️  Missing last updated date"
fi

# 检查链接是否可达
echo "Checking links..."
broken=0
while read -r line; do
  if [[ $line == https://* ]]; then
    url=$(echo "$line" | sed 's/^- //')
    status=$(curl -s -o /dev/null -w "%{http_code}" "$url")
    if [ "$status" != "200" ]; then
      echo "❌ Broken link: $url ($status)"
      broken=$((broken + 1))
    fi
  fi
done < public/llms.txt

if [ $broken -eq 0 ]; then
  echo "✅ All links are valid"
else
  echo "❌ Found $broken broken links"
  exit 1
fi