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:
- You see results โ โthis function probably does Xโ becomes certainty after one run
- 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:
- Decompose the system prompt into modular, composable pieces โ donโt put everything in one giant prompt
- Wrap tool calls with validation and permission management โ not raw function calls
- Package high-frequency tasks into standardized Skills
- Spell out in the prompt what NOT to do โ explicit constraints prevent drift
- 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.
Links
- Raschkaโs full analysis: Claude Codeโs Real Secret Sauce
- Latent.Space coverage: The Claude Code Source Leak
- Deep Dive book (25 chapters): sawzhang/deep-dive-claude-code
- Agent OS analysis + teaching impl: tvytlx/claude-code-deep-dive
- Reverse-engineering repo: ComeOnOliver/claude-code-analysis
- Runnable fork: beita6969/claude-code
- Learn by rebuilding: shareAI-lab/learn-claude-code โ nano Claude Code agent built from scratch