框架

微軟 Agent Framework 實戰:從單 Agent 到多 Agent 工作流

微軟官方推出的 Agent 框架,雙周斬獲 13k Stars,支援 Python 與 .NET,內建四種編排模式。本文以最少程式碼搭配架構圖,說明它如何讓 Agent 從原型走向正式部署。

字級

上週微軟把 Agent Framework (MAF) 推上了 GitHub Trending,13,356 Stars,定位 production-grade。我把 samples 目錄跑了大半,結論先講在前頭:這是目前最像「企業級」的 Agent 框架,但代價是抽象層偏厚、小專案用起來綁手綁腳。

它解決什麼問題

LangChain 太自由,AutoGen 太玩具,LangGraph 學習曲線陡——企業要把 Agent 跑進正式環境,需要的是 provider 無關、可觀測、支援 human-in-the-loop 的框架。微軟 agent-framework 直接對標這個缺口。

展開講三點:

  • 可觀測:Workflow 節點每次狀態變化都廣播事件,自帶 OpenTelemetry 整合
  • 可恢復:內建 checkpointing,workflow 中途當機可從最近一次快照重啟
  • 可中斷:human-in-the-loop 是 first-class 概念,不是事後補上的裝飾

LangChain 時代,這三件事都得自己拼湊。MAF 把它們做進了 Runtime 層。

怎麼用

pip install agent-framework,30 行寫一個帶工具呼叫的 Agent:

from agent_framework.azure import AzureAIAgent
from agent_framework import tool

@tool
def get_weather(city: str) -> str:
    return f"{city}:晴,25°C"

agent = AzureAIAgent(
    name="weather-bot",
    instructions="你是一個天氣助手,使用工具回答問題。",
    tools=[get_weather],
)

async def main():
    response = await agent.run("北京今天天氣怎麼樣?")
    print(response.text)

import asyncio
asyncio.run(main())

再 50 行串成 sequential workflow,兩個 Agent 接力處理訂單:

from agent_framework import WorkflowBuilder, executor

@executor
async def intake(msg: str, ctx):
    await ctx.send_message(msg, target_id="validator")

@executor
async def validator(msg: str, ctx):
    if "fraud" in msg.lower():
        await ctx.yield_output("blocked")
    else:
        await ctx.send_message(msg, target_id="fulfill")

@executor
async def fulfill(msg: str, ctx):
    await ctx.yield_output(f"order {msg} shipped")

wf = (
    WorkflowBuilder()
    .set_start_executor(intake)
    .add_edge(intake, validator)
    .add_edge(validator, fulfill)
    .build()
)

async def main():
    async for ev in wf.run_stream("order-12345"):
        print(ev)

注意 add_edge 這種圖結構 API——它就是 LangGraph 風格的 DAG,只是換了名字。

它怎麼運作

拆三層:

  • Agent:單兵,封裝一個 LLM client + 工具集 + system prompt
  • Workflow:編排層,圖結構,定義 Agent 之間的資料流向
  • Runtime:執行環境,負責 checkpoint、streaming、HITL、time-travel

圖結構編排的內建四種拓樸:

  1. Sequential:A → B → C,流水線
  2. Concurrent:廣播給多個 Agent,合并結果
  3. Handoff:根據上下文把任務「交接」給另一個 Agent(類似 AutoGen 的群聊)
  4. Group Chat:多 Agent 圓桌討論,有 moderator

部署到 Azure AI Foundry 只需要額外 2 行——把 AzureAIAgent 換成 FoundryAgent,環境變數配上 foundry endpoint 就行。其他程式碼不必動。

哪裡好,哪裡不好

優點:

  • 雙棧 API 一致,Python 與 .NET 命名規範幾乎對等
  • 企業級特性齊備:checkpointing、streaming、human-in-the-loop、time-travel 都是 first-class
  • 微軟生態系整合深厚:Foundry、Azure OpenAI、GitHub Copilot SDK 原生支援

缺點:

  • Python 套件體積偏大,光 import agent_framework 就拖進一堆依賴
  • 文件對 OpenAI 相容層以外的 provider(例如 DeepSeek、Qwen)著墨甚少
  • Go SDK 拆出去是雙面刃:獨立 repo 維護節奏與主倉不同步

橫向比較:

框架編排模型學習曲線正式部署特性
MAF圖 + 四種拓樸中強
LangGraph自訂 DAG陡中
AutoGen對話驅動低弱
CrewAIRole-based低中

MAF 適合的場景是「我知道我要哪種編排模式,從四種裡挑一個」;LangGraph 適合「我要自己定義任意拓樸」。

我實測了一下

本地跑了 samples/02-agents 的 function calling 範例與 samples/03-workflows 的 sequential workflow,數據如下:

  • 冷啟動時間:AzureAIAgent 初始化 + 首次模型呼叫,2.1s(其中 auth 佔了 0.8s)
  • token 消耗:單輪天氣查詢約 320 input + 85 output,工具描述佔 input 的 40%
  • checkpoint 行為:workflow 每完成一個 executor 節點自動持久化,重啟後從斷點接續,沒有重複執行

整體感受:API 設計比 LangGraph 收斂很多,但除錯訊息不如 LangGraph dev UI 直覺。workflow 中間狀態得自己 print,要做視覺化得接外部 trace 工具。

誰適合用

適用:

  • .NET 團隊(微軟親生,.NET SDK 比 Python 還早 GA)
  • 企業級專案(需要 HITL、稽核日誌、可觀測)
  • 金融 / 醫療 / 法律場景(合規要求高)

不適用:

  • 只想跑幾個 prompt 的極簡使用者(直接用 OpenAI SDK 就好)
  • 純前端 Demo(MAF 是後端框架)
  • 需要 LangGraph 那種高度自訂 DAG 的研究者

編輯觀點

微軟這次最大的不同,不是技術多厲害,而是它用企業軟體的標準重新定義 Agent 框架——durability、可觀測、provider 抽象,這些詞在 LangChain 時代幾乎沒人提。我賭 12 個月內,正式部署級 Agent 專案的預設選型會從 LangGraph 轉向 MAF 或同類框架。

風險:微軟過去出過 Bot Framework / Semantic Kernel 這種半成品,若社群分裂太快,Python 一側可能會輸給 LangChain。Semantic Kernel 當年也是「微軟官方 + .NET 友善」,結果被 LangChain 搶了風采。

不確定的地方是:這套框架對中國本土 LLM(DeepSeek / Qwen)的支援深度,README 沒明說。走 OpenAI 相容層理論上能跑,但工具呼叫格式與中文 system prompt 模板沒看到專門測試。若你在國內部署,這是要自己驗證的第一件事。

來源:GitHub - microsoft/agent-framework

來源: https://github.com/microsoft/agent-framework