Skip to content

01 - Agent 核心概念

1. ReAct 模式 (Reasoning + Acting)

ReAct 是目前最主流的 Agent 框架,论文来自 Google Brain

  核心思想: LLM 交替进行 推理(Thought) 和 行动(Action)

  ┌──────────────────────────────────────────────────────────┐
  │                    ReAct 循环                             │
  │                                                          │
  │  用户: "北京今天的天气怎么样?"                              │
  │                                                          │
  │  ┌─ Loop ──────────────────────────────────────────────┐ │
  │  │                                                     │ │
  │  │  Thought: 我需要查询北京的天气。                      │ │
  │  │           我应该使用天气查询工具。                      │ │
  │  │           ▼                                         │ │
  │  │  Action:  get_weather(city="北京")                   │ │
  │  │           ▼                                         │ │
  │  │  Observation: {"temp": 22, "condition": "晴"}       │ │
  │  │           ▼                                         │ │
  │  │  Thought: 我已经拿到了天气数据。                      │ │
  │  │           可以回答用户了。                              │ │
  │  │           ▼                                         │ │
  │  │  Answer:  北京今天22°C,天气晴朗。                    │ │
  │  │                                                     │ │
  │  └─────────────────────────────────────────────────────┘ │
  │                                                          │
  └──────────────────────────────────────────────────────────┘

  ReAct 的每一步:
  ┌───────────────┬──────────────────────────────────────┐
  │  Thought      │ LLM 的推理过程(我需要做什么?)       │
  │  Action       │ 调用一个工具(函数调用)                │
  │  Observation  │ 工具返回的结果                         │
  │  ... 重复 ... │ 直到 LLM 认为可以给出最终答案          │
  │  Answer       │ 最终回答用户                           │
  └───────────────┴──────────────────────────────────────┘

2. Tool Use(工具调用)

Agent 的 "手" 和 "脚" —— 通过工具与外部世界交互

  ┌──────────────────────────────────────────────────────────┐
  │                  Agent 可用的工具类型                      │
  │                                                          │
  │  ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────┐ │
  │  │ 信息检索  │  │ 代码执行  │  │ 文件操作  │  │ 外部API  │ │
  │  │          │  │          │  │          │  │          │ │
  │  │ 搜索引擎  │  │ Bash    │  │ 读/写文件 │  │ HTTP    │ │
  │  │ 数据库    │  │ Python  │  │ 编辑代码  │  │ Slack   │ │
  │  │ 向量检索  │  │ REPL    │  │ Git 操作  │  │ Email   │ │
  │  └──────────┘  └──────────┘  └──────────┘  └──────────┘ │
  │                                                          │
  │  工具的定义 (给 LLM 看的):                                 │
  │  ┌──────────────────────────────────────────────┐        │
  │  │  {                                           │        │
  │  │    "name": "get_weather",                    │        │
  │  │    "description": "获取指定城市的天气",         │        │
  │  │    "parameters": {                           │        │
  │  │      "city": {                               │        │
  │  │        "type": "string",                     │        │
  │  │        "description": "城市名称"              │        │
  │  │      }                                       │        │
  │  │    }                                         │        │
  │  │  }                                           │        │
  │  └──────────────────────────────────────────────┘        │
  │                                                          │
  │  LLM 并不直接执行工具!                                     │
  │  LLM 只是输出 "我要调用什么工具、传什么参数"                  │
  │  Agent 框架负责真正执行工具,并把结果喂回给 LLM              │
  │                                                          │
  └──────────────────────────────────────────────────────────┘

3. Agent Loop(Agent 主循环)

完整的 Agent 运行流程:

  ┌─────────────────────────────────────────────────────────────┐
  │                                                             │
  │  输入: 用户请求 + 工具定义 + System Prompt                    │
  │     │                                                       │
  │     ▼                                                       │
  │  ┌──────────────────────────────────────────┐               │
  │  │         构建 Prompt                       │               │
  │  │                                          │               │
  │  │  System: "你是一个助手,可以使用以下工具..." │               │
  │  │  Tools:  [get_weather, search, ...]       │               │
  │  │  History: [之前的对话历史]                  │               │
  │  │  User:   "北京天气怎么样?"                │               │
  │  └────────────────────┬─────────────────────┘               │
  │                       │                                     │
  │                       ▼                                     │
  │  ┌────────────────────────────────────┐                     │
  │  │           调用 LLM API             │ ◀──────────┐        │
  │  └────────────────┬───────────────────┘            │        │
  │                   │                                │        │
  │            ┌──────┴──────┐                         │        │
  │            │ 响应类型?    │                         │        │
  │            └──────┬──────┘                         │        │
  │                   │                                │        │
  │          ┌────────┼────────┐                       │        │
  │          │                 │                       │        │
  │     文本回答           工具调用                      │        │
  │          │                 │                       │        │
  │          ▼                 ▼                       │        │
  │    ┌──────────┐    ┌──────────────┐               │        │
  │    │ 返回给    │    │ 执行工具      │               │        │
  │    │ 用户     │    │ get_weather() │               │        │
  │    └──────────┘    └──────┬───────┘               │        │
  │     (结束循环)             │                       │        │
  │                           ▼                       │        │
  │                    ┌──────────────┐               │        │
  │                    │ 工具结果加入   │               │        │
  │                    │ 对话历史       │───────────────┘        │
  │                    └──────────────┘                         │
  │                     (继续循环)                               │
  │                                                             │
  └─────────────────────────────────────────────────────────────┘

  伪代码:
  ┌──────────────────────────────────────┐
  │  messages = [system_prompt, user_msg]│
  │                                      │
  │  while true:                         │
  │    response = llm.chat(messages)     │
  │                                      │
  │    if response.has_tool_call:        │
  │      result = execute(tool_call)     │
  │      messages.append(tool_result)    │
  │      continue   ← 继续循环           │
  │                                      │
  │    else:                             │
  │      return response.text ← 返回     │
  └──────────────────────────────────────┘

