Context Providers:拯救 Agent 上下文污染的遺失層 (Context Providers: The Missing Layer)

原始來源與檔名:20260512_2026-04-28T092611+0800-Context providers the missing layer between agents and tools.md

來源:[[@ashpreetbedi]] / X (Twitter) — 2026-04-28 原始檔名:2026-04-28T092611+0800-Context providers the missing layer between agents and tools.md


NAPKIN | 餐巾紙

餐巾紙公式

The Problem: Agent <- sees 50 tools (Slack, Drive, Notion) -> Context pollution & scope collision. The Solution: Agent <-> ContextProvider (Sub-agent) <-> Source Tools Main Agent only sees: query_slack(question) and update_slack(instruction).

當你的 Agent 擁有多個系統(Slack, Google Drive, CRM)的工具權限時,它會撞牆:上下文被工具描述塞滿、工具名稱衝突(如多個 search 工具),且主 Prompt 必須記住每個 API 的怪癖。作者提出了一個架構模式:「Context Providers」。在主 Agent 與工具之間插入一層子代理(Sub-agent)。主 Agent 永遠只看到兩個自然語言工具:query_sourceupdate_source。底層的複雜 API 邏輯由獨立的子代理處理。這大幅壓縮了主 Agent 的負擔並解決了衝突。

一句話

這是一篇解決 Agent 工具過載與幻覺的關鍵架構文。現代 Agent 架構常常把幾十個 API 工具直接丟給主模型,導致嚴重的上下文污染與作用域衝突(例如 Slack 的 search 和 Drive 的 search 搞混)。解法是引入「Context Providers」封裝層:用一個專門的子代理接管 Slack 的所有底層工具,並只向主代理暴露兩個極簡介面:自然語言查詢(讀)與自然語言指令(寫)。這種「去中心化工具路由」讓主代理的智商瞬間回血。

餐巾紙草圖

[ Traditional Flawed Architecture ]
Main Agent System Prompt:
- Here is how to use Slack's 12 tools.
- Here is how to use Drive's 8 tools.
- Here is how to use CRM's 10 tools.
Result: 80% of context is API docs. Hallucinations happen.

[ Context Provider Architecture ]
Main Agent System Prompt:
- Tools available: `query_slack`, `update_slack`, `query_drive`.
     |
     v (calls `query_slack("What did John say?")`)
Slack Sub-agent (Has the 12 tools, does the lookup logic)
     |
     v (returns plain text summary)
Main Agent synthesizes the final answer.

ROUND 1: SKELETON | 骨架掃描

“這篇文章在說什麼”

ROUND 2: DISSECTION | 血肉解剖

“憑什麼這麼說”

核心論證鏈

  1. 封裝與抽象 (Encapsulation and Abstraction): 軟體工程的黃金法則在 Prompt Engineering 同樣適用。微服務架構之所以能解決單體應用的泥潭,就是因為隱藏了底層實作細節。ContextProvider 就是 Agent 的「微服務閘道器 (API Gateway)」。
  2. 自然語言即通用協定 (NL as Universal Protocol): 傳統工具依賴 JSON Schema 進行強型別綁定。但 LLM 最擅長的是自然語言。透過把 search_channel(id="C123", query="bug") 轉換成 query_slack("Find discussions about the bug in the engineering channel"),將「找 ID、組 JSON」的髒活下放給專門的子代理,極大降低了主代理的認知負擔。
  3. 讀寫分離的安全性 (CQRS): 將 query (讀) 與 update (寫) 分開成為兩個工具,從根本上防止了模型在「只是想搜尋資訊」時,意外呼叫破壞性 API 的危險。

關鍵證據

邊界條件

ROUND 3: SOUL | 靈魂提取

“還能怎么用”