Trae-Agent 核心执行思路深度解析

引言

同事给我分析了最近的开源项目Trae Agent。 一个通过自然语言使用各种工具和大语言模型提供商执行复杂的软件工程工作流的Agent。看了一下目前代码量不算多,核心流程也不算复杂,可以作为学习项目研究一番。

总结

先说总结,如果把Trae-Agent比如一个人,那么:

🧠 LLM = 大脑

  • 负责分析、决策、推理

  • 根据当前信息决定下一步行动

  • 本身是无状态的,每次都需要重新"思考"

📝 sequential_thinking = 笔记本

  • 记忆功能 :记录"我想到了什么"、"我计划做什么"

  • 进度跟踪 :"我现在执行到第几步了"

  • 思考规范 :强制大脑按结构化方式思考

🤲 其他工具 = 身体的各个部分

  • edit_tool = 手(操作文件)

  • bash_tool = 腿(执行命令)

  • task_done = 嘴(报告完成)

🔄 tool_calls + tool_results = 神经系统

大脑发出指令 (tool_calls) → 身体执行 → 感觉反馈 (tool_results) → 大脑感知结果 → 决定下一步。

核心词解释:

  • sequential_thinking: Trae-Agent 的核心思考工具,它让 LLM 能够将复杂任务分解为多个有序的思考步骤,通过结构化的思维过程来提高决策质量和问题解决的准确性。
  • edit_tool、bash_tool、task_done:其他工具,同sequential_thinking一样都是供LLM调用的工具而已。所以我们也可以编写自己的工具给LLM使用,这就是它的可扩展性呗。

从一个简单的问题开始

让trae-agent执行一个最简单的任务:写一个最简单的网页,命令如下

bash 复制代码
trae-cli run "写一个最简单的网页" --provider doubao

结果生成了目标------一个网页,并且将执行轨迹也输出了一个json文件。

html 复制代码
// 生成的网页
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Simple Web Page</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

执行轨迹json虽然内容不多,但是也接近1000行,不帖json内容了,贴一个精简版。Trae-Agent 执行轨迹精简版:

json 复制代码
{
  "task": "写一个最简单的网页",
  "start_time": "2025-07-09T10:48:11",
  "end_time": "2025-07-09T11:05:25",
  "provider": "doubao",
  "model": "doubao-1-5-pro-32k-250115",
  "execution_summary": {
    "total_steps": 6,
    "tools_used": ["sequentialthinking", "str_replace_based_edit_tool", "bash", "task_done"],
    "key_actions": [
      {
        "step": 1,
        "action": "思考规划",
        "tool": "sequentialthinking",
        "thought": "创建HTML文件,编写基础代码"
      },
      {
        "step": 2,
        "action": "创建文件",
        "tool": "str_replace_based_edit_tool",
        "file": "simple_page.html",
        "content": "<!DOCTYPE html>\n<html>\n<head>\n<title>Simple Web Page</title>\n</head>\n<body>\n<h1>Hello, World!</h1>\n</body>\n</html>"
      },
      {
        "step": 3,
        "action": "验证文件",
        "tool": "str_replace_based_edit_tool",
        "command": "view"
      },
      {
        "step": 4,
        "action": "测试网页",
        "tool": "bash",
        "command": "python3 -m http.server 8000 & curl localhost:8000/simple_page.html"
      },
      {
        "step": 5,
        "action": "完成任务",
        "tool": "task_done"
      }
    ]
  },
  "result": {
    "status": "success",
    "output_file": "simple_page.html",
    "verification": "网页可正常访问"
  },
  "performance": {
    "duration_minutes": 17,
    "total_tokens": 15000,
    "llm_calls": 6
  }
}

执行轨迹解析

Trae-Agent 采用"LLM 决策 + 工具执行 + 轨迹记录"的循环架构,通过 LLM 智能选择合适的工具(思考、文件操作、命令执行、任务完成),执行后将结果反馈给 LLM 形成持续的决策循环,直至任务完成。流程图如下: Trae-Agent 通过"用户输入 → Agent 初始化 → LLM 智能决策 → 工具执行 → 结果反馈"的时序流程,实现了从任务理解到逐步执行的完整闭环,其中 LLM 作为决策中枢,根据任务需求依次调用思考、文件操作、命令执行等工具,直至任务完成。时序图如下: 关键点:我们注意到奇数步骤,都是调用sequential_thinking工具,这个工具是Trae-Agent进行ReAction模式的核心工具,它就像一个记事本,每次都记录着当前任务都执行状态,比如当前思考步数,预计总思考数等这些信息。

核心内容展示

核心的执行伪代码。我将实际代码的最核心逻辑总结出来,就是一个循环调用,如下:

python 复制代码
    async def execute_task(self) -> AgentExecution:
        while step_number <= max_steps:
            # 1. LLM 决策
            llm_response = self.llm_client.chat(messages, tools)
            
            # 2. 判断任务完成
            if self.llm_indicates_task_completed(llm_response):
                break
                
            # 3. 工具调用
            if llm_response.tool_calls:
                tool_results = await self.tool_caller.call(tool_calls)
                messages.append(tool_results)
                
            # 4. 轨迹记录
            self.trajectory_recorder.record_agent_step(step)
            
            step_number += 1

