Claude Code Source Deep-Dive Tutorial
A structured learning path from zero to advanced, based on a source-map reconstruction of 1,987 TypeScript files from the
@anthropic-ai/claude-codenpm package.
📚 Tutorial Table of Contents
This tutorial has six main chapters, progressing along three dimensions:
Functionality dimension (what it can do)
↓
Architecture dimension (why it is designed this way)
↓
Source-code dimension (how it is implemented)
📖 Chapter 1: Global Overview — What is Claude Code?
Best for: beginners who want a complete mental model first
- 1.1 Beneath the Surface: It's More Than a CLI
- 1.2 Tech Stack and Runtime Environment
- 1.3 Source Map: How to Navigate 1,987 Files
- 1.4 Vim Mode: A Full Editor Inside the Terminal
🏗️ Chapter 2: Architecture Design — System Structure and Layering
Best for: readers who understand usage and want system-level design insight
- 2.1 Six-Layer Architecture: From Terminal to AI
- 2.2 The Three-Layer Feature Gate System
- 2.3 Global State Management:
bootstrap/state.tsDeep Dive - 2.4 UI Layer: How React + Ink Render in Terminal
⚙️ Chapter 3: Core Engine — The Agent Conversation Loop
Best for: readers who want to understand how AI agents "think and act"
- 3.1 Full Lifecycle of a Single Conversation
- 3.2
query.ts: Main Loop Source Deep Read - 3.3 QueryEngine: Session State Manager
- 3.4 How System Prompt Construction Works
- 3.5 Context Compaction: How History Avoids Exploding
- 3.6 Hooks System: User Scripts in the AI Decision Flow
🔧 Chapter 4: Tool System — Design Philosophy Behind 53 Tools
Best for: readers who want to understand how AI interacts with the real world
- 4.1 Tool Abstraction: What Is a Tool?
- 4.2 Core Tool Deep Dive: Bash / FileEdit / Agent
- 4.3 Permission System:
canUseToolDecision Chain - 4.4 MCP Protocol: The Extensibility Boundary of Tools
- 4.5 Tool Concurrency and Orchestration
🚀 Chapter 5: Advanced Topics — Hidden Features Deep Dive
Best for: readers curious about what Anthropic is building internally
- 5.1 KAIROS: The Never-Off Persistent AI Assistant
- 5.2 Coordinator: Multi-Agent Orchestration Mode
- 5.3 Bridge: Remote Control of Local CLI from claude.ai
- 5.4 BUDDY: Deterministic Generation for an AI Desktop Pet
- 5.5 Voice & Proactive: Voice and Initiative Modes
- 5.6 Hidden Command Reference: Slash Commands / CLI Flags / Env Vars
🔒 Chapter 6: Engineering Practices — Release and Quality Control
Best for: readers focused on engineering management and production rollout
- 6.1 Three-Layer Gates:
feature()/USER_TYPE/ GrowthBook - 6.2 Full Compile Flag Reference (50 Flags)
- 6.3 Telemetry & Observability: OpenTelemetry in Practice
- 6.4 Session Persistence and History
- 6.5 Services Layer Panorama: Responsibility Map of 26 Subservices
- 6.6 Complete Environment Variable Reference (70+)
🎓 Chapter 7: Summary
🧠 ClaudeCode Walkthrough (Added)
🗺️ Quick Navigation
| I want to understand... | Jump to |
|---|---|
| What Claude Code is and what it can do | Chapter 1 |
| How to read the overall code structure | 1.3 Source Map |
| Vim mode keybindings | 1.4 Vim Mode |
| How the AI agent main loop works | 3.2 query.ts Deep Read |
| How to inject scripts into Claude's decisions | 3.6 Hooks System |
| How tool permissions are decided | 4.3 Permission System |
| How KAIROS persistent assistant is implemented | 5.1 KAIROS |
| How multi-agent orchestration works | 5.2 Coordinator |
| Hidden commands/flags/env vars quick lookup | 5.6 Hidden Command Reference |
| How feature switches are controlled | 6.1 Three-Layer Gates |
| What services exist in services layer | 6.5 Services Overview |
| What you can do after finishing this tutorial | Tutorial Summary |
📌 Notes
- This tutorial is based on reconstructed TypeScript source and is for research/learning only
- Source code copyright belongs to Anthropic
- All quoted code references include file paths for side-by-side reading
💡 Suggested reading order: beginners should read chapter by chapter; experienced developers can jump directly to Chapter 3 (
query.ts) or Chapter 5 (advanced topics); for quick lookup, use the Quick Navigation table.