Skip to content

Raflimoon/learn-real-claude-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

English | 中文

Learn Real Claude Code — Source Archaeology on a 512K-Line Agent

The Source Code Doesn't Lie

On March 31, 2026, a security researcher discovered that Anthropic had accidentally shipped the complete TypeScript source of Claude Code inside an npm package — 512,000+ lines, 1,902 files, a 60MB sourcemap that unzips to the full codebase.

This was not the first time. The same mistake happened in February 2025.

Before we talk about what the source reveals, let's be precise about what you are actually looking at.

Claude Code is not an agent. Claude Code is a harness.

The agent is Claude — a neural network, trained by Anthropic, capable of reasoning, planning, and acting. Claude Code is the shell around that model: the tools, the context management, the permission system, the terminal UI, the streaming infrastructure. It is the vehicle the agent drives. Not the driver.

This distinction matters because it changes what you learn from reading the source. You are not studying "how Claude thinks." You are studying how Anthropic built the environment that lets Claude think at its best — in production, at scale, with real users.

And what you find, when you read those 512,000 lines carefully, is deeply instructive.

What the Source Reveals

The common mental model of an AI coding agent looks like this:

user message → LLM call → parse response → if tool_call: run tool → loop

Clean. Simple. Teachable in a weekend.

The reality in Claude Code's source is different:

user message
    → fast-path check (exit in < 5ms if --version, --help, etc.)
    → lazy module evaluation (main.tsx is 804KB; don't load what you don't need)
    → parallel prefetch (config + auth + MCP init all fire concurrently)
    → command registry check (100+ slash commands before touching the model)
    → system prompt assembly (10+ sources, SYSTEM_PROMPT_DYNAMIC_BOUNDARY for cache)
    → StreamingToolExecutor fires tools MID-STREAM (not after full response)
    → concurrent tool execution (isConcurrencySafe() → reads parallel, writes exclusive)
    → result budget enforcement (truncation to prevent context overflow)
    → token budget check (> 95% full → 7-strategy compression pipeline)
    → permission racing (hooks + classifier + user confirm run concurrently; first safe wins)
    → telemetry (StatsD metrics + Sentry errors + per-model cost tracking)
    → loop

The loop is not the hard part. The edges are the hard part.

This is what 512,000 lines teaches you: production agents are mostly not LLM calls. They are careful resource management — context windows, token budgets, permission systems, error recovery, streaming infrastructure. The model is 20% of the code. The harness is 80%.

What You Cannot Learn From Textbooks

There are four things about production agent engineering that textbook examples systematically hide:

1. Mid-stream tool execution. Every tutorial shows: receive full response → parse tool calls → execute tools → send results. Claude Code's StreamingToolExecutor fires tools at the delimiter boundary — while the response is still streaming. This is not a micro-optimization. It is an architectural decision that changes how the agent feels to use.

2. Context as a finite resource. Context windows are not "just bigger now." Production agents still hit limits. Claude Code has seven independent compression strategies that fire in priority order: snip duplicate system messages, microcompact recent tool results, collapse long file reads, summarize the full conversation. Miss any one of these and you get a silent failure at 200K tokens.

3. Permission racing. The naive pattern is: check permission → if ok, run tool → show result. Claude Code runs three things concurrently: shell hooks, an AI classifier, and the user confirmation dialog. The first to return a safe signal wins. This means most tools run without noticeable latency, and the classifier learns from your approval patterns.

4. Multi-agent as file I/O. Not a framework. Not a graph. coordinatorMode.ts spawns subprocesses that inherit the parent's prompt cache, write results to temp files, and the coordinator reads those files. Fork subagents share context at near-zero cost. The "architecture" is process spawning plus a glob.

These patterns don't appear in any agent tutorial. They appear in Claude Code's source — quietly, without documentation, doing exactly what they need to do.


Hidden in the Source

The leak also exposed features Anthropic has never announced.

