Claude Code Source Analysis โ€” What Makes It Work & How to Actually Learn From It

Claude Code Source Analysis โ€” What Makes It Work & How to Actually Learn From It

When Claude Codeโ€™s source was accidentally leaked on March 31, 2026, the community immediately started reverse-engineering. Two standout analyses emerged: Sebastian Raschka dissected what makes Claude Code work (spoiler: itโ€™s the harness, not the model), and ๅฎ็މ xp explained how to actually learn from reading source code โ€” a 4-step method applicable to any large open-source project.

*Source: Sebastian Raschka โ€” Claude Codeโ€™s Real Secret Sauce ๅฎ็މ xp on Weibo (2026-04-01) Latent.Space โ€” The Claude Code Source Leak*

Part 1: The 6 Architectural Secrets (Raschkaโ€™s Analysis)

Raschkaโ€™s thesis: Claude Codeโ€™s core advantage is the software harness, not the model. Swap in DeepSeek, MiniMax, or Kimi with the same harness โ†’ potentially strong coding performance.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   Claude Code Architecture                   โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚  โ”‚ 1. Live  โ”‚  โ”‚ 2. Promptโ”‚  โ”‚ 3. Real  โ”‚  โ”‚ 4. Contextโ”‚   โ”‚
โ”‚  โ”‚ Repo     โ”‚  โ”‚ Cache    โ”‚  โ”‚ Tools    โ”‚  โ”‚ Bloat     โ”‚   โ”‚
โ”‚  โ”‚ Context  โ”‚  โ”‚ (Static/ โ”‚  โ”‚ (not     โ”‚  โ”‚ Control   โ”‚   โ”‚
โ”‚  โ”‚          โ”‚  โ”‚ Dynamic) โ”‚  โ”‚ bash)    โ”‚  โ”‚           โ”‚   โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚ 5. Structured        โ”‚  โ”‚ 6. Forks & Subagents     โ”‚     โ”‚
โ”‚  โ”‚    Session Memory    โ”‚  โ”‚    (shared cache,         โ”‚     โ”‚
โ”‚  โ”‚    (title, tasks,    โ”‚  โ”‚     parallel work)        โ”‚     โ”‚
โ”‚  โ”‚     errors, logs)    โ”‚  โ”‚                           โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                                              โ”‚
โ”‚            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                      โ”‚
โ”‚            โ”‚      LLM (Claude)       โ”‚ โ† swappable          โ”‚
โ”‚            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

1. Live Repo Context

When you start prompting, Claude Code loads real-time repository state โ€” main branch, current branch, recent commits, and CLAUDE.md โ€” before the model sees your question. This is why it โ€œknowsโ€ your codebase without you explaining it.

2. Aggressive Prompt Cache

A boundary marker separates static content (system prompt, tool definitions, CLAUDE.md) from dynamic content (conversation). Static parts are cached globally across turns, avoiding the cost of rebuilding the full prompt every iteration.

3. Real Tools, Not โ€œChat with Filesโ€

Claude Code doesnโ€™t shell out to grep or cat. It uses dedicated tools:

Tool Advantage over Bash
Grep Better permission handling, structured results
Glob Intelligent file discovery with patterns
LSP Semantic code understanding โ€” call hierarchy, references

This is code more like a structured IDE, less like a chatbot pasting terminal output.

4. Minimizing Context Bloat

The biggest challenge for coding agents: context windows fill up fast. Claude Code fights this with:

  • File-read deduplication โ€” skip re-reading unchanged files
  • Compressed disk previews โ€” summaries instead of full content
  • Auto-truncate + smart summarization โ€” tool outputs capped with intelligent truncation
  • Automatic context compression โ€” triggered when approaching window limits

5. Structured Session Memory

Each conversation maintains a structured Markdown document:

Session Title
Current Status
Task Description
Files & Functions
Workflow
Errors & Fixes
Codebase & System Docs
Learnings
Key Results
Work Log

This mirrors how humans take notes while coding โ€” itโ€™s not a flat chat log but an organized working document.

6. Forks & Subagents

Forked agents reuse the parentโ€™s prompt cache โ€” a byte-identical copy of the parent context. This means:

  • Parallel subagents cost almost nothing extra (shared cache)
  • Background analysis (summarization, memory extraction) doesnโ€™t pollute the main agent loop
  • State awareness lets subagents see mutable state while operating independently

Part 2: How to Actually Learn From Source Code (ๅฎ็މโ€™s Method)

ๅฎ็މ argues that the leakโ€™s value for learning is limited if you just skim the code. Most people get stuck at โ€œreadingโ€ and never reach โ€œunderstanding.โ€ His 4-step method applies to any large open-source project:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Step 1: RUN IT           "Code is dead,         โ”‚
โ”‚  Don't read. Execute.      running is alive."    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Step 2: FOLLOW A THREAD  Pick one feature,      โ”‚
โ”‚  Trace end-to-end.        not the whole system.  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Step 3: MODIFY IT        Secondary development. โ”‚
โ”‚  Write code, leave marks. Minimize AI help.      โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Step 4: REBUILD          From imitation to       โ”‚
โ”‚  Ask "why this design?"   transcendence.         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Step 1: Run It First

