Claude Code — Source Tree Analysis
An exploration of the Claude Code source at /workspaces/src/ — Anthropic's official CLI coding agent — with an emphasis on what is interesting to learn from: how the agent loop is structured, how the LLM is invoked, what guardrails are in the prompt, and how the many subsystems coordinate.
Documents
Overview series
- 01-purpose-and-stack.md — what this project is, who it is for, languages, runtime, notable dependencies.
- 02-architecture-overview.md — the 30,000-foot view: components, dependency layout, data flow, diagrams.
- 03-entry-and-execution.md — startup path, fast-path flags, boot-to-TUI, SDK headless, bridge / remote / daemon entry points.
- 04-agent-loop-and-llm.md — the core generator-based query loop, streaming, retry, thinking, compaction. Focus doc for LLM usage.
- 05-tool-system.md —
Tool.tscontract, permissions, deferred tools + ToolSearch, a tour of every tool. - 06-agents-skills-commands.md — subagents (built-in + custom), skills, slash commands, fork subagents, MCP.
- 07-prompts-guardrails.md — verbatim excerpts of the system prompt, agent prompts, compaction prompts, and memory prompts, with commentary on the guardrails and techniques used.
- 08-memory-and-context.md — CLAUDE.md, auto-memory, session memory, file history, attribution.
- 09-state-ui-session.md — React/Ink TUI, AppState store, transcript persistence, resume.
- 10-design-patterns-and-tradeoffs.md — patterns used, tradeoffs, clever tricks, pitfalls to watch for.
- 11-key-files-map.md — a mapping from essential file → responsibility, in priority-read order.
Deep-dive series
- 12-btw-command.md — how
/btwworks: fork agent, cache-safe params, no-tools policy, verbatim wrapper prompt. - 13-multi-agent-coordination.md — parallel tool dispatch,
StreamingToolExecutor, fork vs fresh subagents, background agents, teams,SendMessageTool, coordinator mode. - 14-interruption-and-queue.md — the command queue, query guard, per-tool
interruptBehavior, Escape, mailbox, how mid-turn messages work end-to-end. - 15-memory-deep-dive.md — extraction prompt verbatim, trigger thresholds, fork agent shape, recall scoring via Sonnet, team memory scope and security, cache implications.
- 16-agentic-loop-schematic.md — a line-by-line walkthrough of
query.ts'squeryLoop(): every phase, everytransition.reason, invariants, complexity hotspots.
How to read this analysis
If you only have an hour: read 02, 04, 07, 10 in that order. If you want to implement a similar system: also read 05 and 06. If you are porting / extending: read 11 first to find the files you will edit.
If you want the deep mechanics: 16 is the detailed loop schematic; 13 and 14 cover the multi-agent and concurrency parts that overlap with 4; 15 is the full memory story; 12 is the focused /btw case study.
Every major claim cites file:line. Prompt strings are quoted as they appear in source; code snippets are verbatim. Where the source uses feature-flag conditionals, I note the gate rather than inventing a dominant branch.