This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
An educational resource for studying Claude Code's TypeScript source architecture. It teaches the system through a request-flow-first approach: trace one complete request through the system before branching into specialized topics.
The repo contains:
- 9 standalone Python examples (
examples/l1_startup.pythroughl9_context_mgmt.py) — pedagogical mappings, not line-by-line ports - Bilingual docs (
*.md= Chinese,*.en.md= English) covering 15 deep-dive layers, 4-stage learning paths, exercises, and FAQs claudecode_src/— a gitignored local mirror of the actual Claude Code TypeScript source (not published)
No build step. All examples are standalone Python scripts:
# Most examples run without any API key
python examples/l1_startup.py
# l2 and l8 require a live model call
# Set ANTHROPIC_API_KEY or DEEPSEEK_API_KEY in .env
python examples/l2_agent_loop.py
python examples/l8_streaming.pyDependencies: pip install openai python-dotenv
examples/ 9 teaching layers (l1–l9), each maps to specific source files
docs/
paths/ 4-stage learning curriculum (p1–p4), each has .md and .en.md
layers/ 15 deep-dive topic docs (l1–l15), each has .md and .en.md
superpowers/ Design specs and implementation plans
source-navigation.* How to search symbols in the real source
example-source-bridge.* Maps each example to its source file counterparts
PHILOSOPHY.MD 10 core design principles of Claude Code
agent.md Handoff document: verified source facts, themes, constraints
- Every
.mdcontent file has an.en.mdEnglish counterpart — keep both in sync when editing. - All explanations must be tied to verifiable lines/symbols in
claudecode_src/(cited by file path and line number). - Examples are pedagogical abstractions — they illustrate patterns, not exact implementations.
- The teaching progression is: P1 → P2 → P3 → P4 (learning paths), with examples l1–l9 as hands-on supplements.
The central teaching spine is the request lifecycle:
User Input → Startup/Mode Routing (main.tsx + entrypoints/)
→ queryLoop (QueryEngine.ts) → query() async generator (query.ts)
→ Model streaming → Tool schema selection → Tool execution
→ Tool result reentry → State/UI update → Next turn or exit
Key patterns explained across examples and docs:
async function* query()— the agent loop is an async generator, not callbacksyield*delegation — child agent events propagate transparently to parent/UI- Tool = Schema + Function — model sees JSON schema; runtime executes the function
- Three-layer permissions — semantic denial → rule matching → user confirmation
- Prompt cache stability — static/dynamic boundary, sticky latches protect cache hits
claudecode_src/src/bootstrap/state.ts— global state with explicit "DO NOT ADD MORE STATE HERE" discipline
- When adding a new layer doc or path doc, create both
*.mdand*.en.mdversions. - New examples follow the
l{N}_{topic}.pynaming pattern and must include a source file map in their header comments. - Update
docs/example-source-bridge.mdand.en.mdwhen adding new examples. - Cross-reference
agent.mdfor verified source facts (line numbers, symbol names) before citing them.