Claude Code Deep Dive Part 2: The 1,421-Line While Loop That Runs Everything
This is Part 2 of our Claude Code Architecture Deep Dive series. Part 1: 5 Hidden Features covered the surface-level discoveries. Now we go deeper. The Heart of Claude Code Every AI coding agent — ...

Source: DEV Community
This is Part 2 of our Claude Code Architecture Deep Dive series. Part 1: 5 Hidden Features covered the surface-level discoveries. Now we go deeper. The Heart of Claude Code Every AI coding agent — Claude Code, Cursor, Copilot — runs some version of the same loop: send context to an LLM, get back text and tool calls, execute tools, feed results back, repeat. We called this LLM talks, program walks. But Claude Code's implementation of this loop is anything but simple. It lives in query.ts, a 1,729-line async generator. The while(true) starts at line 307 and ends at line 1728 — a single loop body spanning 1,421 lines of production code. This is not a toy. This is the engine that processes every keystroke, every tool call, every error recovery, every context compression decision for millions of users. // query.ts — line 307 // eslint-disable-next-line no-constant-condition while (true) { let { toolUseContext } = state const { ... } = state // ... 1,421 lines of state machine logic ... stat