核心系统提示词。感兴趣的可以自己翻译一下看看,给大模型一个角色和一些规范,重点是告诉它使用sequential_thinking这个工具,才能进行多轮的ReAct。

vbnet 复制代码
   You are an expert AI software engineering agent.

   All file system operations must use relative paths from the project root directory provided in the user's message. Do not assume you are in a `/repo` or `/workspace` directory. Always use the provided `[Project root path]` as your current working directory.

   Your primary goal is to resolve a given GitHub issue by navigating the provided codebase, identifying the root cause of the bug, implementing a robust fix, and ensuring your changes are safe and well-tested.

   Follow these steps methodically:

   1.  Understand the Problem:
       - Begin by carefully reading the user's problem description to fully grasp the issue.
       - Identify the core components and expected behavior.

   2.  Explore and Locate:
       - Use the available tools to explore the codebase.
       - Locate the most relevant files (source code, tests, examples) related to the bug report.

   3.  Reproduce the Bug (Crucial Step):
       - Before making any changes, you **must** create a script or a test case that reliably reproduces the bug. This will be your baseline for verification.
       - Analyze the output of your reproduction script to confirm your understanding of the bug's manifestation.

   4.  Debug and Diagnose:
       - Inspect the relevant code sections you identified.
       - If necessary, create debugging scripts with print statements or use other methods to trace the execution flow and pinpoint the exact root cause of the bug.

   5.  Develop and Implement a Fix:
       - Once you have identified the root cause, develop a precise and targeted code modification to fix it.
       - Use the provided file editing tools to apply your patch. Aim for minimal, clean changes.

   6.  Verify and Test Rigorously:
       - Verify the Fix: Run your initial reproduction script to confirm that the bug is resolved.
       - Prevent Regressions: Execute the existing test suite for the modified files and related components to ensure your fix has not introduced any new bugs.
       - Write New Tests: Create new, specific test cases (e.g., using `pytest`) that cover the original bug scenario. This is essential to prevent the bug from recurring in the future. Add these tests to the codebase.
       - Consider Edge Cases: Think about and test potential edge cases related to your changes.

   7.  Summarize Your Work:
       - Conclude your trajectory with a clear and concise summary. Explain the nature of the bug, the logic of your fix, and the steps you took to verify its correctness and safety.

   **Guiding Principle:** Act like a senior software engineer. Prioritize correctness, safety, and high-quality, test-driven development.

   # GUIDE FOR HOW TO USE "sequential_thinking" TOOL:
   - Your thinking should be thorough and so it's fine if it's very long. Set total_thoughts to at least 5, but setting it up to 25 is fine as well. You'll need more total thoughts when you are considering multiple possible solutions or root causes for an issue.
   - Use this tool as much as you find necessary to improve the quality of your answers.
   - You can run bash commands (like tests, a reproduction script, or 'grep'/'find' to find relevant context) in between thoughts.
   - The sequential_thinking tool can help you break down complex problems, analyze issues step-by-step, and ensure a thorough approach to problem-solving.
   - Don't hesitate to use it multiple times throughout your thought process to enhance the depth and accuracy of your solutions.

   If you are sure the issue has been solved, you should call the `task_done` to finish the task.

结语

以上是对Trae-Agent的执行流程进行了初步探索,接下来可以考虑许多场景和应用,以充分发挥其能力完成我们的AI应用创意。

相关推荐
山顶夕景6 分钟前
【VLM风控】UNIVID: Unified VLM for Video Moderation
llm·多模态·vlm·风控·视频理解
积流成海2 小时前
Agent演化史(十):2027年它会消失吗?(终章)
python·agent·智能体·大模型应用
掘金安东尼2 小时前
智能体包办工作时,批判性思维怎么办(Addy Osmani 全文转译)
前端·程序员·agent
Summer-Bright3 小时前
深度 | 微调接棒开源:权重免费后,后训练成了云厂商的新收费单元
人工智能·开源·llm·微调·强化训练
大模型真好玩3 小时前
为什么国产大模型进步放缓了?
人工智能·agent·deepseek
ZhengEnCi4 小时前
AI 操作电脑时一直点同一个按钮:不是手不准,是截图卡住了
agent·ai编程·自动化运维
Haooog15 小时前
OpenHands 定时任务同步后还是关着?先查 Git Sync 的启停开关
git·agent·定时任务·配置管理·openhands
大大大大晴天16 小时前
Plan-and-Execute:面向复杂任务的 Agent 规划与任务拆解
agent
掘金安东尼17 小时前
智能体走出聊天框:互联互通的两条路与可核验边界
llm
枫叶丹418 小时前
AI文本水印:生成机制、统计检测与误报
人工智能·机器学习·chatgpt·agent·codex