掘金 人工智能 07月10日 14:10
Trae-Agent 核心执行思路深度解析
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了Trae Agent,一个利用大语言模型(LLM)和各种工具执行复杂软件工程任务的开源项目。文章通过分析Trae Agent的架构,将其比作具备大脑、笔记本和身体的“人”,LLM负责决策,sequential_thinking工具用于记录思考过程,其他工具则负责实际操作。文章还通过一个简单的网页创建任务,展示了Trae Agent的执行流程和核心代码逻辑。最后,文章强调了Trae Agent采用的“LLM决策+工具执行+轨迹记录”的循环架构,以及其在软件工程领域的应用潜力。

🧠 Trae Agent的核心架构基于“LLM决策+工具执行+轨迹记录”的循环模式。LLM作为“大脑”,负责分析、决策和推理,并根据当前信息决定下一步行动。

📝 sequential_thinking是Trae Agent的核心工具,类似于“笔记本”,用于记录LLM的思考过程,跟踪任务进度,并强制LLM按结构化方式思考。该工具使得Agent能够将复杂任务分解为多个有序的思考步骤。

🛠️ Trae Agent通过调用不同的工具来完成任务,例如edit_tool(编辑文件)、bash_tool(执行命令)和task_done(报告完成)。工具的调用和结果反馈构成了Agent的神经系统,从而实现闭环的决策和执行流程。

⚙️ Trae Agent的关键在于其核心执行循环,包括LLM决策、任务完成判断、工具调用和轨迹记录。系统提示词指导LLM使用sequential_thinking工具,进行多轮的ReAct模式,从而实现复杂任务的逐步解决。

引言

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

总结

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

🧠 LLM = 大脑

📝 sequential_thinking = 笔记本

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

🔄 tool_calls + tool_results = 神经系统

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

核心词解释

从一个简单的问题开始

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

trae-cli run "写一个最简单的网页" --provider doubao

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

// 生成的网页<!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 执行轨迹精简版:

{  "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模式的核心工具,它就像一个记事本,每次都记录着当前任务都执行状态,比如当前思考步数,预计总思考数等这些信息。

核心内容展示

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

    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。

   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应用创意。

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Trae Agent LLM 软件工程 ReAct AI Agent
相关文章