Claude Code Runs on a 20-Line While Loop. That's the Whole Architecture.
Watch on TikTok
The engine behind Claude Code is a while True loop in Python, roughly 20 lines long. The creator of this video reverse-engineered Anthropic's CLI agent and found that the entire orchestration layer fits inside a single function called run_master_loop. There is no task-specific routing, no conditional branching for different job types, and no hand-written retry logic. The model decides what to do, and a thin harness executes it.
The Three-Step Cycle
The video presents the loop as a repeating cycle with three phases, shown in a clean diagram with the presenter's face at the center and three labeled nodes connected by directional arrows.
Perception. Claude receives the full conversation history, including every previous tool call and its result. This is the model's entire view of the world at each iteration.
Action. Based on that context, Claude decides to call a tool. That might be running a bash command, reading a file, editing code, or performing a search. The model picks the tool and the arguments.
Observation. The harness executes the requested tool call, captures the output, and appends it back into the conversation history as a new user message. Then the loop starts over.
This cycle repeats until the model returns a response with a stop reason other than tool_use. At that point, the loop breaks, and the final answer is delivered.
The Actual Code
One of the frames in the video shows the complete harness in a file called harness.py. The function signature is def run_master_loop(history, tools). Inside, a while True loop calls client.messages.create with the model, a system prompt, the conversation history, a set of basic tools, and a max token limit of 8,000. The assistant's response gets appended to the history. If the stop reason is anything other than tool_use, the loop breaks. Otherwise, the harness dispatches the tool calls via dispatch_tools(response.content, tools), appends the tool outputs as a user message, and loops again.
That is the entire control flow. There is no state machine, no planner, no task decomposition layer wrapping it.
Why It Self-Corrects
The self-correction behavior that makes Claude Code feel reliable comes from the same loop. When a bash command fails or a file read returns an error, that error text goes straight back into the conversation history on the next iteration. Claude sees the failure in context, adjusts its approach, and tries something different. The video emphasizes this with a frame showing "retry logic" and "fallback branches" crossed out, replaced by the phrase "just the loop." There are no human-authored fallback conditions. The model handles recovery the same way it handles everything else: by reading the full context and deciding what to do next.
The Design Philosophy
A key frame in the video displays the text "Anthropic didn't write special logic. They trusted the model." The architectural bet here is that a sufficiently capable model, given the right tools and the full conversation history, will make good decisions about what to do next without needing a complex orchestration framework on top. The harness never makes decisions. It only executes what Claude requests. That separation between model-as-decision-maker and harness-as-executor is what allows the system to scale. You build the loop once, and from that point forward, expanding Claude Code's capabilities means adding new tools, not rewriting control flow.
Key Takeaways
- Claude Code's master loop is a single
while Trueloop in Python, approximately 20 lines long, with no task-specific branching. - The loop follows a Perception-Action-Observation cycle: read context, call a tool, feed the result back in, repeat.
- Self-correction happens naturally because errors are appended to the conversation history and the model adapts on the next iteration.
- The harness never makes decisions. It executes tool calls and returns results. All reasoning lives in the model.
- Extending the system means adding tools, not modifying the loop. The control flow stays the same whether the task is a one-line fix or a full codebase refactor.
Published May 26, 2026. Writeup generated from a favorited TikTok.