27%

1.4 Workflow Enforcement and Handoff

Workflow Enforcement คืออะไร

Workflow Enforcement คือการบังคับให้ agent ทำงานตามลำดับขั้นตอนที่กำหนดไว้ — ไม่ใช่ปล่อยให้ตัดสินใจเองทั้งหมด ในหลายๆ use case เราต้องการให้ agent ทำตาม process ที่แน่นอน เช่น “ต้อง validate input ก่อน → แล้ว process → แล้ว review → แล้ว output” ถ้า agent ข้าม step ไปตรงๆ อาจได้ผลลัพธ์ที่ไม่ถูกต้อง

Handoff คือจุดที่ agent ตัวหนึ่งส่งต่องานให้อีกตัว — รวมถึงข้อมูล context ที่จำเป็น สถานะปัจจุบัน และ instructions สำหรับ step ถัดไป

State Machine Pattern

ใช้ state machine เพื่อควบคุม flow ของ agent:

from enum import Enum

class WorkflowState(Enum):
    INTAKE = "intake"
    RESEARCH = "research"
    DESIGN = "design"
    REVIEW = "review"
    DELIVER = "deliver"

TRANSITIONS = {
    WorkflowState.INTAKE: [WorkflowState.RESEARCH],
    WorkflowState.RESEARCH: [WorkflowState.DESIGN],
    WorkflowState.DESIGN: [WorkflowState.REVIEW],
    WorkflowState.REVIEW: [WorkflowState.DESIGN, WorkflowState.DELIVER],
    WorkflowState.DELIVER: [],
}

class WorkflowEngine:
    def __init__(self):
        self.state = WorkflowState.INTAKE
        self.context = {}

    def transition(self, next_state):
        if next_state not in TRANSITIONS[self.state]:
            raise ValueError(f"Cannot transition from {self.state} to {next_state}")
        self.state = next_state

    def run_current_step(self):
        agent_prompt = self.build_prompt_for_state(self.state)
        result = run_agent(agent_prompt, context=self.context)
        self.context[self.state.value] = result
        return result

Handoff Protocol

เมื่อ agent ส่งต่องาน ต้องส่ง handoff packet ที่มีข้อมูลครบ:

class HandoffPacket:
    def __init__(self):
        self.from_agent: str          # ใครส่ง
        self.to_agent: str            # ส่งให้ใคร
        self.task_summary: str        # สรุปงานที่ทำมาแล้ว
        self.deliverables: list       # output ที่ได้
        self.open_questions: list     # คำถามที่ยังไม่ตอบ
        self.constraints: list        # ข้อจำกัดที่ต้องเคารพ
        self.next_action: str         # สิ่งที่ต้องทำต่อ

ตัวอย่าง Handoff ใน Workflow Script

phase('Research')
const research = await agent(`
  Research the following requirements and produce verified findings:
  ${requirements}

  Output: research.md with citations
`, {label: 'researcher', phase: 'Research'})

phase('Design')
const design = await agent(`
  Based on the research findings below, design a solution architecture.

  ## Research Findings
  ${research}

  ## Constraints
  - Must support 10k concurrent users
  - Budget: $5k/month cloud cost

  Output: design.md with diagram description
`, {label: 'designer', phase: 'Design'})

Guardrails

Guardrails คือกฎที่ป้องกันไม่ให้ agent ทำสิ่งที่ไม่ควร:

Input Guardrails

ตรวจ input ก่อนส่งให้ agent:

  • Validate format
  • Check for injection attempts
  • Ensure required fields present

Output Guardrails

ตรวจ output หลัง agent ทำงานเสร็จ:

  • Schema validation
  • Safety checks (ไม่มี PII, ไม่มี harmful content)
  • Business rule compliance

Execution Guardrails

ควบคุมระหว่าง agent ทำงาน:

  • Tool allowlist/blocklist
  • Rate limiting
  • Cost caps
  • Time limits
class GuardedAgent:
    def __init__(self, allowed_tools, max_cost, max_time):
        self.allowed_tools = allowed_tools
        self.max_cost = max_cost
        self.max_time = max_time
        self.total_cost = 0

    def execute_tool(self, tool_name, params):
        if tool_name not in self.allowed_tools:
            return {"error": f"Tool {tool_name} not allowed in this workflow step"}
        if self.total_cost > self.max_cost:
            return {"error": "Cost budget exceeded"}
        return run_tool(tool_name, params)

Key Concepts

  • Deterministic Workflows — บาง flow ต้อง deterministic (state machine) ไม่ใช่ทุก flow ที่ควรให้ agent ตัดสินใจเอง
  • Handoff Completeness — handoff ที่ดีต้องมีข้อมูลเพียงพอให้ agent ถัดไปทำงานได้โดยไม่ต้องถามกลับ
  • Gate Conditions — ก่อนจะ transition ไป state ถัดไป ต้องผ่าน gate (เช่น review ต้อง pass ก่อนถึง deliver)
  • Rollback — ถ้า step ล้มเหลว ต้องมีวิธี rollback กลับไป state ก่อนหน้า

Exam Tips

  • ข้อสอบจะถามว่า pattern ไหนเหมาะกับ scenario ไหน — state machine เหมาะกับ regulated workflows ที่ต้อง audit ได้
  • Handoff protocol ที่ดีต้อง self-contained — agent ที่รับงานไม่ควรต้องหาข้อมูลเพิ่มเอง
  • Guardrails ควรอยู่ทั้ง 3 layers (input, execution, output) — ไม่ใช่แค่ตรวจ output
  • เข้าใจว่า workflow enforcement ลด flexibility ของ agent แต่เพิ่ม predictability และ safety
  • Gate conditions (review before deliver) เป็น pattern สำคัญสำหรับ production systems