扩展-Trae Agent 深度技术分析
字节跳动开源的 LLM 驱动软件工程 AI Agent
目录
- [1. 项目概述](#1. 项目概述)
- [2. 架构设计](#2. 架构设计)
- [3. 核心组件](#3. 核心组件)
- [4. 实现原理](#4. 实现原理)
- [5. 工具系统](#5. 工具系统)
- [6. 使用示例](#6. 使用示例)
- [7. 执行轨迹格式](#7. 执行轨迹格式)
- [8. 与竞品对比](#8. 与竞品对比)
- [9. 依赖与环境](#9. 依赖与环境)
- [10. 适用场景与局限](#10. 适用场景与局限)
1. 项目概述
Trae Agent 是字节跳动开源的 LLM 驱动通用软件工程 Agent,专为自动解决复杂编程任务而设计,包括 GitHub issue 修复、代码重构、单元测试生成等。
核心定位
与 Cursor、Claude Code 等闭源 AI 编码助手不同,Trae Agent 强调研究友好的透明化架构:
- 完全开源,易于修改和扩展
- 模块化设计,便于进行消融研究(Ablation Study)
- 详细的执行轨迹记录,支持学术研究
- 支持 7+ 主流 LLM 提供商
解决的问题
| 问题类别 | 具体描述 |
|---|---|
| 代码理解与导航 | 在大型陌生代码库中快速定位相关文件和函数 |
| 问题重现 | 自动创建复现脚本验证 bug 存在性 |
| 根因分析 | 通过代码分析和调试找出 bug 根本原因 |
| 修复实现 | 精准、最小化的代码修改解决问题 |
| 验证与测试 | 执行完整测试套件,防止回归 |
| 执行可视化 | 完整的轨迹记录便于理解 Agent 决策过程 |
2. 架构设计
整体架构
┌─────────────────────────────────────────────────────────┐
│ CLI Interface │
│ (trae_agent/cli.py) │
│ 命令行参数解析 / 配置文件处理(YAML/JSON) / Docker支持 │
└──────────────────────────┬──────────────────────────────┘
│
┌──────────────────────────▼──────────────────────────────┐
│ Config Layer │
│ (trae_agent/utils/config.py) │
│ ModelProvider / ModelConfig / TraeAgentConfig │
└──────────────────────────┬──────────────────────────────┘
│
┌──────────────────────────▼──────────────────────────────┐
│ Agent Core (BaseAgent) │
│ (trae_agent/agent/base_agent.py) │
│ 消息管理 / LLM调用编排 / 工具调用管理 / 步骤追踪 │
└──────────┬───────────────┬──────────────────────────────┘
│ │ │
┌──────▼──────┐ ┌──────▼──────┐ ┌────────▼────────┐
│ Tool Caller │ │ LLM Client │ │ Trajectory │
│ │ │ (多厂商适配) │ │ Recorder │
└──────┬──────┘ └──────┬──────┘ └─────────────────┘
│ │
┌──────┴───────┐ ┌────┴───────────────────────────┐
│ 工具集合 │ │ Providers │
│ - bash │ │ OpenAI / Anthropic / Google │
│ - edit │ │ Azure / Ollama / Doubao │
│ - json_edit │ │ OpenRouter │
│ - thinking │ └────────────────────────────────┘
│ - MCP tools │
└──────────────┘
目录结构
trae-agent/
├── trae_agent/
│ ├── agent/
│ │ ├── base_agent.py # Agent 核心执行循环
│ │ └── trae_agent.py # SWE 专用 Agent 实现
│ ├── tools/
│ │ ├── bash_tool.py # Shell 命令执行
│ │ ├── edit_tool.py # 文件编辑工具
│ │ ├── json_edit_tool.py # JSON 精确编辑
│ │ ├── sequential_thinking_tool.py # 结构化思考
│ │ ├── task_done_tool.py # 完成标记
│ │ └── mcp_tool.py # MCP 工具集成
│ ├── utils/
│ │ ├── config.py # 配置管理
│ │ ├── llm_client.py # LLM 多厂商适配
│ │ └── trajectory_recorder.py # 执行轨迹记录
│ └── cli.py # CLI 入口
├── docs/
│ └── roadmap.md
├── pyproject.toml
└── trae_config.yaml # 示例配置
3. 核心组件
3.1 BaseAgent --- 执行引擎
Agent 核心类,实现完整的执行状态机:
状态机设计:
Agent 生命周期:
INITIALIZING → RUNNING → COMPLETED
↓
ERROR
单步状态:
THINKING → CALLING_TOOL → REFLECTING → COMPLETED
↓
ERROR
关键方法:
| 方法 | 职责 |
|---|---|
execute_task() |
主执行循环,管理 Agent 状态机 |
_run_llm_step() |
单个 LLM 推理步骤 |
_tool_call_handler() |
工具调用和结果处理 |
_finalize_step() |
步骤完成和记录 |
3.2 TraeAgent --- SWE 专用实现
继承 BaseAgent,针对软件工程任务增强:
- MCP 服务发现与动态初始化
- Git diff 生成和补丁管理(
--must-patch模式) - 定制化系统提示词(TRAE_AGENT_SYSTEM_PROMPT)
- 测试目录过滤(防止误修改测试文件)
3.3 LLMClient --- 多厂商适配器
采用工厂模式,统一接口对接 7 个 LLM 提供商:
python
class LLMProvider(Enum):
OPENAI = "openai"
ANTHROPIC = "anthropic"
AZURE = "azure"
OLLAMA = "ollama"
OPENROUTER = "openrouter"
DOUBAO = "doubao" # 字节跳动豆包
GOOGLE = "google" # Google Gemini
配置优先级:CLI 参数 > 配置文件 > 环境变量 > 默认值
3.4 TrajectoryRecorder --- 执行轨迹记录
完整记录 Agent 每一步的执行细节:
- LLM 请求 / 响应对(含 token 用量)
- Agent 步骤和状态转换
- 工具调用和执行结果
- 完整元数据(时间戳、模型配置、任务描述)
4. 实现原理
4.1 Agent 主执行循环
python
async def execute_task(self) -> AgentExecution:
execution = AgentExecution(task=self._task, steps=[])
while step_number <= self._max_steps:
step = AgentStep(step_number=step_number,
state=AgentStepState.THINKING)
# 1. LLM 推理
messages = await self._run_llm_step(step, messages, execution)
# 2. 记录步骤
await self._finalize_step(step, messages, execution)
# 3. 检查完成
if execution.agent_state == AgentState.COMPLETED:
break
step_number += 1
await self._close_tools()
await self.cleanup_mcp_clients()
return execution
4.2 LLM 推理步骤
python
async def _run_llm_step(self, step, messages, execution):
# 调用 LLM
llm_response = self._llm_client.chat(
messages,
self._model_config,
self._tools
)
# 检查任务完成标记
if self.llm_indicates_task_completed(llm_response):
if self._is_task_completed(llm_response):
execution.success = True
return messages
# 处理工具调用
if llm_response.tool_calls:
return await self._tool_call_handler(
llm_response.tool_calls,
step
)
4.3 工具执行策略
支持并行和顺序两种调用模式:
python
if self._model_config.parallel_tool_calls:
tool_results = await self._tool_caller.parallel_tool_call(tool_calls)
else:
tool_results = await self._tool_caller.sequential_tool_call(tool_calls)
# 将结果追加到消息历史
for tool_result in tool_results:
messages.append(LLMMessage(role="user", tool_result=tool_result))
# 可选:生成 Agent 反思
reflection = self.reflect_on_result(tool_results)
if reflection:
messages.append(LLMMessage(role="assistant", content=reflection))
4.4 MCP 工具动态发现
python
async def initialise_mcp(self):
await self.discover_mcp_tools()
if self.mcp_tools:
self._tools.extend(self.mcp_tools) # 动态扩展工具集
async def discover_mcp_tools(self):
for name, config in self.mcp_servers_config.items():
mcp_client = MCPClient()
await mcp_client.connect_and_discover(
name, config, self.mcp_tools,
self._llm_client.provider.value
)
5. 工具系统
内置工具
| 工具名称 | 功能 | 关键特性 |
|---|---|---|
bash |
Shell 命令执行 | 持久化会话、120s 超时、精确错误码捕获 |
str_replace_based_edit_tool |
文件编辑 | view/create/str_replace/insert 操作 |
json_edit_tool |
JSON 精确编辑 | JSONPath 表达式定位修改 |
sequential_thinking |
结构化思考 | 支持分支、修正、5-25 步深度思考 |
task_done |
完成标记 | 触发 Agent 停止循环 |
Bash 工具的会话管理
python
class _BashSession:
_timeout: float = 120.0 # 默认超时秒数
_sentinel: str = ",,,,bash-command-exit-__ERROR_CODE__-banner,,,,"
async def run(self, command: str) -> ToolExecResult:
# 发送命令 → Sentinel 等待完成 → 提取输出和错误码
return ToolExecResult(output=output, error=error, error_code=error_code)
状态在命令间持久保持,支持 Unix 和 Windows。
文件编辑工具操作
view - 显示文件内容或目录结构(支持行范围)
create - 创建新文件(文件已存在则报错)
str_replace - 精确字符串替换(必须唯一匹配,包括空白符)
insert - 在指定行后插入文本
JSON 编辑工具示例
python
# JSONPath 表达式示例
$.users[0].name # 第一个用户名
$.config.database.host # 嵌套属性
$.items[*].price # 所有项目价格
$..key # 递归搜索
顺序思考工具参数
python
{
"thought": "当前思考内容",
"thought_number": 3,
"total_thoughts": 10,
"next_thought_needed": True,
"is_revision": False, # 是否修正前一个思考
"branch_id": "branch_a" # 多分支探索标识
}
6. 使用示例
安装
bash
pip install trae-agent
# 或从源码安装
git clone https://github.com/bytedance/trae-agent
cd trae-agent && pip install -e .
环境变量配置
bash
export ANTHROPIC_API_KEY="your-key"
export OPENAI_API_KEY="your-key"
export GOOGLE_API_KEY="your-key"
基础命令
bash
# 执行任务
trae-cli run "Create a hello world Python script"
# 查看当前配置
trae-cli show-config
# 交互模式(REPL)
trae-cli interactive
指定 LLM 提供商
bash
# OpenAI GPT-4o
trae-cli run "Fix the bug in main.py" \
--provider openai \
--model gpt-4o
# Anthropic Claude
trae-cli run "Add unit tests for utils.py" \
--provider anthropic \
--model claude-sonnet-4-20250514
# Google Gemini
trae-cli run "Optimize the sorting algorithm" \
--provider google \
--model gemini-2.5-flash
# 本地 Ollama 模型
trae-cli run "Add comments to this file" \
--provider ollama \
--model qwen3
# OpenRouter(统一接入多 provider)
trae-cli run "Review this code" \
--provider openrouter \
--model "anthropic/claude-3-5-sonnet"
高级选项
bash
# 指定工作目录
trae-cli run "Add tests for auth module" \
--working-dir /path/to/project
# 保存执行轨迹
trae-cli run "Debug authentication flow" \
--trajectory-file debug_session.json
# 强制生成代码补丁(SWE 模式)
trae-cli run "Fix login bug from issue #42" \
--must-patch
# Docker 隔离执行(指定镜像)
trae-cli run "Run tests in isolated env" \
--docker-image python:3.12 \
--working-dir ./workspace
# Docker(使用 Dockerfile 构建)
trae-cli run "Debug authentication" \
--dockerfile-path ./workspace/Dockerfile
# Docker(附加到已有容器)
trae-cli run "Update API endpoints" \
--docker-container-id 91998a56056c
配置文件(trae_config.yaml)
yaml
agents:
trae_agent:
enable_lakeview: true
model: trae_agent_model
max_steps: 200
tools:
- bash
- str_replace_based_edit_tool
- sequentialthinking
- task_done
# MCP 服务配置(可选)
allow_mcp_servers:
- playwright
mcp_servers:
playwright:
command: npx
args:
- "@playwright/mcp@0.0.27"
model_providers:
anthropic:
api_key: ${ANTHROPIC_API_KEY}
provider: anthropic
openai:
api_key: ${OPENAI_API_KEY}
provider: openai
models:
trae_agent_model:
model_provider: anthropic
model: claude-sonnet-4-20250514
max_tokens: 4096
temperature: 0.5
top_p: 1.0
max_retries: 10
parallel_tool_calls: true
7. 执行轨迹格式
每次运行生成一个 JSON 文件,包含完整执行记录:
json
{
"task": "Fix the authentication bug",
"start_time": "2025-06-12T22:05:46.433797",
"end_time": "2025-06-12T22:06:15.123456",
"provider": "anthropic",
"model": "claude-sonnet-4-20250514",
"max_steps": 20,
"llm_interactions": [
{
"timestamp": "2025-06-12T22:05:47.000000",
"input_messages": [...],
"response": {
"content": "I'll analyze the authentication code...",
"finish_reason": "end_turn",
"usage": {
"input_tokens": 150,
"output_tokens": 75,
"cache_read_input_tokens": 0
},
"tool_calls": [...]
}
}
],
"agent_steps": [
{
"step_number": 1,
"state": "thinking",
"tool_calls": [...],
"tool_results": [...],
"reflection": null,
"error": null
}
],
"success": true,
"final_result": "Fixed JWT token expiry validation in auth.py",
"execution_time": 28.69
}
轨迹文件可用于:
- 分析 Agent 决策过程
- 统计 Token 使用量和成本
- 进行消融研究和对比实验
- 接入 MLOps 平台(Wandb / MLFlow)
8. 与竞品对比
| 特性 | Trae Agent | Claude Code | Cursor |
|---|---|---|---|
| 开源 | ✓ MIT | ✓ 开源 | ✗ 闭源 |
| 研究友好 | ✓ 专为研究设计 | ✓ 中等 | ✗ 生产优先 |
| 完整执行轨迹 | ✓ JSON 详细记录 | 有限 | ✗ |
| 多 LLM 支持 | ✓ 7+ 提供商 | 主要 Anthropic | ✓ 多个 |
| Docker 隔离 | ✓ 完整支持 | ✗ | ✗ |
| MCP 集成 | ✓ 完整支持 | ✓ 部分 | ✗ |
| 消融研究 | ✓ 易于修改组件 | ✗ | ✗ |
| 交互模式 | ✓ REPL | ✓ | ✓ |
| 商业成熟度 | 研究阶段 | 生产可用 | 生产优先 |
Trae Agent 最大差异点:是目前唯一专门为 AI Agent 学术研究设计的开源框架,完整的执行轨迹记录和高度模块化使其成为研究工具调用、Agent 决策、多步推理的理想平台。
9. 依赖与环境
toml
# 核心依赖(pyproject.toml)
python = ">=3.11"
anthropic = ">=0.54.0,<=0.60.0"
openai = ">=1.86.0"
google-genai = ">=1.24.0"
ollama = ">=0.5.1"
mcp = "==1.12.2" # Model Context Protocol
pydantic = ">=2.0.0"
pyyaml = ">=6.0.2"
tree-sitter = "==0.21.3" # 代码解析
tree-sitter-languages = "==1.10.2" # 多语言支持
textual = ">=0.50.0" # TUI 界面
pyinstaller = "==6.15.0" # Docker 工具打包
10. 适用场景与局限
最适合的场景
- GitHub issue 自动化修复(SWE-bench 类任务)
- 代码审查与重构
- 单元测试自动生成
- 文档批量更新
- Bug 根因分析
不太适合的场景
- 实时交互式编码(推荐 Cursor / Claude Code)
- GUI 应用开发(缺乏 UI 预览)
- 交互式调试会话(缺乏实时反馈)
未来路线图
| 阶段 | 功能 |
|---|---|
| SDK 开发 | 无头接口、流式轨迹记录 |
| 沙箱环境 | 隔离任务执行、并行任务支持 |
| 轨迹分析 | MLOps 集成(Wandb / MLFlow) |
| 工具扩展 | Jupyter 支持、MCP 标准化 |
| 多 Agent 编排 | Agent 协作、工作流模式 |
*分析基于 trae-agent v0.1.0