5 Agentic workflows with LangGraph
The chapter introduces agentic workflows as structured, multi-step patterns that use large language models to choose the next action from a predefined set while maintaining explicit, reliable state throughout execution. It distinguishes these workflows—valuing transparency, repeatability, and control—from more autonomous agents that dynamically plan and combine tools in response to evolving context. After outlining when agent-based architectures are useful and highlighting common workflow patterns such as Controller-Worker and Router, the chapter briefly surveys popular frameworks and emphasizes LangGraph for building persistent, stateful workflows.
LangGraph extends LangChain with a graph-based model in which nodes are task functions and edges define the data and control flow between them, all driven by an explicit, strongly typed state. Using a StateGraph and a TypedDict state, developers define entry points, conditional edges, and end conditions to manage branching, looping, and cyclical refinement. Compared to linear chains, this structure enables better decision-making, memory, and error handling, while improving observability and debugging by making data movement and transitions clear and auditable.
To demonstrate the transition from chains to an agentic workflow, the chapter converts a LangChain web research assistant into a LangGraph application. The system is decomposed into modular nodes—assistant selection, query generation, web search, summarization, relevance evaluation, and report writing—connected by conditional routing that loops back to regenerate queries when fewer than half the summaries are relevant, with a cap on iterations to prevent infinite cycles. The result is a flexible, maintainable workflow with explicit state, clearer flow control, targeted error handling, and straightforward extensibility, illustrating how LangGraph enables adaptive, multi-step applications that remain predictable and reproducible.
Workflows and agents: workflows use the LLM to choose the next step from a fixed set of options, such as routing a request to a SQL database or a REST API and synthetizing the answer with the related results. Agents, however, dynamically select and combine tools to achieve their objectives.
Common Workflows patterns: the Controller-Worker pattern uses the LLM in the controller orchestrates the flow by assigning various tasks to workers following a certain sequence. In the Router pattern, the LLM simply directs the task to the appropriate worker based on the context.
LangGraph core Components: a strongly typed state (in this example, modelled with ResearchState) flows through the workflow. Nodes, usually Python functions (like def select_assistant), perform tasks, and edges create directed data flows between nodes, in some cases with conditional paths.
Summary
- Agentic workflows follow fixed, predictable steps, while agents use LLMs for reasoning, dynamic tool selection, and real-time adaptation.
- Moving from basic LangChain apps to agentic workflows with LangGraph gives greater control and flexibility for complex tasks.
- LangGraph extends LangChain with stateful, multi-step workflows organized as nodes, edges, and explicit state updates.
- Replacing linear chains with LangGraph enables explicit state tracking and conditional branching, simplifying debugging and error handling.
- Conditional flows based on runtime evaluations let applications adapt, such as re-running searches when results are insufficient.
- Core components include the StateGraph, node functions, and well-defined edges and entry points for maintainable, extensible designs.
- The case study shows converting a web research assistant from a rigid chain to a flexible agent-based system with modular design and clear state management.
- LangGraph boosts control, modularity, and future scalability, enabling complex decision-making and better overall performance.
FAQ
What is an agentic workflow, and how does it differ from an agent?
Agentic workflows guide an application through a fixed sequence of predetermined steps. The LLM selects among predefined options to advance the flow, emphasizing reliability, transparency, and control. Agents, by contrast, reason and decide dynamically, selecting and combining tools based on evolving context to achieve goals. Workflows keep a structured, predictable path; agents adapt in real time.
What are the Controller-Worker and Router workflow patterns?
- Controller-Worker: An LLM-powered controller orchestrates the flow by assigning tasks to workers in a specific sequence.
- Router: The LLM routes the task to the most appropriate worker (processor) based on context, without orchestrating a full sequence.
When should I choose agent-based architectures (workflows or agents)?
Use them when your app needs to break complex tasks into smaller steps, make decisions based on intermediate results, access external tools or data, or maintain context across extended interactions. Adopt them when explicit state management and dynamic control provide clear value relative to added complexity.
What is LangGraph, and how does it relate to LangChain?
LangGraph is a framework for building stateful, multi-step AI applications using a graph-based structure. It extends (does not replace) LangChain: LangChain provides components (LLMs, embeddings, retrievers), while LangGraph offers the blueprint to connect them into a structured, stateful workflow with nodes, edges, and explicit state.
Why move from LangChain chains to LangGraph?
- Better state management: Explicit, strongly typed state across steps.
- Conditional branching: Take different paths based on results.
- Cyclical workflows: Repeat steps until conditions are met.
- Clarity and debugging: Graph structure makes data flow traceable.
- Scalability: Easier to model complex, multi-step and parallel processes.
What are the core components of a LangGraph application?
- State: A strongly typed object (e.g., a TypedDict) that flows through the graph.
- Nodes: Processing units (often Python functions) that read and update state.
- Edges: Directed transitions connecting nodes.
- Conditional edges: Dynamic routing based on runtime state.
- Entry point and end condition: Define where execution starts and when it completes (e.g., using END).
How is state defined and updated in LangGraph?
State is explicitly defined (commonly as a Python TypedDict) to ensure structure and type safety. Each node receives the current state and returns a partial update (a dict), which merges into the overall state, keeping data flow predictable and debuggable.
How do nodes, edges, and conditional edges work?
- Nodes: Functions that take the current state and return updates (e.g., generate queries, call APIs, summarize).
- Edges: Specify the next node(s) to run (linear transitions).
- Conditional edges: A routing function inspects the state and returns the next node name, enabling dynamic paths (e.g., refine queries vs. summarize results).
How was the Web Research Assistant transformed into a LangGraph workflow?
The original linear LangChain app was decomposed into nodes: Assistant Selector, Query Generator, Web Searcher, Content Summarizer, Relevance Evaluator, and Report Writer. A conditional edge routes based on relevance: if fewer than a threshold of summaries are relevant, the flow loops back to regenerate queries; otherwise, it proceeds to report writing. An iteration cap (e.g., 3) prevents infinite loops.
Which agent development frameworks are covered, and how does LangGraph compare?
- LangGraph: Stateful, persistent agentic workflows with graph-based execution; powerful for complex apps; requires more technical expertise.
- AutoGPT: Fully autonomous, goal-driven agents; may struggle with task consistency.
- LlamaIndex: Strong in knowledge retrieval; narrower scope.
- Microsoft Autogen: Highly customizable multi-agent conversations; steeper learning curve.
- OpenAI Agent SDK & Google ADK: Streamlined APIs for multi-agent systems and workflows.
- n8n & LangFlow: Visual interfaces and integrations; accessible to non-developers; advanced reasoning may need extra components.
- Microsoft Semantic Kernel: Emphasizes memory and planning; integrates well with Azure.
- CrewAI: Templates for collaborative, specialized multi-agent teams.
AI Agents and Applications ebook for free