# Agents

> 驅動 flow 的專業 agents，以及確保其穩定運作的防護機制。

[Flow](https://pentagi.com/zh-tw/docs/glossary/#flow) 由一組 [agents](https://pentagi.com/zh-tw/docs/glossary/#agent) 團隊執行。每種 [agent type](https://pentagi.com/zh-tw/docs/glossary/#agent-type) 都有其專屬的提示詞、模型設定與 [tools](https://pentagi.com/zh-tw/docs/glossary/#tool)，因此您可以針對不同角色指派更強大或更經濟實惠的模型。

## [Agent types](https://pentagi.com/zh-tw/docs/concepts/agents/#agent-types)

| Agent                  | 角色                                                                                              |
| ---------------------- | ----------------------------------------------------------------------------------------------- |
| `primary_agent`        | 驅動 flow 的主要推理 agent                                                                             |
| `assistant`            | 互動式 [assistant](https://pentagi.com/zh-tw/docs/glossary/#assistant) 模式                                                   |
| `generator`            | 將每個 [task](https://pentagi.com/zh-tw/docs/glossary/#task) 拆分為 [subtasks](https://pentagi.com/zh-tw/docs/glossary/#subtask)                        |
| `refiner`              | 隨著結果回傳持續優化計畫                                                                                    |
| `adviser`              | 提供專家建議；在執行監控中擔任導師，並在 task 規劃中擔任規劃者                                                              |
| `reflector`            | 修正未產生 [tool calls](https://pentagi.com/zh-tw/docs/glossary/#tool-call) 的 agents 行為                                       |
| `searcher`             | 資訊蒐集與網路搜尋                                                                                       |
| `enricher`             | 在 adviser 回答前，從 [memory](https://pentagi.com/zh-tw/docs/glossary/#memory) 和 [sandbox](https://pentagi.com/zh-tw/docs/glossary/#sandbox) 收集額外上下文資訊 |
| `coder`                | 撰寫與分析程式碼                                                                                        |
| `installer`            | 在 sandbox 中安裝並設定 tools                                                                          |
| `pentester`            | [Pentest](https://pentagi.com/zh-tw/docs/glossary/#pentest) 與安全評估                                                        |
| `simple`、`simple_json` | 純文字與 JSON 結構化補全                                                                                 |

另有兩個 agents 在幕後運作：`memorist` 會從長期 memory 中回答其他 agents 關於過往工作的問題，而 `reporter` 則負責撰寫每個 task 的結果報告。

## [工作分派方式](https://pentagi.com/zh-tw/docs/concepts/agents/#how-work-is-delegated)

```mermaid
sequenceDiagram
  accTitle: How a task is split into subtasks and the primary agent delegates each step to specialist agents through shared memory
  participant G as Generator
  participant O as Primary agent
  participant M as Memorist
  participant S as Specialists
  participant V as Vector store
  G->>O: Subtasks of the task
  O->>M: Past work, when the context lacks it
  M->>V: Search memory
  O->>S: Delegate a step to the searcher, pentester, coder or installer
  S->>V: Store tool results
  S-->>O: Result
```

## [防護機制](https://pentagi.com/zh-tw/docs/concepts/agents/#safeguards)

* **Tool call 限制**（始終啟用）— `MAX_GENERAL_AGENT_TOOL_CALLS`（預設值 100）會限制 `primary_agent`、`assistant`、`pentester`、`coder` 和 `installer` 鏈中的模型輪次，而 `MAX_LIMITED_AGENT_TOOL_CALLS`（預設值 20）則限制其他 agent 的輪次。一輪可以呼叫多個 tool。數值 1 至 5 會提升至 6，0 或以下則表示使用預設值。
* **重複偵測**（始終啟用）— 從第三次連續以相同引數呼叫同一個 tool 開始，該 tool 將不會再次執行：agent 會收到先前的答案，並被要求改變策略。第七次這樣的呼叫將終止該鏈。
* **Reflector**（預設啟用）— 當模型首次僅回應文字而未發出 tool call 時介入，引導 agent 重新使用 tool；若連續三輪仍未成功，則終止該鏈結。
* **執行監控**（測試版，`EXECUTION_MONITOR_ENABLED`）— 針對 `primary_agent`、`pentester`、`coder`、`installer` 以及與 agent 協作時的 `assistant`，當 agent 連續 `EXECUTION_MONITOR_SAME_TOOL_LIMIT` 次呼叫同一個 tool（預設 5）或自上次建議以來已發出 `EXECUTION_MONITOR_TOTAL_TOOL_LIMIT` 次 tool calls（預設 10）時，adviser 會擔任導師角色。此觸發機制統計的是呼叫次數，而非偵測停滯；隨後 adviser 會檢視 agent 的進度，並建議是否繼續、改變方法或停止。
* **智慧 task 規劃**（Beta 版，`AGENT_PLANNING_STEP_ENABLED`）— 在專家開始執行 subtask 前，adviser 會先擬定 3 至 7 個具體步驟。

> [!NOTE]
> 對於參數量低於 32B 的模型，啟用執行監控與 task 規劃大約可使結果品質提升一倍，但代價是 Token 消耗量與耗時增加 2 至 3 倍。這些功能預設為關閉狀態。

在 Enterprise 中，planner 始終執行，且執行監控預設為開啟，限制分別為 20 和 40 — 請參閱 [Agent profiles](https://pentagi.com/zh-tw/docs/enterprise/agent-profiles/#planner-and-execution-monitor)。