โ€œCode is dead. Running code is alive.โ€

Donโ€™t open files and start reading. Clone a runnable fork, get it executing, and observe behavior. Add console.log, set breakpoints, trace what happens live. Two reasons:

  1. You see results โ€” โ€œthis function probably does Xโ€ becomes certainty after one run
  2. You can add logging and breakpoints โ€” then analyze specific tool functions in their live execution context

Runnable forks were available at repos like claude-code-best/claude-code.

Step 2: Follow a Thread, Not the Whole Codebase

โ€œDonโ€™t try to read 50,000 lines top to bottom. Youโ€™ll quit in three days.โ€

Pick one specific feature and trace it end-to-end:

  • Agent Loop โ†’ Print all API requests. See the prompt, model response, tool calls, and results in sequence. One conversation gives you a direct understanding of โ€œhow an agent decomposes tasks and calls tools.โ€
  • Memory system โ†’ How does it store and retrieve? What triggers a memory save?
  • Tool dispatch โ†’ Input comes in, what routing happens, which tool fires?

Previously, claude-trace could do this. Now with the source, you can add logging yourself for much finer detail.

Step 3: Modify the Code

โ€œReading โ†’ โ€˜I think I understand.โ€™ Writing โ†’ actually understanding.โ€

Donโ€™t just read. Do secondary development on a mature codebase:

  • Implement a new slash command (like /buddy)
  • Add a feature to an existing system (extend memory, create a new tool)
  • Research how a subsystem works, then reimplement it yourself

Critical: Minimize AI assistance here. The point is to struggle with โ€œwhy is this module here? Why this interface?โ€ โ€” that friction is where understanding forms. When you build a feature end-to-end, your understanding shifts from โ€œseen itโ€ to โ€œdone it.โ€

Step 4: From Imitation to Transcendence

Once you know the architecture, ask the hardest question: โ€œWhy was it designed this way?โ€

Architecture decisions have invisible context:

  • Historical baggage and prior iterations
  • Team size and skill constraints
  • Deadlines and time pressure
  • Technical limitations at the time of writing

You see โ€œchose Aโ€ but canโ€™t see โ€œwhy not B or C.โ€ The best way to understand: rebuild from scratch, reference the original, and make your own design decisions. When your hand reaches a point where the original made a choice that now feels โ€œobvious,โ€ youโ€™ve truly understood.

On the Leak Itself

ๅฎ็މโ€™s take on why Anthropic wonโ€™t open-source Claude Code:

Reason Detail
Hide implementation hacks Internal shortcuts that would invite criticism if public
Anti-distillation Logic to prevent model extraction/cloning
User tracking Telemetry and user identification markers
Release control Open source means you canโ€™t hide unfinished features (buddy, buddy mode, Kairos, etc.)

Anthropicโ€™s response was notable โ€” Boris Cherny (VP Eng) credited team culture: โ€œMistakes happen. The real question is process, team design, and infrastructure.โ€ No individual blame. The issue was a manual deployment step that should have been automated.

Part 3: 5 Agent Design Patterns โ€” โ€œTreat AI as an Untrustworthy Contractorโ€ (็ˆฑๅฏๅฏโ€™s Analysis)

็ˆฑๅฏๅฏ-็ˆฑ็”Ÿๆดป distilled the leak into a production-grade agent design philosophy: donโ€™t treat AI as a trustworthy contractor โ€” build audit, rollback, and verification mechanisms as if it will fail.

*Source: ็ˆฑๅฏๅฏ-็ˆฑ็”Ÿๆดป on Weibo (2026-04) Reddit discussion*

Pattern 1: Skeptical Memory (ๆ€€็–‘ๅผ่ฎฐๅฟ†)

Most agent developers default-trust the modelโ€™s prior outputs. This causes error compounding โ€” each bad output becomes โ€œfactโ€ for the next turn, drifting the agent further off course.

Claude Codeโ€™s fix: treat stored memory as โ€œhints, not facts.โ€ Before acting on recalled information, verify against the real world first. This is like adding a validation unit in the pipeline โ€” prevents corrupted instructions from polluting the entire state machine.

The logic: treat memory as โ€œsuggestionsโ€ not โ€œtruth.โ€ Before acting, verify against reality.

Pattern 2: autoDream โ€” Background Memory Consolidation

When idle, the agent runs background consolidation โ€” deduplicating, compressing, and cleaning messy observations from the session. Like human sleep: active defragmentation prevents context bloat and information noise from accumulating.

This prevents the #1 killer of long-running agents: context window filled with garbage that degrades response quality over time.

Pattern 3: KAIROS โ€” Guardian Daemon Process

KAIROS gives the agent background session capability โ€” it can work autonomously while a daemon constrains its behavior:

  • Risk-tiered actions: routine operations proceed automatically, high-risk operations require human approval
  • Subscribes to external events (GitHub Webhooks)
  • Includes the โ€œdreamโ€ consolidation mechanism for long-term memory

