Step 7 of 16
Use agents as functions and build sequential, concurrent, handoff, or checkpointed workflows.
แยก specialist เมื่อ agent หลักมีหน้าที่มากขึ้น และเลือก workflow เมื่อลำดับงานต้องควบคุมครับ
Level: Advanced
เมื่อ agent ตัวเดียวมี instructions และ tools มากเกินไป การเลือก tool จะยากขึ้นครับ แยก specialist ตาม capability แล้วให้ coordinator เรียก agent ย่อยเป็น function หรือใช้ workflow เมื่อ execution order ต้องควบคุม
เลือก orchestration จาก ownership ของ decision แล้วสร้าง workflow ที่มี deterministic code รอบ agent step ครับ
AIAgent policyAgent = projectClient.AsAIAgent(
model: model,
name: "PolicyAgent",
description: "Answers questions from approved policy context.",
instructions: "Return an answer with source IDs.");
AIAgent coordinator = projectClient.AsAIAgent(
model: model,
name: "SupportCoordinator",
instructions: "Delegate policy questions to PolicyAgent.",
tools: [policyAgent.AsAIFunction()]);
วิธีนี้เหมาะเมื่อ coordinator ให้ model เลือกว่าจะ delegate หรือไม่ครับ กำหนด description ให้ agent ย่อยแคบ และจำกัด tools ตาม least privilege
ถ้างานต้อง ingest, validate, approve และ publish ตามลำดับ ให้ใช้ workflow แทนการหวังว่า prompt จะรักษาลำดับ:
using Microsoft.Agents.AI.Workflows;
Func<string, string> normalize = text => text.Trim();
var normalizeExecutor = normalize.BindAsExecutor("Normalize");
var validateExecutor = new ValidateDocumentExecutor();
var builder = new WorkflowBuilder(normalizeExecutor);
builder
.AddEdge(normalizeExecutor, validateExecutor)
.WithOutputFrom(validateExecutor);
var workflow = builder.Build();
Workflow เพิ่ม checkpointing, routing และ human-in-the-loop ได้ แต่มี state ที่ต้องดูแลมากขึ้นครับ เริ่มด้วย sequential graph ขนาดเล็กแล้วค่อยเพิ่ม branch
Agent Framework workflows ใช้ superstep model ครับ Executors ที่ถูก trigger ใน superstep เดียวกันทำงานคู่ขนาน และ workflow จะรอให้ทุกตัวจบก่อนเดินต่อ หาก branch หนึ่งต้องก้าวต่ออย่างอิสระ ให้รวม chain นั้นใน executor เดียวหรือแยก workflow
| Need | Pattern |
|---|---|
| Model เลือก specialist | Agent as function |
| หลายงานอิสระ | Concurrent workflow |
| ลำดับคงที่ | Sequential workflow |
| Specialist ส่งงานต่อ | Handoff |
| ต้อง resume หลังหยุด | Checkpointed workflow |
เก็บ trace ของ coordinator, specialist และ workflow run ใต้ correlation ID เดียวเพื่อ debug ได้ทั้งเส้นครับ
ทดสอบ deterministic executor แยกจาก agent แล้วเพิ่ม workflow test สำหรับ happy path, one branch failure, cancellation และ resume from checkpoint เปรียบเทียบ output เมื่อ run input เดิมหลายรอบ และระบุส่วนที่ model ทำให้ผลเปลี่ยนได้ครับ
ใช้ standard CheckpointManager เมื่อต้อง resume ภายใน Agent Framework runtime ใช้ Durable Extension เมื่อต้องกระจายงานข้าม workers, รอหลายชั่วโมง หรือ recover โดยไม่ทำ completed step ซ้ำ สองแบบนี้แก้โจทย์คนละระดับครับ
ส่ง orchestration decision record, workflow diagram, executor tests, checkpoint test และ failure policy ต่อ node