Skip to content

Luxshan2000/inside-claude-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” Inside Claude Code

Architecture Deep Dive β€’ Flow Diagrams β€’ How Every Feature Works Under the Hood

Documentation Architecture Deep Dives Stars License

Ever wondered how Claude Code actually works? How it shrinks memory, executes tools, manages permissions, or streams responses? This repo breaks it all down β€” one diagram at a time.


🧠 What Is This?

Claude Code (and its open-source sibling Claw Code) is an agentic AI coding assistant that lives in your terminal. It can read files, write code, run commands, search the web, and orchestrate complex multi-step tasks β€” all while managing context, permissions, and tool execution autonomously.

This repository is a visual architecture guide. No code. Just diagrams, explanations, and deep dives into every subsystem that makes the magic happen.

Whether you're:

  • πŸ—οΈ Building your own AI agent and want to learn from Claude Code's architecture
  • πŸ” Curious about how agentic loops work under the hood
  • πŸ“š Studying software architecture patterns in production AI systems
  • πŸ› οΈ Contributing to Claw Code / Claude Code and need to understand the internals

...this repo is for you.


πŸ—οΈ Architecture Map

The 30,000-foot view. How all the pieces fit together.

graph TB
    subgraph "πŸ–₯️ User Interface"
        CLI["CLI / REPL"]
        SLASH["Slash Commands<br/>/compact /model /help"]
    end

    subgraph "🧠 Core Runtime"
        LOOP["Agentic Conversation Loop"]
        PROMPT["System Prompt Builder"]
        COMPACT["Memory Compaction"]
        SESSION["Session Manager"]
    end

    subgraph "πŸ”§ Tool Ecosystem"
        TOOLS["Built-in Tools<br/>bash, read, write, edit, glob, grep"]
        MCP["MCP Servers<br/>stdio, SSE, WebSocket"]
        HOOKS["Hook System<br/>PreToolUse / PostToolUse"]
    end

    subgraph "πŸ” Security Layer"
        PERMS["Permission Model<br/>ReadOnly β†’ WorkspaceWrite β†’ DangerFull"]
        SANDBOX["Sandbox Execution"]
        AUTH["Auth: OAuth PKCE + API Keys"]
    end

    subgraph "🌐 External"
        API["Anthropic API<br/>Streaming SSE"]
        CONFIG["Config Hierarchy<br/>User β†’ Project β†’ Local"]
        GIT["Git Integration"]
    end

    CLI --> LOOP
    SLASH --> LOOP
    LOOP --> PROMPT
    LOOP --> COMPACT
    LOOP --> SESSION
    LOOP --> TOOLS
    LOOP --> MCP
    TOOLS --> HOOKS
    TOOLS --> PERMS
    TOOLS --> SANDBOX
    LOOP --> API
    PROMPT --> CONFIG
    PROMPT --> GIT
    API --> AUTH
Loading

πŸ“– Full Architecture Overview β†’


πŸ”¬ Deep Dives

Each doc below zooms into one specific feature with flow diagrams, sequence diagrams, and detailed explanations.

# Feature What You'll Learn Status
00 Architecture Overview High-level system design, crate structure, data flow βœ…
01 The Agentic Conversation Loop How the core loop streams, calls tools, and iterates βœ…
02 Memory Shrinking & Context Compaction How Claude Code fits infinite conversations into finite context βœ…
03 Tool System Tool registration, execution, input schemas, built-in tools βœ…
04 Permission Model Three-tier authorization, interactive prompting, escalation βœ…
05 MCP Integration Model Context Protocol β€” extending tools via external servers βœ…
06 Hook System Pre/Post tool hooks, exit codes, environment variables βœ…
07 Session Management Persistence, resume, message serialization βœ…
08 Streaming & SSE Parsing Server-Sent Events, incremental parsing, real-time output βœ…
09 Configuration System Multi-level config, deep merge, feature extraction βœ…
10 Authentication OAuth PKCE flow, API keys, token refresh βœ…
11 CLI & REPL Terminal rendering, markdown output, input handling βœ…
12 Sandbox Execution Linux namespace isolation, container detection βœ…
13 System Prompt Building Dynamic prompt construction, CLAUDE.md discovery βœ…
14 Slash Commands Command registry, parsing, execution βœ…
15 Error Handling & Retry Error types, exponential backoff, retryability βœ…
16 Bootstrap Lifecycle Startup phases, initialization sequence βœ…

🧩 Key Concepts at a Glance

The Conversation Loop β€” The beating heart