BUDDY — A virtual companion system. Each user gets a deterministic companion generated by a Mulberry32 PRNG seeded with their userId. The companion has a name, a species, moods, and a 0.01% chance of being a shiny legendary. It was designed to appear as a speech bubble next to the input field. It has never shipped.

KAIROS — "Always-On Claude." A persistent background agent that runs continuously, monitors your codebase for changes, maintains a running context log, and prepares for your next session while you sleep. Not a one-shot CLI tool — a daemon. The name is a reference to the ancient Greek concept of the opportune moment.

Undercover Mode — A system that allows Claude Code to present itself as a different AI assistant. The implementation suggests enterprise white-labeling. The name suggests something else.

Speculative Execution — Pre-runs likely next actions while the user is reading the previous response. If the user confirms, the result is already ready. If they don't, the result is discarded. Claude Code was thinking ahead without you knowing.

These features are complete enough to find in the source, incomplete enough to never ship. They are a map of where Claude Code was going.


What This Repository Does

This is not a tutorial on building agents. learn-claude-code teaches you that.

This is source archaeology — a systematic dissection of 512,000 lines of production TypeScript to extract the engineering decisions, architectural patterns, and hidden mechanisms that make Claude Code work.

                    THE ACTUAL AGENT LOOP (query.ts)
                    ==================================

    User input
         |
         v
    [ Fast Path? ]---yes---> exit(0) before any imports load
         |
         no
         |
         v
    [ Slash Command? ]---yes---> commandRegistry.dispatch()
         |
         no
         |
         v
    Build messages[] ←───────────────────────────────┐
         |                                            |
         v                                            |
    Assemble system prompt                            |
    (10+ sources, cache boundary)                     |
         |                                            |
         v                                            |
    Stream API call ──────────────────────────────────┼──> telemetry
         |                                            |
         | (mid-stream)                               |
         v                                            |
    StreamingToolExecutor fires tools                 |
    (concurrent if isConcurrencySafe())               |
         |                                            |
         v                                            |
    Inject tool results                               |
         |                                            |
         v                                            |
    [ tokenBudget > 95%? ]---yes---> compressContext()
         |                                            |
         v                                            |
    [ More tool calls? ]---yes-----------------------┘
         |
         no
         |
         v
    Render response (Ink/React → terminal)


    Note what's missing from textbooks:
    - Mid-stream tool execution
    - Concurrent tool dispatch
    - Automatic context compression
    - Permission racing (runs in parallel with tool execution)
    - Cost tracking on every API call

15 chapters + 3 Plus chapters. Each chapter reverse-engineers one architectural mechanism.

c01   "Fast paths exit before importing anything; expensive I/O runs in parallel with module evaluation" — How a 804KB TypeScript monolith starts in 500ms

c02   "StreamingToolExecutor fires tools mid-stream without waiting for the full response" — The real agent loop: streaming, mid-flight tools, auto-compact

c03   "Tools declare isConcurrencySafe() — reads run in parallel, writes run exclusively" — Orchestrating 40+ tools with concurrent execution and result budgets

c04   "Commands are user-initiated UI actions; tools are model-initiated API actions" — 100+ slash commands: registration, dispatch, the command/tool distinction

c05   "SYSTEM_PROMPT_DYNAMIC_BOUNDARY splits static (globally cached) from dynamic (per-session)" — 110+ strings assembled into one system prompt, with cache economics

c06   "Seven independent strategies fire in priority order: snip → microcompact → collapse → summarize" — Context as a finite resource, with seven compression strategies

c07   "Auto-Dream runs four phases (orient→gather→consolidate→prune) with a read-only bash subagent" — Cross-session memory via CLAUDE.md persistence and Auto-Dream consolidation

c08   "Hooks/classifier race concurrently with user confirmation — first to return wins" — Five-layer permission stack and permission racing

c09   "Hooks can modify tool input, block execution, or inject system messages" — Hook lifecycle: PreToolUse, PostToolUse, three backends

c10   "Fork subagents share the parent's prompt cache for near-zero-cost context inheritance" — One Claude commanding many: Coordinator Mode, file IPC, cache sharing

