Implement Workflow Patterns with LangGraph
Summary
This is a 45-minute hands-on lab that teaches 3 core workflow patterns for building coordinated multi-agent AI systems using LangGraph and LangChain. Learners implement practical projects including job application assistants, intelligent task routers, and multilingual processors, and complete a final exercise building a multi-agent routing system for ride hailing, restaurant orders, and grocery services.
Estimated Time
45 minutes
Core Prerequisites & Dependencies
Required Python packages for implementation:
langchain-openai==0.3.27langgraph==0.6.6pygraphviz==1.14
Core LLM configuration: gpt-4o-mini model via the ChatOpenAI interface.
LangGraph Fundamentals
Core Components
- StateGraph: The foundational workflow class that defines the structure of data (via TypedDict) flowing through the workflow, validates data types, and manages state transitions between nodes.
- Nodes: Discrete, modular steps in the workflow, each representing a specialized agent or task. Nodes receive the current state, process data (typically via LLM calls), and return an updated state.
- Edges: Connections between nodes that define the execution order of the workflow:
- Standard edges: Define sequential execution between two nodes
- Conditional edges: Dynamically route execution to different nodes based on a state value
- State Management: Shared memory for the workflow, defined using Python’s
TypedDict, that stores all intermediate and final outputs passed between nodes. - Entry/Finish Points: Define the start and end nodes of the workflow, marking where execution begins and terminates.
Utility Function
def print_workflow_info(workflow, app=None):
"""Prints comprehensive information about a LangGraph workflow."""
print("WORKFLOW INFORMATION")
print("====================")
print(f"Nodes: {workflow.nodes}")
print(f"Edges: {workflow.edges}")
# Access finish points if available
try:
finish_points = workflow.finish_points
print(f"Finish points: {finish_points}")
except:
try:
print(f"Finish point: {workflow._finish_point}")
except:
print("Finish points attribute not directly accessible")
# Visualize workflow if compiled app is provided
if app:
print("\nWorkflow Visualization:")
from IPython.display import display
display(app.get_graph().draw_png())Core Workflow Patterns
1. Sequential Agent Coordination (Prompt Chaining)
Definition
A workflow design pattern where complex tasks are decomposed into a sequence of LLM calls. Each step depends on the output of the previous one, allowing for step-by-step refinement or evolution of the data being processed. The pattern supports modularity, function calling, and external tool injection between steps.
Common Use Cases
- Step-by-step content generation (idea → outline → draft → polish)
- Automated report generation (extract → analyze → summarize)
- Educational content creation
Implementation Example: Job Application Assistant
- State management via
ChainStateTypedDict (storesjob_description,resume_summary,cover_letter) - 2 sequential nodes:
generate_resume_summary: Creates role-tailored resume summary from the input job descriptiongenerate_cover_letter: Generates a personalized cover letter using the generated resume summary and original job description
- Workflow structure: Entry point →
generate_resume_summary→generate_cover_letter→ End
2. Intent-Based Routing
Definition
A pattern where a specialized router agent classifies incoming input (based on user intent) and routes it to the appropriate specialized sub-process or agent, acting as an intelligent switchboard for multi-task systems.
Routing Techniques
- Hard-coded keyword-based routing (primitive)
- LLM-based routing via classification prompts
- Embedding-based semantic matching with routing maps
Common Use Cases
- AI customer service bots (routing billing, tech support, or general inquiries)
- Multi-skill AI agents
- Adaptive educational bots
Implementation Example: Summarization/Translation Task Router
- State management via
RouterStateTypedDict (storesuser_input,task_type,output) - Nodes:
router_node: Classifies user intent as either “summarize” or “translate” using LLM tool bindingsummarize: Processes summarization requeststranslate: Processes French translation requests
- Workflow structure: Entry point →
router_node→ Conditional edge routing to appropriate task node → End
3. Parallel Agent Execution
Definition
A pattern where independent LLM tasks are executed simultaneously instead of sequentially, reducing processing time and improving system throughput for tasks with no interdependencies.
Parallelization Techniques
- Format Diversity: Run the same input through different prompt styles, languages, or output formats, then combine all results
- Task Splitting: Divide large input into smaller parts, run the same task on each part in parallel, then merge partial results
- Consensus Voting: Run the same task multiple times with different agents or prompt styles, then select the best result via ranking or majority vote
Common Use Cases
- Simultaneous summarization of different sections of a large document
- Batch translation of multiple user messages
- Multi-variation content generation for marketing or product materials
- Ensembled safety checks for AI outputs
Implementation Example: Multilingual Translation Assistant
- State management via
StateTypedDict (storestext,french,spanish,japanese,combined_output) - Nodes:
translate_french,translate_spanish,translate_japanese: All run in parallel immediately from workflow startaggregator: Combines all parallel translation outputs into a single formatted final result
- Workflow structure: Start → All 3 translation nodes (parallel execution) →
aggregator→ End
Exercise: Build a Multi-Agent Routing System
Task Overview
Learners build a production-ready routing system for 3 service categories plus a default fallback handler:
- Ride hailing requests
- Restaurant orders
- Grocery delivery requests
- Default handler for unclassified requests
Core Implementation Steps
- Define a
RouterStateTypedDict for state management and aRouterPydantic model for LLM tool binding - Implement router logic with a fallback to
default_handlerfor unrecognized requests - Implement 4 specialized processing nodes for each service category and fallback
- Assemble the StateGraph with conditional routing rules and compile to an executable application
- Test the system with sample test cases covering all request types.
Authors & Version
Authors
- Kunal Makwana (IBM, Generative AI & Machine Learning Specialist)
- Joseph Santarcangelo (IBM, PhD in Electrical Engineering, ML/Computer Vision Researcher)
Change Log
| Date (YYYY-MM-DD) | Version | Changed By | Change Description |
|---|---|---|---|
| 2025-07-17 | 0.1 | Kunal Makwana | Initial version created |
| 2025-07-22 | 0.2 | Steve Ryan | ID review |
| 2025-07-22 | 0.3 | Leah Hanson | QA review |
Copyright
© IBM Corporation. All rights reserved.