Chapter 01
The Agentic Loop
Everything Claude Code does flows through a single while(true) loop. Understanding this loop is understanding the entire system.
Source: query.ts, claude.ts, toolOrchestration.ts
When you send a message to Claude Code, it enters a loop. The AI thinks, decides if it needs to use any tools (read files, run commands, edit code), executes them, and then thinks again with the results. This repeats until the AI has everything it needs to give you a final answer.
(read, edit, bash...)
and loop again ↑
done!
Key insight: This loop is the ENTIRE architecture of Claude Code. Every feature — file editing, code search, git operations, multi-agent work — is just a tool that this loop can call. The AI decides which tools to use, and the loop handles the rest.
View source code (query.ts) ▶
// query.ts — the core loop
while (true) {
response = await callModel(messages, systemPrompt, tools)
if (response has tool_use) {
results = await runTools(toolUseBlocks)
messages.push(...response, ...results)
continue // loop back with tool results
}
return response // no tools needed — done
}Deep Dive: Inside the Pipeline Steps
The pipeline above showed you what happens at each step. These cards go deeper into the technical details — how the API call is constructed, how streaming works, how tools are executed, and how errors are handled. Click any card to expand.
The API Callclaude.ts▶
claude.ts▶SSE Streamingclaude.ts▶
claude.ts▶Tool Lifecycle (7 steps)toolOrchestration.ts▶
toolOrchestration.ts▶Recovery & Error HandlingwithRetry.ts▶
withRetry.ts▶Tool Concurrency
When the AI asks to use 5 tools at once, Claude Code doesn't run them one by one. It splits them into two groups:
Read-only tools
Read, Glob, Grep, WebFetch — these can't break anything, so they run all at the same time. Up to 10 in parallel.
Write tools
Bash, Edit, Write — these modify files, so they run one at a time to avoid conflicts.
The chart below shows 5 tool calls. Notice how the 3 reads all start at the same time (0ms), while Edit waits for reads to finish, and Bash waits for Edit.
Tool Concurrency
Read-only tools run in parallel. Writes wait and execute one-by-one.
Source Files
query.tsThe while(true) loop, auto-compactionclaude.tsAPI request, SSE streaming, cache breakpointstoolOrchestration.tsConcurrency partitioning, runTools()Tool.tsBase tool interface, 40+ tool types (30KB)withRetry.tsRetry matrix, backoff, model fallbackautoCompact.tsContext compression, 9-section summary