Architecture & Internals

How Claude Code Works

Claude Code is not a simple chat wrapper with shell access. It is an autonomous agent harness engineered around a closed-loop observation engine, sandboxed execution primitives, precision file rewrites, and instant rollback checkpoints.

1. The 3-Stage Agentic Loop

Every prompt submitted to Claude Code triggers a disciplined feedback cycle that mirrors an elite software engineer's workflow:

  1. Gather Context: Before modifying a single character, Claude inspects the environment. It parses your CLAUDE.md project manual, runs ripgrep to find symbol definitions, queries language servers (LSP), and reads the specific lines of relevant files. It avoids context bloat by loading only bounded segments into context.
  2. Take Action: Claude selects an appropriate tool. When editing code, it performs contiguous string replacements using exact text matching. When executing terminal commands, it uses the Bash tool within your permission boundary. If a subtask requires broad codebase exploration, it spawns an isolated subagent so raw search output does not contaminate your main session.
  3. Verify Results: Claude does not guess if its change worked. It executes your test suite, compiler checks, or linter. If the exit code is non-zero, it reads the compiler error output, adjusts its plan, and applies a fix before concluding the turn.

2. The 5 Tool Categories

Claude Code exposes a tightly audited suite of native tools to the model:

CategoryToolsOperational Mechanism
File OperationsRead, Edit, WriteReads files within token limits (up to 25k tokens per read). Edits perform single contiguous replacements with strict target matching. Writes create atomic files on disk.
Code SearchGrep, GlobNative ripgrep queries across file contents and globbing patterns across directory trees. Skips gitignored files by default.
ExecutionBashRuns shell commands inside a persistent subshell. Captures stdout and stderr. Protected by classifier and deny rules.
Web & NetworkWebFetchFetches documentation, RFCs, and API schemas over HTTPS. Converts HTML responses to clean markdown.
Code IntelligenceLSPIntegrates with language servers (TypeScript, Rust, Python, Go) for instant go-to-definition, find-references, and type diagnostics.

3. Rollback Checkpoints & Rewinds

One of Claude Code's most powerful safety mechanisms is automatic turn-based checkpointing. Before modifying any file on disk, Claude Code records an internal snapshot of the modified paths.

If Claude makes a change you do not like, or if you want to explore a different implementation approach:

  • Press Esc twice on an empty prompt to open the checkpoint menu.
  • Type /rewind to select an exact earlier turn to restore.
  • Both file changes on disk and the conversation context roll back seamlessly.
Note on Shell Changes: Checkpoints track files modified through Claude's native file tools (Edit, Write). Files altered directly by a raw Bash command (such as sed or rm) are not tracked by checkpoints and should be managed via git.

4. Permission Modes: From Strict to Autonomous

Claude Code balances developer safety with autonomous velocity across 5 distinct permission levels:

ModeHow It OperatesBest For
Auto Mode (Default)Runs routine commands and edits autonomously. A classifier model evaluates risky actions (production deploys, force pushes, secret reads) and prompts for approval.Everyday feature development in terminal and VS Code
Plan ModeReads files and gathers context freely, but blocks all file edits and destructive commands. Proposes an execution plan for review.Large refactors, architectural planning, sensitive production repos
Manual ModePrompts the user to approve every file write and every non-read-only terminal command.Strict compliance environments and auditing new workflows
Accept Edits ModeAuto-approves file edits without prompting, but continues to prompt for shell command execution.Rapid drafting where you trust file edits but want terminal safety
dontAsk ModePermits only pre-approved tools (passed via --allowedTools) and immediately refuses anything else without prompting.Headless CI/CD pipelines and automated cron jobs

5. The .claude Directory Structure

Claude Code separates project-level configurations (committed to source control) from local user preferences and global machine settings:

my-project/ ├── .claude/ │   ├── settings.json       # Project-wide settings, hooks, and deny rules (committed) │   ├── settings.local.json # Developer-specific overrides (gitignored) │   ├── rules/              # Scoped instructions with path-matching frontmatter │   │   ├── api.md          # Applied only when editing src/api/**/*.ts │   │   └── frontend.md     # Applied only when editing src/components/**/*.tsx │   ├── skills/             # On-demand procedures (SKILL.md) │   │   └── release/ │   │       └── SKILL.md    # Triggered via /release │   └── agents/             # Custom subagent definitions │       └── reviewer.md     # Subagent with restricted tools (Read, Grep, Glob) └── CLAUDE.md               # Project-level single source of truth guidelines (<200 lines)

6. Extensibility: Choosing the Right Abstraction

Claude Code provides six distinct mechanisms for customization. Choosing the correct layer prevents context bloat:

  • CLAUDE.md: For persistent repo-wide facts Claude cannot discover from code (test commands, architecture gotchas). Always loaded.
  • Path-Scoped Rules (.claude/rules/): For rules that only apply to specific subdirectories (e.g. database schema migrations).
  • Skills (SKILL.md): For multi-step procedures (e.g. cutting a release, running a migration) that should only load when called.
  • Subagents (.claude/agents/): For parallel tasks or independent code reviews where you want a fresh context and restricted tools.
  • Lifecycle Hooks: For deterministic enforcement (e.g. blocking .env reads, auto-running Prettier, verifying tests before turn completion).
  • Model Context Protocol (MCP): For connecting external services (Notion, GitHub, BigQuery, Postgres) to Claude.
Next Step: Learn how prompt caching preserves state and lowers billing by 90%: Explore Prompt Caching Architecture →