传统聊天机器人的工作模式很简单:你说一句话,它回一句话。一问一答,仅此而已。但 Claude Code 不同——它是一个 Agent(智能体)。这意味着它不只是回答问题,而是主动执行任务。
Agentic Loop(智能体循环)是 Claude Code 的核心运行机制。每当你给 Claude Code 一个任务,它就会进入一个持续的循环:
这个循环会一直运行,直到任务完成,或者 Claude 判断自己无法继续为止。
在每一轮循环的开始,Claude 会分析当前的上下文:已经做了什么、结果是什么、离目标还有多远。这不是简单的模式匹配——Claude 会基于所有可用信息进行推理,制定下一步的行动计划。
Think 阶段的关键决策包括:
决定了要做什么之后,Claude 会调用相应的工具。每次只调用一个(或一组并行的)工具——这很重要,因为 Claude 需要看到每一步的结果才能决定下一步。
常见的行动包括:
Read 读取源代码文件Grep 搜索代码模式Edit 修改文件内容Bash 运行测试或执行命令Glob 查找文件路径工具执行后会返回结果——可能是文件内容、命令输出、搜索结果或错误信息。Claude 会仔细阅读这些结果,将其纳入上下文,作为下一轮思考的输入。
这就是 Agentic Loop 的强大之处:每一步的结果都会影响下一步的决策。Claude 不是在执行预定义的脚本,而是在动态地根据实际情况调整策略。
假设你说:“修复这个失败的测试”。Claude 的 Agentic Loop 可能会这样运行:
Round 1 - Think: 需要先了解哪个测试失败了
Act: Bash("npm test 2>&1 | tail -50")
Observe: TestSuite > validateEmail > 应该拒绝无效邮箱
Expected: false, Received: true
Round 2 - Think: validateEmail 函数有 bug,需要查看源码
Act: Grep("validateEmail", "src/")
Observe: 找到 src/utils/validator.ts:23
Round 3 - Think: 查看这个函数的实现
Act: Read("src/utils/validator.ts")
Observe: 发现正则表达式缺少对 ".." 的校验
Round 4 - Think: 修复正则表达式
Act: Edit("src/utils/validator.ts", ...)
Observe: 文件已更新
Round 5 - Think: 验证修复是否生效
Act: Bash("npm test")
Observe: All tests passed ✓
Round 6 - Think: 任务完成,返回结果
→ 输出修复报告给用户整个过程中 Claude 经历了 6 轮循环。每一轮都基于上一轮的观察结果做出新的决策。如果第 5 轮测试仍然失败,Claude 会继续循环、尝试不同的修复方案。
Agentic Loop 的一个关键优势是自动错误恢复。当工具调用失败时——比如文件不存在、命令执行报错、编辑冲突——Claude 不会停下来等待人工干预。它会:
例如,如果 Claude 试图编辑一个文件但发现匹配字符串不唯一,它会自动扩大上下文范围,提供更多的周围代码来精确定位编辑位置。
Agentic Loop 的核心逻辑可以用伪代码表示:
function agenticLoop(task: string, tools: Tool[]) {
let messages = [systemPrompt, userMessage(task)];
while (true) {
// Think: Claude 分析上下文,决定下一步
const response = await claude.generate(messages);
// 检查是否完成
if (response.type === "final_answer") {
return response.content; // 任务完成
}
// Act: 执行工具调用
if (response.type === "tool_call") {
const result = await executeTool(
response.toolName,
response.toolArgs
);
// Observe: 将结果加入上下文
messages.push(assistantMessage(response));
messages.push(toolResultMessage(result));
}
// 检查安全限制
if (messages.length > MAX_TURNS) {
return "达到最大轮次限制";
}
}
}理解 Agentic Loop 的关键在于与传统问答模式的对比:
这就是为什么 Claude Code 能够处理”修复这个 bug”这样的开放式任务——它不需要你告诉它每一步该做什么,它会自己找到路径。
Agentic Loop 不会无限运行。以下情况会触发终止:
Agentic Loop 是让 Claude Code 从”语言模型”进化为”智能体”的关键机制。正是这个循环,让 Claude 能够像一个真正的开发者一样工作——面对问题,分析、尝试、验证、迭代,直到问题解决。
Traditional chatbots work in a simple pattern: you say something, they reply. One question, one answer, done. But Claude Code is different — it is an Agent. This means it does not just answer questions — it actively executes tasks.
The Agentic Loop is the core runtime mechanism of Claude Code. Every time you give Claude Code a task, it enters a continuous cycle:
This loop keeps running until the task is complete, or Claude determines it cannot proceed.
At the start of each cycle, Claude analyzes the current context: what has been done, what the results were, and how far it is from the goal. This is not simple pattern matching — Claude reasons over all available information to formulate the next action plan.
Key decisions during the Think phase include:
After deciding what to do, Claude calls the appropriate tool. It calls one (or a batch of parallel) tools at a time — this is important because Claude needs to see the result of each step before deciding the next one.
Common actions include:
Read to read source code filesGrep to search for code patternsEdit to modify file contentsBash to run tests or execute commandsGlob to find file pathsAfter a tool executes, it returns results — file contents, command output, search results, or error messages. Claude carefully reads these results, incorporates them into the context, and uses them as input for the next thinking cycle.
This is the power of the Agentic Loop: the result of every step influences the decision of the next step. Claude is not executing a predefined script — it dynamically adjusts its strategy based on actual outcomes.
Suppose you say: “Fix the failing test.” Claude’s Agentic Loop might run like this:
Round 1 - Think: Need to find out which test is failing
Act: Bash("npm test 2>&1 | tail -50")
Observe: TestSuite > validateEmail > should reject invalid email
Expected: false, Received: true
Round 2 - Think: validateEmail has a bug, need to read source
Act: Grep("validateEmail", "src/")
Observe: Found src/utils/validator.ts:23
Round 3 - Think: Read the function implementation
Act: Read("src/utils/validator.ts")
Observe: Regex is missing validation for ".."
Round 4 - Think: Fix the regex
Act: Edit("src/utils/validator.ts", ...)
Observe: File updated
Round 5 - Think: Verify the fix works
Act: Bash("npm test")
Observe: All tests passed ✓
Round 6 - Think: Task complete, return results
→ Output fix report to userThroughout this process, Claude went through 6 cycles. Each cycle made decisions based on the observations from the previous one. If the test still failed in Round 5, Claude would continue looping, trying a different fix.
A key advantage of the Agentic Loop is automatic error recovery. When a tool call fails — a file doesn’t exist, a command errors out, an edit conflicts — Claude does not stop and wait for human intervention. It will:
For example, if Claude tries to edit a file but finds the match string is not unique, it automatically expands the context range, providing more surrounding code to precisely locate the edit position.
The core logic of the Agentic Loop can be expressed in pseudocode:
function agenticLoop(task: string, tools: Tool[]) {
let messages = [systemPrompt, userMessage(task)];
while (true) {
// Think: Claude analyzes context, decides next step
const response = await claude.generate(messages);
// Check if done
if (response.type === "final_answer") {
return response.content; // Task complete
}
// Act: Execute tool call
if (response.type === "tool_call") {
const result = await executeTool(
response.toolName,
response.toolArgs
);
// Observe: Add result to context
messages.push(assistantMessage(response));
messages.push(toolResultMessage(result));
}
// Safety limits
if (messages.length > MAX_TURNS) {
return "Maximum turn limit reached";
}
}
}Understanding the Agentic Loop requires contrasting it with traditional Q&A:
This is why Claude Code can handle open-ended tasks like “fix this bug” — it does not need you to tell it what to do at every step. It finds the path on its own.
The Agentic Loop does not run forever. The following conditions trigger termination:
The Agentic Loop is the key mechanism that evolves Claude Code from a “language model” into an “agent.” It is this loop that allows Claude to work like a real developer — facing a problem, analyzing, trying, verifying, and iterating until the problem is solved.