Skip to content

Exercise 04: Sequential Pattern

Objective

Implement a multi-agent pipeline where each agent's output feeds into the next — the sequential orchestration pattern.

Concepts Covered

  • Linear agent pipeline (Research → Draft → Edit)
  • Fresh context per stage (only passing the previous agent's output)
  • Progressive refinement of content
  • Context compaction between stages

How It Works

Three agents process content in a pipeline. The critical design decision: each agent gets a fresh messages list containing only the previous agent's final output — not its internal tool calls, reasoning, or full conversation history. This prevents context pollution and keeps each stage focused.

flowchart LR
    subgraph Stage1["Stage 1: Research Agent"]
        R_SYS["System prompt:<br/>'You are a research agent'"]
        R_TOOL["Tools: search_web,<br/>search_academic"]
        R_OUT["Research output<br/>(text summary)"]
        R_SYS --> R_TOOL --> R_OUT
    end

    subgraph Stage2["Stage 2: Draft Writer"]
        D_SYS["System prompt:<br/>'You are a writer'"]
        D_IN["Input: research<br/>output ONLY"]
        D_OUT["Draft article"]
        D_SYS --> D_IN --> D_OUT
    end

    subgraph Stage3["Stage 3: Editor Agent"]
        E_SYS["System prompt:<br/>'You are an editor'"]
        E_IN["Input: draft<br/>article ONLY"]
        E_OUT["Polished article"]
        E_SYS --> E_IN --> E_OUT
    end

    R_OUT -- "Only final text<br/>(no tool history)" --> D_IN
    D_OUT -- "Only draft text<br/>(no prior context)" --> E_IN

    style Stage1 fill:#e8f4fd,stroke:#4a90d9
    style Stage2 fill:#fef9e7,stroke:#f39c12
    style Stage3 fill:#eafaf1,stroke:#2ecc71

Each stage in detail:

sequenceDiagram
    participant Script as Orchestrator
    participant R as Research Agent
    participant D as Draft Writer
    participant E as Editor Agent
    participant LLM as LLM Provider

    Note over Script,LLM: Stage 1 — Research (has tools)
    Script->>R: Fresh messages: [system, user(topic)]
    R->>LLM: messages + tools=[search_web, search_academic]
    LLM-->>R: tool_calls → execute → loop until done
    R-->>Script: research_output (text only)

    Note over Script,LLM: Stage 2 — Draft (no tools)
    Script->>D: Fresh messages: [system, user(research_output)]
    D->>LLM: messages (no tools)
    LLM-->>D: draft article
    D-->>Script: draft_output (text only)

    Note over Script,LLM: Stage 3 — Edit (no tools)
    Script->>E: Fresh messages: [system, user(draft_output)]
    E->>LLM: messages (no tools)
    LLM-->>E: polished article
    E-->>Script: final_article

Context sharing: Fresh context per stage. Each agent gets a new messages list containing only [system_prompt, user_message_with_previous_output]. The Research Agent's tool calls, intermediate reasoning, and raw search results are never seen by the Draft Writer. Only the final text output crosses the boundary. The code uses log_context_pass() to make these handoff points visible in the logs.

Structured output: Not used. Plain text strings are passed between stages.

Why fresh context?

Passing full history would waste tokens and risk confusing downstream agents with irrelevant details (like raw search API responses). Fresh context keeps each stage focused on its specific task while still receiving the essential information it needs.

Interactive Message Flow

You are a research specialist. Gather key facts, statistics, and insights on the given topic.
Research the topic: The impact of remote work on software engineering productivity
Key findings: 1) Studies show 13% productivity increase for remote workers. 2) Communication overhead increases by 20%. 3) Code quality metrics remain stable. 4) Developer satisfaction up 45%. 5) Onboarding new team members is the biggest challenge.
You are a skilled technical writer. Write a clear, engaging article based on the provided research.
Write an article based on this research: Key findings: 1) Studies show 13% productivity increase... 2) Communication overhead increases by 20%... 3) Code quality metrics remain stable...
The Remote Revolution: How Working From Home Changes Code. Remote work has transformed software engineering. Studies reveal a 13% boost in individual productivity, though teams face new communication challenges with a 20% increase in coordination overhead...
You are an expert editor. Polish the article for clarity, flow, grammar, and professional style.
Polish this draft: The Remote Revolution: How Working From Home Changes Code. Remote work has transformed software engineering...
The Remote Revolution: Rethinking Productivity in Software Engineering. The shift to remote work has fundamentally reshaped how software teams operate — and the data tells a compelling story. Individual productivity has risen by 13%, yet this gain comes with a nuanced trade-off...

Files

  1. 01_content_pipeline.py — Three-agent content pipeline that produces a polished article

How to Run

python exercises/04_sequential/01_content_pipeline.py

Expected Output

Structured logging showing each pipeline stage, what context is passed between agents, and the progressive refinement of the article.

Next

→ Next: Exercise 05: Concurrent Pattern