Claude Code Agent Architecture: Single-Threaded Master Loop for Autonomous Coding
anthropic · Tech · 2025 · source Agentic: yes · promotional bias: med · source-backed · extracted with prompt 0.5.5 (schema v2)
Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.
Problem
Domain: Autonomous coding agents and developer tooling
Claude Code needed to perform autonomous code search, editing, testing, and response generation while staying debuggable, steerable, reviewable, and safe enough for production developer workflows.
Constraints:
- Autonomous code editing requires transparent tool use and reviewable diffs.
- Long-running tasks need planning, memory, and context management.
- Developer tools can be dangerous, so write operations and shell commands need permissions and risk controls.
- The system must remain interactive so users can steer it mid-task instead of restarting.
Architecture
Claude Code uses a deliberately simple single-threaded master loop (nO) connected to a real-time steering queue (h2A), a ToolEngine/Scheduler, sandboxed developer tools, TodoWrite planning, at-most-one sub-agent branch, permission gates, diff-based editing, and context compression into project memory.
Diagram confidence: explicit (topology described by the source).
flowchart LR user-interaction-layer["user interaction layer"] --> h2a-async-queue["h2A async queue"] h2a-async-queue --> no-master-loop["nO master loop"] no-master-loop --> flat-message-history["flat message history"] no-master-loop --> toolengine-scheduler["ToolEngine Scheduler"] no-master-loop --> todowrite-planner["TodoWrite planner"] no-master-loop --> sub-agent-branch["sub-agent branch"] toolengine-scheduler --> developer-tools["developer tools"] toolengine-scheduler --> permission-system["permission system"] permission-system --> developer-tools developer-tools --> diff-workflow["diff workflow"] diff-workflow --> user-interaction-layer flat-message-history --> compressor-wu2["Compressor wU2"] compressor-wu2 --> claude-md-project-memory["CLAUDE.md project memory"]
Components
| Component | Role | Interacts with |
|---|---|---|
user interaction layer |
CLI, VS Code plugin, or web UI where developers communicate with Claude Code. | h2A async queue |
h2A async queue |
Handles asynchronous events, pause/resume, and user interjections during work. | user interaction layer, nO master loop |
nO master loop |
Single-threaded agent loop that asks the model for tool calls, executes them, and repeats until a final answer. | h2A async queue, flat message history, ToolEngine Scheduler, TodoWrite planner, sub-agent branch |
flat message history |
Stores the conversation and tool results without complex threading. | nO master loop, Compressor wU2 |
ToolEngine Scheduler |
Orchestrates tool invocations and queued model queries. | nO master loop, developer tools, permission system |
developer tools |
View, Grep, Edit, Bash, WebFetch, and BatchTool operations used to inspect and modify code. | ToolEngine Scheduler, diff workflow |
TodoWrite planner |
Maintains structured task lists with statuses and priorities for multi-step work. | nO master loop, user interaction layer |
sub-agent branch |
Controlled one-level parallel exploration for searches or alternative approaches. | nO master loop |
permission system |
Requires allow/deny decisions for writes, risky Bash commands, MCP, and web access. | ToolEngine Scheduler, developer tools |
diff workflow |
Shows reviewable diffs and supports small, reversible code changes. | developer tools, user interaction layer |
Compressor wU2 |
Summarizes conversations near context limits and writes important information to project memory. | flat message history, CLAUDE.md project memory |
CLAUDE.md project memory |
Markdown long-term memory file for project context. | Compressor wU2 |
Data flow
User input enters through the CLI/editor/web UI, h2A queues live steering events, the nO master loop asks the model what to do, ToolEngine executes JSON tool calls in sandboxed tools, results return as plain text into the flat message history, TODO reminders keep the plan visible, Compressor wU2 summarizes near context limits into CLAUDE.md memory, and the loop terminates when the model returns a plain text answer.
3 supporting quotes
Why this architecture
The architecture favors a constrained single-loop design over swarms because debuggability, transparent audit trails, real-time steering, and reviewable diffs matter more for coding work than unconstrained parallel autonomy.
3 supporting quotes
Models used: Claude Code model · Claude
Scale
- Users reportedly ran Claude Code continuously enough that Anthropic added weekly limits.“Anthropic had to add weekly limits after users ran Claude Code 24/7”article, stated
- Compressor wU2 is described as triggering at approximately 92% context-window usage.“automatically triggers at approximately 92% context window usage to summarize conversations”article, stated
- The View tool defaults to about 2000 lines.“The View tool reads files (defaulting to about 2000 lines)”article, stated
Alternatives considered (roads not taken)
Complex multi-agent swarms
Rejected because: The architecture intentionally keeps one main thread and allows at most one sub-agent branch to avoid uncontrolled proliferation.
Batch-mode autonomous coding
Rejected because: h2A allows mid-task user interjections so users can redirect the agent without restarting the job.
Database-backed memory and semantic search everywhere
Rejected because: The source emphasizes simple Markdown memory and regex/search tools where simpler mechanisms suffice.
Unmediated shell and write access
Rejected because: Claude Code uses permissions, risk classification, command sanitization, and confirmation prompts for dangerous operations.
Patterns
tool-use-orchestration (agent-runtime)
The master loop repeatedly selects developer tools, executes them through the scheduler, and feeds results back until no more tool calls are needed.
while(tool_call) → execute tool → feed results → repeat. The loop continues as long as the model's response includes tool usage;”article, statedcontext-compaction (memory)
Compressor wU2 summarizes conversations near context limits and stores important information in CLAUDE.md.
agent-memory (memory)
Claude Code uses CLAUDE.md as persistent project memory alongside the compressed message history.
multi-agent-orchestration (controlled-parallelism)
Sub-agents can explore wide searches or alternate approaches, but they feed results back to the main loop and cannot recursively spawn more agents.
human-in-the-loop-gate (safety)
Permission prompts and diff review keep developers in control of writes, risky shell commands, external tools, and generated code changes.
Tradeoffs
Use a single-threaded master loop.
Chose One flat main loop with simple tool-call repetition over A multi-agent swarm architecture.
Rationale: The article argues the single loop is easier to debug, audit, and steer while still delivering practical autonomy.
Allow controlled sub-agents only for bounded exploration.
Chose At most one depth-limited sub-agent branch over Recursive or unconstrained parallel agents.
Rationale: The system still supports wide searches and alternative approaches but prevents recursive agent explosion.
Use diff-first editing.
Chose Surgical patches, minimal diffs, and reviewable changes over Opaque file rewrites or unreviewed code generation.
Rationale: Diffs make AI-generated changes transparent and easier to review, revert, and test.
Gotchas (what bit them)
Multi-agent proliferation can reduce reliability and debuggability.
Root cause: Multiple personas and uncontrolled recursive branches make it harder to understand who is controlling the task.
Resolution: Use one main loop and at most one sub-agent branch with depth limitations.
Long coding sessions can lose task state.
Root cause: The agent's context window fills and the model can forget objectives in long conversations.
Resolution: Use TODO reminders, flat message history, compression, and CLAUDE.md project memory.
Developer tools can create security or damage risks.
Root cause: Bash, write operations, MCP/web calls, and command injection vectors can affect real projects and environments.
Resolution: Classify command risk, sanitize dangerous constructs, require permissions, and show diffs.
$() constructs)”article, statedEvaluation
The case evaluates Claude Code qualitatively through architecture transparency, tool auditability, safety controls, and adoption signal from heavy usage that led to weekly limits.
Metrics: weekly usage limits after 24/7 use · tool-call audit trail · permission decisions · risk-classified Bash commands · diff reviewability · context-window usage threshold around 92%
3 supporting quotes
Outcomes
- Claude Code's architecture is presented as a controllable autonomous coding agent built from a simple loop and disciplined tools.
- The system reportedly saw enough continuous use that Anthropic added weekly limits.
- The strongest production lesson is that constrained simplicity can outperform more elaborate multi-agent orchestration for coding work.
Reported scale details include users running Claude Code 24/7, weekly usage limits, a 92% context-compression trigger, and file reads defaulting to about 2000 lines.