← Back to Tutorials

9. Agent Orchestration

Sequential Workflows

Chain agents in sequence where each agent's output becomes the next agent's input. Useful for document processing pipelines.

# Sequential workflow pseudocode
# Agent 1: Extract document metadata
# Agent 2: Classify document type
# Agent 3: Generate summary
# Agent 4: Store results

async def process_document(document):
    metadata = await extract_agent.run(f"Extract metadata from: {document}")
    doc_type = await classify_agent.run(f"Classify: {metadata}")
    summary = await summarize_agent.run(f"Summarize: {document}")
    await storage_agent.run(f"Store: {metadata} | {doc_type} | {summary}")
    return summary

Human-in-the-Loop (HITL)

For critical decisions, agents can pause and request human approval before proceeding.

# HITL pattern
async def approve_expense(amount, reason):
    if amount > 1000:
        # Pause and ask human
        approval = await ask_human(
            f"Approve ${amount} for {reason}?",
            timeout=3600  # Wait up to 1 hour
        )
        if not approval:
            return "Expense rejected by manager"
    return "Expense approved"

Agent Chat Systems

Build multi-turn chat systems where different agents handle different parts of the conversation:

Orchestration Best Practices

✏️ Exercise: Build a three-agent sequential workflow: (1) a research agent that gathers information, (2) an analysis agent that processes the information, and (3) a reporting agent that generates a formatted report. Add a human-in-the-loop approval step before the report is finalized.