4. Agent 架构模式对比

┌──────────────────────────────────────────────────────────────┐
│                  主流 Agent 架构模式                           │
├──────────────┬──────────────┬──────────────┬────────────────┤
│  单步 Agent   │  ReAct Agent │  Plan+Exec   │  Multi-Agent  │
├──────────────┼──────────────┼──────────────┼────────────────┤
│              │              │              │                │
│  User→LLM   │  Thought     │  Plan:       │  Orchestrator  │
│  →Tool      │  →Action     │  1. xxx      │  ┌────┐┌────┐  │
│  →Response  │  →Observe    │  2. xxx      │  │AgA ││AgB │  │
│              │  →Thought   │  3. xxx      │  └────┘└────┘  │
│  只调用一次  │  →...        │  Exec:       │  ┌────┐       │
│  工具        │  循环多步     │  逐步执行Plan │  │AgC │       │
│              │              │              │  └────┘       │
│  简单任务 ✓  │  大多数场景 ✓ │  复杂任务 ✓  │  超复杂任务 ✓  │
└──────────────┴──────────────┴──────────────┴────────────────┘

  Claude Code 用的是哪种?
  → ReAct + 一些 Plan 特性
  → 它会在 "think" 中推理,然后调用工具(Bash/Read/Edit等)
  → 观察结果后继续推理,直到任务完成

  Agent 的关键设计决策:
  ┌────────────────────────────────────────────────────┐
  │  1. 给 LLM 什么工具? (能力边界)                    │
  │  2. System Prompt 怎么写? (行为约束)               │
  │  3. 循环什么时候停止? (停止条件)                    │
  │  4. 上下文怎么管理? (历史太长怎么办)                │
  │  5. 出错了怎么恢复? (错误处理策略)                  │
  └────────────────────────────────────────────────────┘

5. 2025 年主流 Agent 框架

┌──────────────────────────────────────────────────────────────┐
│              Agent 框架生态(2025年)                         │
├────────────────┬─────────────────────────────────────────────┤
│  框架           │  定位与特点                                  │
├────────────────┼─────────────────────────────────────────────┤
│ OpenAI         │  Python-first,轻量级                        │
│ Agents SDK     │  原语: Agent + Handoff + Guardrail           │
│ (2025.3)       │  内置 MCP 工具集成、实时语音 Agent            │
├────────────────┼─────────────────────────────────────────────┤
│ Google ADK     │  企业级,多语言(Python/TS/Go/Java 均有 1.0)  │
│ (2025)         │  内置 Vertex AI 部署,支持图结构工作流         │
├────────────────┼─────────────────────────────────────────────┤
│ LangGraph      │  有状态图结构 Agent 编排                      │
│                │  适合: 复杂多步骤、需要回溯的 Agent 流程       │
│                │  LangChain 官方重心已转移至此                 │
├────────────────┼─────────────────────────────────────────────┤
│ CrewAI         │  多角色协作 Agent                             │
│                │  月均 4.5 亿次 workflow,Fortune 500 广泛使用  │
├────────────────┼─────────────────────────────────────────────┤
│ AutoGen v0.4   │  微软出品,图工作流 + Swarm 协调模式           │
│                │  AgentChat 高层 API,研究+生产兼顾             │
└────────────────┴─────────────────────────────────────────────┘

  选型建议:
  • 快速原型 / OpenAI 生态    → OpenAI Agents SDK
  • 需要上 GCP / 企业级部署   → Google ADK
  • 复杂有状态流程            → LangGraph
  • 多角色协作任务            → CrewAI
  • 自己从零理解原理(本教程)  → 手写 ReAct Loop(模块四)

下一节: 02 - Go 实现 Agent — 从零写一个可调用工具的 Agent