c11   "18 deferred tools load on-demand via ToolSearchTool to keep the base prompt under 200K tokens" — MCP: infinite extension via deferred tool loading

c12   "Three interning pools (Char/Style/Hyperlink) turn O(n) string comparisons into O(1) integer checks" — React in the terminal: custom reconciler, Yoga layout, double-buffered screen

c13   "Bun feature() eliminates code at compile time; GrowthBook/Statsig gates features at runtime" — Dual feature gating: compile-time DCE + runtime flags, 108 missing modules

c14   "Circuit breaker (max 3 consecutive failures) prevents infinite retry loops" — Observability: dual telemetry, cost tracking, 11-step error recovery

c15   "BUDDY uses Mulberry32 PRNG seeded by userId — 0.01% chance of shiny legendary" — Secrets in the source: BUDDY, KAIROS, Undercover, speculative execution

p01   [PLUS] "Why is 80% of what matters dynamic context, not static prompts?" — Context Engineering: the 2026 paradigm shift

p02   [PLUS] "The difference between a demo agent and a production agent is almost entirely in the scaffolding" — Agent scaffolding: why the harness matters more than the prompts

p03   [PLUS] "Industrial-grade agents are 20% LLM calls and 80% careful resource management" — What Claude Code reveals about 2026 agent architecture


The Source

The Claude Code source used in this analysis was extracted from npm package @anthropic-ai/claude-code version 2.1.88, via the cli.js.map sourcemap file included by mistake.

The leak is confirmed by multiple security researchers and widely documented. This is the second time the same mistake occurred (the first was February 2025).

Source mirror: github.com/674019130/claude-code Original mirror: github.com/Kuberwastaken/claude-code

Discussion:


Quick Start

git clone https://github.com/674019130/learn-real-claude-code
cd learn-real-claude-code/web
npm install
npm run dev     # http://localhost:3000

Web Platform Features

Page Description
/[locale]/c01/c15 Chapter tutorials with interactive visualizations
/[locale]/architecture SVG architecture map — hover to trace data flow
/[locale]/flow Step-through of the full request lifecycle (9 steps)
/[locale]/compare Naive vs Production: 5 side-by-side pattern comparisons
/[locale]/explorer Browse all 1,902 source files with search and filter

Supports English and Chinese (/en/..., /zh/...).


Architecture

learn-real-claude-code/
├── docs/
│   ├── en/          # English tutorial markdown (c01-c15, p01-p03)
│   └── zh/          # Chinese tutorial markdown (c01-c15, p01-p03)
├── source -> ~/Projects/claude-code-leaked/   # symlink to source
└── web/
    ├── scripts/
    │   └── extract-source.ts    # indexes source → docs.json + source-index.json
    └── src/
        ├── app/[locale]/
        │   ├── page.tsx         # homepage
        │   ├── (learn)/
        │   │   ├── [chapter]/   # chapter pages (Learn / Visualize / Code / Deep Dive)
        │   │   ├── architecture/
        │   │   ├── flow/
        │   │   ├── compare/
        │   │   └── explorer/
        ├── components/
        │   ├── layout/          # header, sidebar
        │   ├── visualizations/  # c01-c15 interactive SVG components
        │   └── docs/            # markdown renderer
        └── data/generated/      # docs.json, source-index.json (built by extract-source.ts)

What Is Not Here

This repository analyzes Claude Code's architecture from source. It does not:

  • Reproduce Claude Code's functionality (building a competing coding agent is not the goal)
  • Include the full 512K-line source (link to the mirror instead)
  • Make any claims about Anthropic's intentions, roadmap, or internal decisions
  • Guarantee accuracy — source code analysis involves interpretation

The Plus chapters (p01-p03) include opinions about 2026 agent engineering trends. Treat them as analysis, not fact.


License

MIT


The source code doesn't lie. 512,000 lines say: production agents are mostly plumbing.

Read the plumbing. Build better agents.

Releases

No releases published

Packages

 
 
 

Contributors