Ads

How Claude Code actually works – the agent loop, step by step

Discover how Claude Code works under the hood, from tool calls to permission gates. Learn the step-by-step agent loop that fixes bugs autonomously.

⏱ 9min 👁 2,202 views 📅 November 18, 2026

Summary

The core mechanism of Claude Code

Claude Code appears to act as an autonomous developer, modifying files and running commands on a local machine. In reality, it is a program that orchestrates a simple but powerful cycle. The language model at its center can only read and write text. It has no memory between requests and no direct access to the file system. Claude Code bridges this gap by acting as an intermediary, passing the model's requests through a controlled execution environment and feeding the results back into the conversation. This architecture allows a stateless model to perform stateful tasks, such as fixing a failing test or refactoring a project.

The key insight is that the model does not need to remember anything between requests because the entire context is resent each time. Claude Code maintains the conversation history, appends the results of tool calls, and presents the model with a fresh, complete context window on every iteration. This is the foundation of the agent loop, a design pattern that turns a text-only predictor into an interactive agent.

The agent loop as a five-step cycle

The agent loop begins with a user prompt, such as "fix the failing test." Claude Code packages the full conversation, including system instructions, tool definitions, and all previous messages, into a single request to the model. The model responds with either plain text or a structured request to use a tool. When the model requests a tool, Claude Code intercepts the response instead of displaying it directly. It checks the request against the user's permission rules, executes the command if allowed, and captures the output. That output is appended to the conversation as a tool result. The entire updated conversation is then sent back to the model for the next iteration.

The loop continues until the model replies without requesting a tool. At that point, Claude Code presents the final text response to the user. This pattern repeats for every interaction, regardless of complexity. Whether the model is reading a file, running tests, or editing code, the same five-step process governs each turn: receive input, call the model, parse the tool request, execute the tool, and feed the result back.

Tools as the interface between text and action

Tools are the mechanism that allows a text-only model to affect the real world. Claude Code defines a set of tools that the model can request, such as reading a file, writing to a file, running a shell command, or searching the codebase. Each tool has a name, a description, and a schema that specifies its parameters. When the model decides it needs information or needs to take an action, it emits a tool call with the appropriate arguments.

Importantly, the model does not actually run the tool. It merely generates the text that represents the request. Claude Code parses that text, validates the arguments, and performs the actual operation. This separation of concerns is essential. The model remains a pure text generator, while Claude Code acts as the executor. The tool result is then converted back into text and added to the conversation, allowing the model to interpret the outcome and decide on the next step.

The permission gate as a safety boundary

Every tool call passes through a permission gate before execution. This gate is the point where the user retains control over what runs on their machine. Claude Code checks each tool request against a set of rules that the user can configure. Some commands may be pre-approved, others may require confirmation, and still others may be denied entirely. The permission system is not merely a convenience; it is a critical safety mechanism that prevents the agent from taking harmful or unexpected actions.

The permission gate works by examining the tool request before execution. It considers the type of tool, the arguments provided, and the current permission settings. If a request matches an allow rule, it runs automatically. If it matches a deny rule, it is blocked. If it falls into the intermediate zone, the user is prompted to approve or reject the action. This creates a balance between automation and oversight, allowing Claude Code to work autonomously while keeping the user in charge of potentially dangerous operations.

Why the model keeps nothing between requests

One of the most counterintuitive aspects of Claude Code is that the language model has no memory between requests. Each call to the model is stateless. The model does not retain any information from previous turns outside of the text that is explicitly passed back to it. This is why Claude Code resends the entire conversation history on every iteration. The statelessness is not a limitation of Claude Code specifically, but a fundamental property of how large language models process requests.

The implication is that context length becomes a critical resource. As the conversation grows, the amount of text that must be resent increases. Eventually, the context window fills up, and the oldest parts of the conversation must be dropped or summarized. This is where compaction comes into play. Claude Code can compress earlier parts of the conversation into a shorter summary, preserving the essential information while freeing up space. However, compaction can also lose important details, which is why a model may sometimes forget a rule that was established earlier in a long session.

Compaction and the cost of forgetting

Compaction is the process of reducing the size of the conversation context to fit within the model's limits. When the context approaches its maximum length, Claude Code summarizes the earliest messages and replaces them with the summary. This allows the conversation to continue, but it comes at a cost. The summary is a lossy representation of the original messages, and important nuances can be lost in the process.

The video highlights a practical example: a model may forget a rule that was stated early in the conversation after compaction occurs. This is a natural consequence of trading precision for space. Understanding compaction is essential for working effectively with Claude Code over long sessions. It explains why the agent may occasionally repeat itself or fail to follow an instruction that was given many turns ago. Developers can mitigate this risk by periodically restating critical constraints or by structuring conversations to minimize the need for compaction.

The practical implications for developers

Claude Code's architecture has direct consequences for how developers use it. The agent loop means that every tool call consumes context, and the more tools the model uses, the faster the context fills. This creates a tension between autonomy and longevity. A model that runs many tests, reads many files, and makes many edits will produce a richer conversation, but it will also hit the context limit sooner. Developers should be aware of this trade-off and structure their prompts accordingly.

The permission system also has practical implications. By configuring allow and deny rules carefully, developers can grant Claude Code enough freedom to work autonomously without exposing their machines to unnecessary risk. The checkpoints feature adds another layer of safety, allowing users to rewind the conversation to an earlier state if the agent makes a mistake. Together, these mechanisms make Claude Code a more predictable and controllable tool.

The final lesson is that the agent loop is not magic. It is a simple, repeatable pattern that combines a stateless model with a stateful orchestrator, a set of tools, and a permission system. By understanding each component, developers can use Claude Code more effectively and build their own agent-based tools with the same principles.

What you will learn

  • Understand the five-step agent loop that powers Claude Code
  • Explain how tools bridge the gap between text generation and real-world actions
  • Describe the role of the permission gate in controlling what runs
  • Identify why compaction causes the model to forget earlier instructions
  • Apply the principles of the agent loop to design safer autonomous agents

Concepts covered

Technologies used

Chapters 10 markers

  1. One prompt, five steps
  2. Doing it by hand in a chat window
  3. Claude Code, tools and the tool call
  4. The permission gate
  5. The agent loop, one step at a time
  6. The whole control flow
  7. Why the model keeps nothing
  8. Compaction and forgetting rules
  9. Permissions and execution decisions
  10. Checkpoints and rewind

Next suggested video

Reviews

Student rating 0.0
★★★★★ 0 reviews
Rate this lesson

Help other students decide if this lesson is useful.

No reviews yet. Be the first to rate this lesson.