Part VI: Agentic AI
Chapter 24: Multi-Agent Systems

State Management, Workflows & Orchestration

"State is the silent partner in every agent conversation. Lose track of it and everything unravels."

Census Census, Statefully Orchestrated AI Agent
Big Picture

State is the silent partner in every agent system, and losing track of it is the leading cause of production failures. Complex agent workflows (customer onboarding, document processing, multi-step approvals) progress through well-defined states where different agents are responsible, different tools are available, and different failure modes apply. This section covers state machine design for agent workflows, checkpointing for long-running processes, and the orchestration patterns (especially LangGraph's graph-based approach) that make multi-agent workflows predictable, debuggable, and resumable across restarts. The architecture patterns from Section 24.2 define the topology; this section provides the execution engine.

Prerequisites

This section builds on tool use and protocols from Chapter 23 and agent foundations from Chapter 22.

Several friendly robots gathered around a large shared whiteboard, each writing with a different colored marker, with one robot apologetically erasing something another wrote, and a small traffic light showing access coordination
Figure 24.4.1: Shared state in multi-agent systems acts like a communal whiteboard. Multiple agents read and write to the same state, but without coordination (the traffic light), race conditions and conflicts can arise.

1. State Machines for Agent Workflows

Complex agent workflows require explicit state management. A customer onboarding workflow progresses through states: application received, identity verified, credit checked, compliance approved, account created, welcome email sent. At each state, different agents are responsible, different tools are available, and different failure modes apply. Modeling this as a state machine makes the workflow predictable, debuggable, and resumable.

LangGraph provides the most popular state machine implementation for agent workflows. State is represented as a TypedDict that flows between nodes (functions). Edges define transitions, and conditional edges enable branching based on state values. The state is checkpointed after each node execution, enabling the workflow to be paused and resumed at any point. This is critical for workflows that span hours or days, where the system must survive restarts and infrastructure changes.

from langgraph.graph import StateGraph, END
from langgraph.checkpoint.sqlite import SqliteSaver
from typing import TypedDict, Optional, Literal

class OnboardingState(TypedDict):
 customer_id: str
 application: dict
 identity_verified: Optional[bool]
 credit_score: Optional[int]
 compliance_status: Optional[str]
 account_id: Optional[str]
 status: str

def verify_identity(state: OnboardingState) -> dict:
 result = identity_agent.verify(state["application"])
 return {"identity_verified": result.verified, "status": "identity_checked"}

def check_credit(state: OnboardingState) -> dict:
 score = credit_agent.check(state["customer_id"])
 return {"credit_score": score, "status": "credit_checked"}

def compliance_review(state: OnboardingState) -> dict:
 result = compliance_agent.review(state)
 return {"compliance_status": result.status, "status": "compliance_reviewed"}

def route_after_compliance(state: OnboardingState) -> str:
 if state["compliance_status"] == "approved":
 return "create_account"
 elif state["compliance_status"] == "needs_review":
 return "human_review"
 return "reject"

# Build workflow with checkpointing
checkpointer = SqliteSaver.from_conn_string("onboarding.db")
graph = StateGraph(OnboardingState)
graph.add_node("verify_identity", verify_identity)
graph.add_node("check_credit", check_credit)
graph.add_node("compliance", compliance_review)
graph.add_node("create_account", create_account)
graph.add_node("human_review", request_human_review)
graph.add_node("reject", send_rejection)

graph.set_entry_point("verify_identity")
graph.add_edge("verify_identity", "check_credit")
graph.add_edge("check_credit", "compliance")
graph.add_conditional_edges("compliance", route_after_compliance)
graph.add_edge("create_account", END)
graph.add_edge("human_review", "compliance") # Re-check after human review
graph.add_edge("reject", END)

workflow = graph.compile(checkpointer=checkpointer)
Code Fragment 24.4.1: This snippet implements a debate pattern between two LangGraph agents with a SqliteSaver checkpoint for state persistence. The advocate and critic nodes exchange arguments over multiple rounds, and a judge_node evaluates both positions to produce a final verdict with confidence scores.

2. Durable Execution with Temporal

For workflows that span extended time periods, involve external service calls that may take minutes to complete, or need guaranteed execution despite infrastructure failures, durable execution frameworks like Temporal provide stronger reliability guarantees than in-process state machines. Temporal persists the complete execution history, automatically retries failed activities, and can resume workflows after server restarts without losing progress.

The integration pattern is straightforward: each agent step becomes a Temporal activity, the workflow definition describes the orchestration logic, and Temporal handles the infrastructure concerns (persistence, retries, timeouts, visibility). This separates the agent logic (what to do) from the infrastructure logic (how to do it reliably), making the system easier to reason about and maintain.

Real-World Scenario: LangGraph vs. Temporal for Different Scales

Who: A platform team at a Series C fintech company choosing infrastructure for their multi-agent compliance pipeline.

Situation: The pipeline processed loan applications through four agents (intake, credit check, compliance, underwriting). The prototype ran on a single server with LangGraph's SQLite checkpointer, completing most applications in under three minutes.

Problem: As volume grew to 500 applications per day, some workflows took hours (waiting for third-party credit bureau responses) and occasionally failed mid-pipeline when the server restarted during deployments. LangGraph's in-process checkpointing could not recover workflows that were interrupted across server restarts.

Decision: The team migrated long-running workflows to Temporal while keeping short-lived agent interactions on LangGraph. Each agent step became a Temporal activity with automatic retries and timeout handling. LangGraph remained the framework for the reasoning logic within each agent; Temporal handled the cross-agent orchestration and durability.

Result: Zero lost applications after the migration, compared to 3 to 5 per week previously. Observability improved because Temporal's built-in dashboard showed the state of every in-flight application. Operational overhead increased (Temporal required a dedicated cluster), but the reliability gain justified the cost.

Lesson: Use lightweight checkpointing for workflows that complete in minutes; invest in durable execution infrastructure like Temporal when workflows span hours, touch unreliable external services, or must survive infrastructure restarts.

Key Insight

State management is the hidden complexity of multi-agent systems. Most agent tutorials focus on the reasoning and tool-calling aspects, but production failures disproportionately come from state management: a workflow that cannot resume after a crash, state that becomes inconsistent when two agents update it simultaneously, or context that is lost during an agent handoff. Invest in state management infrastructure (checkpointing, typed state schemas, conflict resolution policies) early. It is far easier to add better reasoning later than to retrofit reliable state management into a system designed without it. The observability practices from Section 26.3 help you detect state-related failures before they become customer-facing incidents.

3. Parallel Execution and Fan-Out

Many agent workflows contain steps that can execute in parallel. When a supervisor delegates independent subtasks to multiple specialists, those subtasks can run concurrently rather than sequentially. LangGraph supports this through the Send API, which fans out execution to multiple node instances and collects results. This can reduce wall-clock time dramatically for tasks with parallelizable subtasks.

The challenge with parallel execution is error handling. If three of five parallel subtasks succeed but two fail, what should the system do? Continue with partial results? Retry the failed tasks? Fail the entire workflow? The answer depends on the task: a research workflow can proceed with partial results, while a compliance workflow must have all checks pass. Design your fan-out patterns with explicit handling for partial failure scenarios.

Warning

Parallel agent execution multiplies API costs linearly and can hit rate limits quickly. If you fan out to 10 agents simultaneously, each making 5 LLM calls, that is 50 concurrent API requests. Implement concurrency limits in your orchestrator and test against your API provider's rate limits before deploying parallel patterns to production. Most providers offer batch APIs that are cheaper for high-volume parallel use cases.

Exercises

Exercise 24.4.1: State Machine Fundamentals Conceptual

Explain how a state machine differs from a simple sequential pipeline for agent workflows. What capabilities does a state machine provide that a pipeline does not?

Answer Sketch

A pipeline is a fixed sequence: A then B then C. A state machine supports conditional transitions: from state B, go to C if successful or back to A if failed. State machines enable loops, branching, error recovery, and dynamic routing based on intermediate results. They can also pause and resume (durability), which pipelines cannot do natively.

Exercise 24.4.2: LangGraph State Machine Coding

Build a LangGraph state machine with four states (intake, research, draft, review) and conditional edges. From the review state, the workflow should loop back to draft if quality is below threshold, or proceed to a final output state.

Answer Sketch

Define a WorkflowState TypedDict. Create node functions for each state. Add a quality_check function that returns 'revise' or 'approve'. Use graph.add_conditional_edges('review', quality_check, {'revise': 'draft', 'approve': END}). Add a revision counter to prevent infinite loops.

Exercise 24.4.3: Durable Execution Benefits Conceptual

Explain why durable execution (as provided by Temporal or Inngest) is important for production agent workflows. What happens to a standard agent workflow if the server crashes mid-execution?

Answer Sketch

Without durable execution, a server crash loses all in-progress state. The workflow must restart from scratch, potentially repeating expensive LLM calls and tool executions. Durable execution persists each step's result, so after a crash the workflow resumes from the last completed step. This is critical for long-running agent tasks that span minutes or hours and involve costly API calls.

Exercise 24.4.4: Fan-Out/Fan-In Pattern Coding

Implement a fan-out/fan-in pattern where a coordinator agent splits a research task into three parallel subtasks, runs them concurrently, and merges the results. Use Python's asyncio.gather().

Answer Sketch

The coordinator generates three subtask descriptions. Use asyncio.gather(run_agent(t1), run_agent(t2), run_agent(t3)) to execute in parallel. Collect results, pass them to a synthesis agent that merges the findings into a coherent report. Handle partial failures by proceeding with available results and noting which subtasks failed.

Exercise 24.4.5: Workflow Checkpoint Design Conceptual

Design a checkpointing strategy for a multi-step agent workflow. What state should be saved at each checkpoint, and how should the system handle recovery from different failure points?

Answer Sketch

Save at each checkpoint: the current state (all accumulated data), the completed steps list, the pending steps, and any tool call results. On recovery, load the latest checkpoint and resume from the next pending step. For idempotent tools, simply re-execute. For non-idempotent tools (e.g., sending emails), check a sent-messages log before re-executing to avoid duplicates.

Key Takeaways
Self-Check
Q1: Why are state machines useful for orchestrating agent workflows?
Show Answer

State machines make the workflow's possible states, transitions, and guard conditions explicit and inspectable. This prevents agents from entering invalid states, enables checkpoint/resume, and makes debugging easier because you can trace exactly which state transitions occurred.

Q2: What is the difference between orchestration and choreography in multi-agent workflows?
Show Answer

Orchestration uses a central controller that directs agent interactions according to a predefined workflow. Choreography has no central controller; agents react to events and follow local rules, with the global workflow emerging from their interactions. Orchestration is easier to debug; choreography is more resilient.

What Comes Next

In the next section, Human-in-the-Loop Agent Systems, we explore how to integrate human oversight, approval gates, and feedback loops into multi-agent workflows.

References and Further Reading

State Management and Orchestration

LangChain (2024). "LangGraph: Build Stateful Multi-Actor Applications." LangGraph Documentation.

Documentation for building stateful agent workflows as graphs with persistence, checkpointing, and human-in-the-loop support using LangGraph.

Documentation

Temporal Technologies (2024). "Temporal Documentation." docs.temporal.io.

Documentation for Temporal, a durable execution platform that provides exactly-once execution guarantees for long-running agent workflows with automatic retry and recovery.

Documentation

Hong, S., Zhuge, M., Chen, J., et al. (2024). "MetaGPT: Meta Programming for a Multi-Agent Collaborative Framework." ICLR 2024.

Implements structured state management through shared message pools and role-based access control, demonstrating workflow orchestration patterns for software development agents.

Paper

Parallel and Distributed Execution

Wu, Q., Bansal, G., Zhang, J., et al. (2023). "AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation." arXiv preprint.

Demonstrates group chat orchestration with dynamic speaker selection, fan-out patterns, and nested conversation workflows for parallel multi-agent execution.

Paper

Qian, C., Cong, X., Yang, C., et al. (2024). "Communicative Agents for Software Development." ACL 2024.

ChatDev uses phase-based state machines with clear handoff points between agents, illustrating workflow orchestration for multi-step collaborative tasks.

Paper

Kapoor, S., Stroebl, B., Siber, Z.S., et al. (2024). "AI Agents That Matter." arXiv preprint.

Analyzes cost and latency trade-offs in agent systems, relevant to orchestration decisions about parallel execution versus sequential processing.

Paper