graph LR
    A["πŸ‘€ User Message"] --> B["πŸ“‘ Stream to API"]
    B --> C{"πŸ€– Response Type?"}
    C -->|"Text"| D["πŸ’¬ Display Text"]
    C -->|"Tool Use"| E["πŸ”§ Execute Tool"]
    E --> F["πŸ” Check Permission"]
    F --> G["⚑ Run Hook: PreToolUse"]
    G --> H["πŸƒ Execute"]
    H --> I["⚑ Run Hook: PostToolUse"]
    I --> J["πŸ“¨ Send Result to API"]
    J --> C
    C -->|"End Turn"| K["βœ… Done"]
Loading

Memory Compaction β€” How infinite conversations fit in finite context

graph TD
    A["Token Count Check"] --> B{"Exceeds Budget?<br/>default: 200K tokens"}
    B -->|"No"| C["Continue Normally"]
    B -->|"Yes"| D["Identify Old Messages"]
    D --> E["Generate Summary<br/>of Removed Turns"]
    E --> F["Inject Summary as<br/>System Context"]
    F --> G["Preserve Recent N<br/>Messages Intact"]
    G --> H["Resume with<br/>Compacted Context"]
Loading

Permission Tiers β€” Security by design

graph LR
    RO["🟒 ReadOnly<br/>read files, glob, grep"]
    WW["🟑 WorkspaceWrite<br/>+ write files, edit"]
    DA["πŸ”΄ DangerFullAccess<br/>+ bash, network, web"]

    RO --> WW --> DA
Loading

πŸ“Š System at a Glance

Component Details
Modular Architecture Separate modules for API, commands, runtime core, CLI, and tools
Built-in Tools 18 tools β€” file ops, shell, search, web, orchestration
MCP Transports 6 types β€” stdio, SSE, HTTP, WebSocket, SDK, proxy
Permission Tiers 3 levels β€” ReadOnly, WorkspaceWrite, DangerFullAccess
Config Sources 5 locations β€” user, project, local, CLI flags, env vars
Slash Commands 15+ β€” /compact, /model, /permissions, /cost, /diff, and more
Bootstrap Phases 12 ordered startup phases
Supported Models Opus, Sonnet, Haiku model families

πŸ—‚οΈ Repository Structure

inside-claude-code/
β”œβ”€β”€ README.md                              ← You are here
β”œβ”€β”€ LICENSE
β”œβ”€β”€ assets/
β”‚   └── banner.svg
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ 00-architecture-overview/          ← Start here
β”‚   β”œβ”€β”€ 01-conversation-loop/
β”‚   β”œβ”€β”€ 02-memory-and-context/
β”‚   β”œβ”€β”€ 03-tool-system/
β”‚   β”œβ”€β”€ 04-permission-model/
β”‚   β”œβ”€β”€ 05-mcp-integration/
β”‚   β”œβ”€β”€ 06-hook-system/
β”‚   β”œβ”€β”€ 07-session-management/
β”‚   β”œβ”€β”€ 08-streaming-and-sse/
β”‚   β”œβ”€β”€ 09-config-system/
β”‚   β”œβ”€β”€ 10-authentication/
β”‚   β”œβ”€β”€ 11-cli-and-repl/
β”‚   β”œβ”€β”€ 12-sandbox-execution/
β”‚   β”œβ”€β”€ 13-system-prompt-building/
β”‚   β”œβ”€β”€ 14-slash-commands/
β”‚   β”œβ”€β”€ 15-error-handling-and-retry/
β”‚   └── 16-bootstrap-lifecycle/

🌟 Why This Exists

The agentic AI coding assistant space is exploding. Claude Code, Cursor, Windsurf, Copilot β€” they all share similar architectural patterns. Understanding how one works gives you superpowers to:

  1. Build your own agentic coding tools
  2. Debug issues when things go wrong
  3. Extend and customize existing tools
  4. Learn production architecture patterns for AI systems

This repo distills thousands of lines of Rust into clear, visual documentation anyone can understand.


🀝 Contributing

Found a mistake? Want to add a diagram? PRs welcome!

  1. Fork the repo
  2. Create a feature branch
  3. Add or improve docs in the docs/ folder
  4. Use Mermaid for diagrams
  5. Submit a PR

πŸ“œ License

MIT β€” Use these docs however you like.


⭐ Star History

If this helped you understand how AI coding assistants work, drop a star!

Star History Chart


Built with πŸ” curiosity and πŸ“ diagrams
By @Luxshan2000

About

πŸ” Inside Claude Code β€” Architecture Deep Dive | Flow Diagrams, UML, Sequence Diagrams | How Memory Shrinking, Tool Execution, Permissions & MCP Work Under the Hood

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors