用 Claude Code 实现 Karpathy LLM Wiki:从方法论到可跑通的知识库系统Andrej Karpathy 在 2026 年 4 月发布了一篇 gist,提出了 LLM Wiki 的知识管理模式。原文是方法论,没有配套工具。本文把它落地成一套用 Claude Code + CLAUDE.md + Obsidian + Git 可以完整复现的系统。
所有步骤都是我在 macOS / Linux 下验证可跑通。Windows 用户需要把路径分隔符换成 \,其余一致。
核心思路:把工作从查询时移到导入时传统 RAG 的问题在于,每次查询都临时召回原始文档片段然后综合。这意味着:
知识不会沉淀,好的推理每次都重建多来源冲突在回答时才暴露,没有预处理积累的是文档仓库,不是可读的知识层Karpathy 的替代方案:
原始资料 → [导入时] AI 编译成 Wiki 页面 → [查询时] 读 Wiki → 好答案沉淀回 Wiki
编译发生在导入阶段。查询时读的是已经结构化的知识,不是 raw chunk。
环境准备安装 Claude Codenpm install -g @anthropic-ai/claude-code
需要 Node.js 18+。安装后运行:
claude --version
初始化知识库目录mkdir -p ~/wiki
cd ~/wiki
git init
# 创建目录结构
mkdir -p concepts workflows decisions references articles archive
mkdir -p .claude/commands
# 创建 Git 同步配置
echo "*.DS_Store" > .gitignore
echo ".obsidian/workspace*" >> .gitignore
目录说明:
目录
存放内容
concepts/
术语、技术、方法论的概念页
workflows/
可执行流程、操作步骤
decisions/
选型判断、架构决策记录
references/
外部资料的编译摘要
articles/
长文分析
archive/
过期内容
第一步:写 CLAUDE.md(Schema 层)CLAUDE.md 是整套系统的大脑。它告诉 Claude Code 知识库的规则,在每次会话时自动加载。
cd ~/wiki
claude /init
/init 会在当前目录创建 CLAUDE.md。然后用以下内容覆盖:
cat > CLAUDE.md << 'EOF'
# LLM Wiki — Knowledge Base Schema
## Directory Structure
wiki/ ├── CLAUDE.md # This file — schema and rules ├── README.md # Index of all notes ├── concepts/ # Term/method/tech concept pages ├── workflows/ # Executable process docs ├── decisions/ # ADR-style decision records ├── references/ # Compiled summaries of external sources ├── articles/ # Long-form analysis └── archive/ # Deprecated content
## Frontmatter Standard
Every note MUST have:
```yaml
---
title:
type: concept | workflow | decision | reference | article
tags: [tag1, tag2]
status: active | draft | deprecated
created: YYYY-MM-DD
updated: YYYY-MM-DD
source:
---
Ingest ProtocolWhen asked to ingest an external article or source:
Create file in references/ named .mdExtract and record in frontmatter: title, author, URL, published date, accessed dateWrite the following sections: Summary: 3–5 sentences capturing the core argument Key Claims: bullet list of specific assertions Entities: people, tools, organizations mentioned with brief context Concepts: technical terms or methods introduced or relied upon Relationships: how entities/concepts relate to each other Procedures: any step-by-step processes described Caveats: limitations, scope constraints, or disclaimers stated by the source Conflicts: contradictions with existing wiki content (cross-link to conflicting note) Related Notes: [[wikilinks]] to existing pages in this wikiIf new entities or concepts emerge, create stub pages in concepts/Update README.md indexQuery ProtocolWhen answering questions:
Check if a relevant note exists in the wiki before reasoning from scratchIf a note exists, cite it and use it as the basis for the answerFollow [[wikilinks]] to retrieve related contextIf the answer involves multiple notes, synthesize and note the path takenIf the answer contradicts a wiki note, flag the conflict explicitlyWrite PermissionsNever write to the wiki autonomously. All writes must be explicitly requested by the user. Do not create or modify notes without a direct instruction.
Lint RulesWhen running a lint check, report:
Notes missing required frontmatter fieldsBroken [[wikilinks]] (target file does not exist)Orphan notes (no other note links to them)Notes with source: set to an external URL but missing a Summary sectionConflict sections that are empty but the note was created from a source that contradicts existing contentNotes not listed in README.md indexFiles in root not matching the directory structure aboveConflict HandlingWhen a new source contradicts an existing note:
Do not silently overwrite the existing noteAdd a ## Conflicts section to the new reference noteAdd a conflict annotation to the existing note: > ⚠️ Conflict: see [[new-reference-note]]Leave resolution to the userEOF
---
## 第二步:创建 README 索引
```bash
cat > README.md << 'EOF'
# Wiki Index
## Concepts
## Workflows
## Decisions
## References
## Articles
EOF
第三步:创建自定义 Skill(三个核心操作)自定义命令存放在 .claude/commands/ 目录下,推荐迁移到 .claude/skills//SKILL.md 格式。这里用 commands 格式,两种都可工作。
3.1 ingest 命令cat > .claude/commands/ingest.md << 'EOF'
---
description: Ingest an external source into the wiki as a compiled reference note
argument-hint:
allowed-tools: Read, Write, Glob
---
Ingest the following source into the wiki following the Ingest Protocol in CLAUDE.md.
Source: $ARGUMENTS
Steps:
1. If $ARGUMENTS is a URL, read the content (I will paste it if it cannot be fetched)
2. Generate a slug from the title (lowercase, hyphens, no special chars)
3. Create `references/.md` with correct frontmatter and all required sections
4. For each new entity or concept not already in the wiki, create a stub in `concepts/.md`
5. Add the new note to README.md under the References section
6. Report: filename created, stubs created, any conflicts detected
Do NOT create the file until you have shown me the planned frontmatter and slug for confirmation.
EOF
3.2 query 命令cat > .claude/commands/query.md << 'EOF'
---
description: Answer a question using the wiki as primary source
argument-hint:
allowed-tools: Read, Glob
---
Answer the following question using the wiki as the primary source of truth.
Question: $ARGUMENTS
Steps:
1. Search for relevant notes using Glob on concepts/, references/, articles/, decisions/
2. Read the most relevant notes
3. Follow [[wikilinks]] to retrieve related context (read those files too)
4. Synthesize an answer citing the specific wiki files used
5. If no relevant wiki content exists, say so explicitly before answering from general knowledge
6. If the question touches a known conflict in the wiki, surface it
Format: Answer first, then list sources as: `Sources: [[note1]], [[note2]]`
EOF
3.3 lint 命令cat > .claude/commands/lint.md << 'EOF'
---
description: Run a health check on the wiki and report issues
allowed-tools: Read, Glob, Bash(find:*)
---
Run a full lint check on this wiki. Check every .md file in concepts/, workflows/, decisions/, references/, articles/.
Check each file for:
1. **Missing frontmatter fields**: title, type, tags, status, created, updated, source — report any missing
2. **Broken wikilinks**: for every [[link]] found, check if the target file exists (search all directories)
3. **Orphan notes**: files that no other note links to (no [[filename]] reference pointing to them)
4. **Missing Summary**: files with an external URL as source but no "## Summary" section
5. **Unflagged conflicts**: files created from external sources where a conflict was likely but the Conflicts section is empty
6. **README gaps**: files not listed in README.md
Output format:
Lint Report — ❌ Missing Frontmatterreferences/foo.md: missing source, updated❌ Broken Wikilinksconcepts/bar.md: [[nonexistent-page]] (line 14)⚠️ Orphan Notesconcepts/baz.md: no incoming links⚠️ Missing Summaryreferences/qux.md: external source but no Summary section✅ README CoverageAll notes indexed.
SummaryX errors, Y warnings. Recommend fixing errors before next ingest.
EOF
验证命令是否加载:
cd ~/wiki
claude
# 在 Claude Code 会话中输入 / 查看是否出现 ingest, query, lint
第四步:配置 ObsidianObsidian 作为浏览和人工编辑层,不参与写入流程。
打开 Obsidian,选择 ~/wiki 作为 Vault开启设置 → Files & Links → Use [[Wikilinks]]开启 Graph View 可以看到知识图谱重要:把 Obsidian 的自动创建新文件功能关掉。路径:Settings → Files & Links → Default location for new notes → 设为 archive/,防止 Obsidian 在根目录产生文件污染目录结构。
第五步:配置 Git 同步cd ~/wiki
git add -A
git commit -m "init: wiki structure and schema"
# 推送到私有仓库(GitHub / Gitea 均可)
git remote add origin
git push -u origin main
每次写入后提交:
git add -A && git commit -m "ingest: "
实际使用流程导入一篇文章在 ~/wiki 目录启动 Claude Code:
cd ~/wiki
claude
然后:
/ingest https://example.com/some-article
如果文章不能直接抓取,把内容粘贴进去:
/ingest paste
[粘贴内容后按回车]
Claude Code 会先给你确认 slug 和 frontmatter,确认后再写入文件。
示例输出:
Planned file: references/karpathy-llm-wiki.md
Slug: karpathy-llm-wiki
Type: reference
Source: https://gist.github.com/karpathy/...
New concept stubs to create:
- concepts/rag.md
- concepts/wiki-compilation.md
Conflicts detected: None
Proceed? (yes/no)
查询知识库/query RAG 和 LLM Wiki 的核心区别是什么
运行健康检查/lint
建议每月或每积累 20 篇新内容后运行一次。
写入红线写入必须由人触发。CLAUDE.md 里已经写明了这条规则。具体体现:
/ingest 在写入前强制确认/query 只读,不写/lint 只读,不写Claude Code 不会在会话中自动更新知识库如果你在测试中发现 Claude Code 试图自动写入,检查 CLAUDE.md 的 Write Permissions 部分是否完整加载:
# 在 Claude Code 会话中
/memory
# 选择 Project Memory 确认 CLAUDE.md 内容是否被读取
目录结构最终状态~/wiki/
├── CLAUDE.md
├── README.md
├── .gitignore
├── .claude/
│ └── commands/
│ ├── ingest.md
│ ├── query.md
│ └── lint.md
├── concepts/
│ └── (concept stubs created during ingest)
├── workflows/
├── decisions/
├── references/
│ └── (compiled reference notes)
├── articles/
└── archive/
常见问题Q:/ingest 命令没出现在 / 菜单里
检查 .claude/commands/ 路径是否在 ~/wiki 目录下,而不是其他项目目录。Claude Code 的 project commands 只在对应目录生效。
Q:Claude Code 抓不到 URL 内容
正常情况,不是所有 URL 都可抓取。用 paste 参数手动粘贴内容即可,/ingest 命令设计上支持两种模式。
Q:Obsidian Graph View 里看不到链接
检查笔记里的链接格式是否是 [[filename]](不带扩展名),且 Obsidian Vault 根目录指向的是 ~/wiki 而不是子目录。
Q:CLAUDE.md 修改后没生效
在 Claude Code 会话中运行 /memory 重新加载,或者退出重新进入会话。
扩展方向如果以后想增强系统能力,可以按以下顺序评估,每步都是独立可选的:
跨设备同步:把 Git remote 换成 Gitea self-hosted,或者直接用 GitHub private repo + Obsidian Git 插件实现自动同步向量检索:在本机跑 sqlite-vss 或 LanceDB,对 references/ 建向量索引,/query 时先向量召回再读全文——但 2GB RAM 以下不建议自动化 lint:把 /lint 做成 Git pre-push hook,每次推送前强制跑一遍多 Agent 并发导入:Claude Code 的 /batch 命令可以并发处理多篇文章,但写入冲突需要人工处理,谨慎使用参考Karpathy LLM Wiki 原文:https://gist.github.com/karpathy/442a6bf555914893e9891c11519de94fClaude Code 自定义命令文档:https://code.claude.com/docs/en/agent-sdk/slash-commandsClaude Code Memory(CLAUDE.md)文档:https://code.claude.com/docs/en/memory#观点创作激励赛##头条精选#@豆包#真心话茶局#
Claude Code 实现Karpathy LLM Wiki:从方法...
用 Claude Code 实现 Karpathy LLM Wiki:从方法论到可跑通的知识库系统Andrej Karp
阅读:0
点赞:0