How Claude Code Works β Deep Dive
The Agentic Loop
Section titled βThe Agentic LoopβClaude Code is not a chatbot. It is an agent β a system that takes actions, observes results, and loops until the task is done.
User prompt β βΌββββββββββββββββββββββββββββββββββββ Plan: what steps are needed? βββββββββββββββββ¬βββββββββββββββββββ β ββββββββββββΌβββββββββββ β Choose a tool call β βββββββββββ ββββββββββββ¬βββββββββββ β β β ββββββββββββΌβββββββββββ β β Execute the tool β β ββββββββββββ¬βββββββββββ β β β ββββββββββββΌβββββββββββ β β Observe the result β β ββββββββββββ¬βββββββββββ β β β ββββββββββββΌβββββββββββ β β Task complete? βββ No ββββββββ ββββββββββββ¬βββββββββββ β Yes ββββββββββββΌβββββββββββ β Reply to user β βββββββββββββββββββββββThis loop runs entirely on Claudeβs side. You see the final answer, but many tool calls may happen silently in between.
The Tool System
Section titled βThe Tool SystemβEverything Claude does is through tools. Claude cannot do anything that isnβt backed by a tool.
Built-in Tools
Section titled βBuilt-in Toolsβ| Tool | What it does |
|---|---|
Read | Read a file from disk |
Write | Create a new file |
Edit | Modify part of an existing file |
Glob | Find files by pattern |
Grep | Search file contents with regex |
Bash | Execute a shell command |
WebFetch | Fetch a URL |
WebSearch | Search the web |
Agent | Spawn a subagent |
TodoWrite / TodoRead | Manage task lists |
How a Tool Call Works
Section titled βHow a Tool Call Worksβ- Claude decides it needs to read a file
- Claude generates a tool call:
Read("src/index.ts") - The harness intercepts β checks permissions
- If allowed: executes the read, returns content
- If denied (or user says no): Claude sees an error and adjusts
- Claude incorporates the file content and continues planning
Parallel Tool Calls
Section titled βParallel Tool CallsβClaude can issue multiple tool calls in a single step when they are independent:
Step 1: Read("src/auth.ts"), Read("src/routes.ts"), Read("tests/auth.test.ts") β all three run in parallel βStep 2: Edit("src/auth.ts") based on findingsThis is why Claude Code is fast β it doesnβt wait for each read one by one.
How Context Is Built
Section titled βHow Context Is BuiltβClaudeβs context window at any point contains:
βββββββββββββββββββββββββββββββββββββββββββββββ System prompt (built-in + CLAUDE.md) ββ Auto memory index (MEMORY.md) ββ Project files (if loaded) βββββββββββββββββββββββββββββββββββββββββββββββ€β Conversation history ββ βββ User messages ββ βββ Claude's text responses ββ βββ Tool call results (file contents, ββ command output, search results) βββββββββββββββββββββββββββββββββββββββββββββββContext window size is finite. As the conversation grows, older content is compressed or dropped. The /compact command or auto-compact (when enabled) summarizes the conversation to free space.
How Files Are Read
Section titled βHow Files Are ReadβClaude does NOT load your entire codebase upfront. It reads files on demand as it needs them.
The typical flow for βfix this bugβ:
- Claude reads the file mentioned in your prompt
- Discovers imports β reads those files
- Discovers the test file β reads it
- Reads related config if needed
- Makes targeted edits
This is why naming the file in your prompt helps: it tells Claude where to start.
How CLAUDE.md Is Loaded
Section titled βHow CLAUDE.md Is LoadedβSession start β βΌLoad managed CLAUDE.md (org-wide) β βΌLoad ~/.claude/CLAUDE.md (user) β βΌLoad ./CLAUDE.md or ./.claude/CLAUDE.md (project) β βΌLoad ./CLAUDE.local.md (personal, gitignored) β βΌLoad .claude/rules/*.md for any files already in context β βΌAll loaded content goes into the system promptImport resolution: @./path/to/file.md is resolved once at load time and the file content is inlined. It is not re-read on every turn.
Path-scoped rules are loaded dynamically: when Claude opens a file that matches a ruleβs paths: pattern, that rule file is added to the context for that turn.
How Sessions Work
Section titled βHow Sessions WorkβA session = one conversation thread with a unique ID.
~/.claude/projects/<project-hash>/sessions.jsonlEach session stores:
- All messages and tool calls
- Which files were read/edited
- Timestamps and cost
Session lifecycle:
claudeβ new sessionclaude --continueβ resumes most recent sessionclaude --resumeβ pick any past sessionCtrl+Dor/exitβ ends session cleanly
Sessions are kept for cleanupPeriodDays (default 30 days), then purged.
How Permissions Flow
Section titled βHow Permissions FlowβUser types a prompt β βΌClaude plans a tool call β βΌHarness checks: is this tool in the allow list? β βββ Yes, pre-approved β execute immediately β βββ No, in deny list β return error to Claude β βββ Unknown β check defaultMode β βββ default / acceptEdits β prompt user β User approves β execute β User denies β return error β βββ auto β analyze safety β execute or prompt β βββ plan β block all writes/shell β βββ bypassPermissions β execute alwaysPre-approved tools (in allow list) never prompt. This is why configuring your allow list in .claude/settings.json makes your workflow much smoother.
How Auto Memory Works
Section titled βHow Auto Memory WorksβDuring a session: Claude notices something worth remembering (e.g. "tests require npm install first") β βΌ Claude calls Write / Edit on a file in ~/.claude/projects/<hash>/memory/ β βΌ Updates MEMORY.md index
Next session: MEMORY.md is loaded into system prompt Claude reads referenced memory files on demandThe memory system is just files β you can read, edit, or delete them at any time.
How Subagents Work
Section titled βHow Subagents WorkβWhen Claude spawns a subagent:
Main Claude session β β Agent("Explore", "find all auth middleware") βΌβββββββββββββββββββββββββββββββββ New Claude instance ββ - Fresh context window ββ - Inherits tool permissions ββ - Isolated from main sessionββ - Runs in parallel (opt) βββββββββββββ¬ββββββββββββββββββββ β β Returns: result text βΌMain Claude continues with resultSubagents:
- Have their own isolated context (they donβt see your full conversation)
- Can be given restricted tool lists
- Can run in separate git worktrees (so their file edits donβt conflict)
- Are billed separately (their tokens count toward your usage)
What Happens When You Press Enter
Section titled βWhat Happens When You Press Enterβ- Your message is appended to the conversation
- All registered hooks for
PreMessagefire (if any) - Claude processes the full context and generates a response
- If the response contains tool calls, the harness executes them
- Tool results are added to context
- Claude generates the next response
- This repeats until Claude produces a final text-only response
PostMessagehooks fire- The response is displayed to you
How Compact / Auto-Compact Works
Section titled βHow Compact / Auto-Compact WorksβWhen the context grows large:
/compactor auto-compact triggers- Claude is asked to summarize the conversation so far
- The summary replaces the conversation history
- Full context of recently-read files is retained
- The summary is much smaller β Claude can continue
This is lossy β detail from early in the conversation may be lost. Use /recap before /compact to preserve a human-readable summary.
π§ Quick Recall
Section titled βπ§ Quick Recallβ- Trick: βOne model + tools in a loop β no magic.β
- Everything is visible:
Ctrl+Oshows the full transcript.
π¨βπ« Teach It
Section titled βπ¨βπ« Teach Itβ- Say: understanding the loop is what demystifies every failure later.
- Show: one turn in verbose transcript β name each tool call as it streams.
- Theyβll trip on: thinking Claude βremembersβ β every turn re-sends everything (caching makes it cheap).
Learning path: β¬ 05-interactive-mode-shortcuts.md Β· Index Β· β‘ 07-tools-deep-dive.md
Written by Fenil Patel