Pattern 4: Constraint-Driven Architecture

The architecture isnโ€™t clever design โ€” itโ€™s the inevitable result of constraints. If you want an agent that works autonomously without losing control, thereโ€™s only one way to build it:

  • Risk classification for all actions
  • Human audit gates for high-risk operations
  • KAIROS-style guardian processes
  • Verification before completion

A Reddit commenter noted: โ€œEveryone independently converged on this same architecture.โ€ When developers build agents independently, they all arrive at these same patterns โ€” meaning the path to production-grade agents has essentially one road.

Pattern 5: The Core Philosophy

Donโ€™t treat AI as a trustworthy contractor. Treat it as an untrustworthy contractor and build a complete system of audit, rollback, and verification around it.

This reframes the entire agent design space. Instead of pursuing โ€œbetter AI that doesnโ€™t make mistakes,โ€ build systems that assume mistakes and handle them gracefully.

Why This Matters

The Harness Thesis

Raschkaโ€™s analysis confirms what many suspected: the model is necessary but not sufficient. Claude Codeโ€™s edge comes from the 99% of code that isnโ€™t the model call โ€” context management, tool design, caching, memory, and agent orchestration. This is why Cursor, Windsurf, and other competitors with the same underlying models deliver different results. The harness is the product.

Learning Method for AI-Era Developers

ๅฎ็މโ€™s 4-step method directly addresses the most common failure mode in the AI era: โ€œIโ€™ve seen a lot, but havenโ€™t built anything.โ€ Many developers use AI to quickly skim code and architecture, getting a false sense of understanding. The antidote is deliberate friction โ€” run, trace, modify, rebuild.

โ€œAI code analysis can give you an โ€˜architecture panoramaโ€™ in seconds. But that understanding is borrowed. It canโ€™t survive follow-up questions.โ€

Deep-Dive Reference Materials

โ€œDeep Dive Claude Codeโ€ โ€” The 25-Chapter Book

sawzhang/deep-dive-claude-code is the most thorough technical dissection of Claude Code available โ€” a full book modeled after classic tech references like โ€œElasticsearch Source Code Analysisโ€ and โ€œUnderstanding the Linux Kernel.โ€

Metric Detail
Chapters 25 + 2 appendices
Words ~120,000
Mermaid diagrams 101 (architecture, sequence, state machines, flows)
TypeScript code blocks 467 with syntax highlighting
License CC BY-NC-SA 4.0

Covers 10 sections:

Foundation (Ch 1-3)    โ†’ Overview, initialization, type systems
Core Engine (Ch 4-6)   โ†’ Query processing, messaging, streaming
Tool System (Ch 7-9)   โ†’ Tool interfaces, built-ins, execution pipelines
Agent System (Ch 10-12)โ†’ Agent models, fork/resume, skills
Security (Ch 13-14)    โ†’ Permission architecture, shell safety
MCP Protocol (Ch 15-16)โ†’ Transport layers, authentication
State (Ch 17-18)       โ†’ Store management, session compression
Terminal UI (Ch 19-20) โ†’ React+Ink rendering, REPL
Engineering (Ch 21-23) โ†’ Optimization, testing, build systems
Philosophy (Ch 24-25)  โ†’ Design patterns, engineering principles

Available as online reading, mdBook build, or PDF export.

Claude Code as โ€œAgent Operating Systemโ€ (้ป˜ๅบตโ€™s Analysis)

้ป˜ๅบต frames Claude Code not as a coding tool but as a full Agent Operating System โ€” with a platform entry layer, composable prompt system, managed tool pipeline, specialized agent roles, and four extension mechanisms (Skills, Plugins, Hooks, MCP).

The tvytlx/claude-code-deep-dive repo contains the analysis report (PDF) plus a minimal teaching agent implementation in Python that strips the architecture down to its essentials โ€” useful for understanding the core patterns without 500K lines of TypeScript.

5 actionable takeaways for agent builders:

  1. Decompose the system prompt into modular, composable pieces โ€” donโ€™t put everything in one giant prompt
  2. Wrap tool calls with validation and permission management โ€” not raw function calls
  3. Package high-frequency tasks into standardized Skills
  4. Spell out in the prompt what NOT to do โ€” explicit constraints prevent drift
  5. Add a standalone verification step after every critical task completion

โ€œThese methods donโ€™t need to be perfect from day one. Add one layer at a time โ€” each layer makes the agent more stable.โ€

How LearnAI Team Could Use This

  • Use the source analysis as a teaching resource for agent architecture literacy โ€” how the model API call is ~1% and the harness is ~99%.
  • Run source-reading workshops where team members trace a specific feature through the codebase.
  • Evaluate agent architectures by comparing Claude Codeโ€™s patterns against student-built or open-source alternatives.

Real-World Use Cases

  • Understanding how Claude Codeโ€™s tool loop works before building custom skills or hooks.
  • Using the 5 agent design patterns as a checklist when reviewing agent system designs.
  • Learning from the โ€œuntrustworthy contractorโ€ metaphor when designing permission and safety boundaries.