No history yet

Stateful Chains

Beyond Stateless Calls

Simple AI applications often work like a vending machine. You put in a prompt (a coin), and you get a response (a snack). Each interaction is isolated and independent. This is a stateless model, and it's fine for one-off tasks like summarizing an article or generating an image description. But complex, multi-step reasoning requires memory. An AI agent building a financial report can't forget the Q1 numbers when it gets to Q2. It needs context that persists and evolves.

This is where stateful architecture comes in. Instead of treating each request as a blank slate, we build a system that maintains a persistent 'thread' of information. This thread, or state, can include conversation history, intermediate calculations, or retrieved documents. It's the agent's working memory, allowing it to build on previous steps, correct mistakes, and handle intricate workflows that unfold over time.

Stateful systems turn a series of disconnected queries into a coherent, goal-oriented conversation.

To build these systems, we need a way to compose operations while passing state between them. In the LangChain ecosystem, the foundation for this is the (LCEL). It provides a 'runnable' protocol, which is a standard interface for chaining components together. Think of runnables as Lego bricks. Each one performs a specific job, like calling an LLM or fetching data from a database. LCEL lets you pipe them together, defining a clear path for data to flow from one brick to the next.

from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser

# Define the components of our chain
prompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")
model = ChatOpenAI()
output_parser = StrOutputParser()

# Use LCEL to pipe them together into a runnable chain
chain = prompt | model | output_parser

# Invoke the chain
response = chain.invoke({"topic": "bears"})
print(response)

This simple chain is still stateless. It takes an input, runs through the sequence, and produces an output. It has no memory of past invocations. To add memory, we need to manage state explicitly. This is where graph-based frameworks like LangGraph enter the picture.

Graphs for Control Flow

LangGraph structures AI workflows as a state machine rather than a simple, linear chain. A state machine is a model that can be in one of a finite number of conditions, or 'states'. It transitions from one state to another based on inputs. Imagine a traffic light: its state can be green, yellow, or red, and it transitions between them based on a timer.

In LangGraph, the entire workflow is represented as a graph. This graph has two primary components:

  • Nodes: These are functions or runnable objects that perform an action. A node might call a tool, query an LLM, or process data. Each node's job is to update the system's state.
  • Edges: These are connections that direct the flow from one node to another. Edges can be conditional, allowing the graph to create cycles, branches, and complex routes. For example, an edge could check if a tool call was successful and route to a 'success' node or a 'retry' node accordingly.

The key to making this work is the State Schema. This is a defined data structure, often a Python dictionary or a Pydantic model, that gets passed between nodes. Each node reads from the state, performs its task, and then writes its results back to the state. This cumulative object is the single source of truth for the workflow's memory.

from typing import Dict, TypedDict, List

# Define the state schema for our graph
class AgentState(TypedDict):
    messages: List[str] # Conversation history
    # Add other fields as needed, e.g., 'documents', 'current_task'

Durable Execution

One of the most powerful features of a stateful, graph-based system is its durability. Production AI systems can't afford to lose their progress if an API call fails or the server restarts. LangGraph solves this with A checkpoint saves the current state of the graph after each step. If the workflow is interrupted, it can resume from the last saved state without starting over.

This isn't just for crash recovery. Checkpointing enables 'time travel' debugging. You can inspect the full state of your system at any point in its execution history. This is incredibly valuable for understanding why an agent made a particular decision or took a wrong turn. You can rewind, examine the state, and diagnose the issue with perfect information.

LangGraph is a graph‑based orchestration framework designed for building stateful, multi‑agent AI systems.

By moving from simple, stateless chains to stateful graphs, we build AI systems that are more powerful, reliable, and inspectable. This approach is fundamental for creating agents that can tackle complex, multi-step problems in the real world.

Quiz Questions 1/6

What is the primary limitation of a simple, stateless AI model when dealing with complex, multi-step tasks?

Quiz Questions 2/6

In the LangGraph framework, what is the fundamental structure used to represent an AI workflow?