LangGraph is a graph-based orchestration framework for LLM agents, built by the LangChain team.
While LangChain is great for linear chains, LangGraph is for graph like AI workflows - stateful, multi-step, multi-agent, containing loops and decision-making steps.
LangChain struggles with loops, retry logic, complex branching, multi-agent coordination, long-running workflows, state persistence; LangGraph fixes all of that and thus very useful for tools, retries, approvals, and multi-agent collaboration use cases.
Core concepts:
Nodes: Node is a function that calls an LLM, uses tools, modifies shared state.
Edges (Transitions): Edges define what happens next - static (always go to X) or conditional (if/else logic).
States: State is a typed object passed between nodes. This is what enables: Memory Loops Recovery after failures Human-in-the-loop.
Multi-agent systems using LangGraph:
Each agent = node
Shared state = collaboration memory
Edges = delegation rules
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from dotenv import load_dotenv
import os
from matplotlib import pyplot as plt
import networkx as nx
load_dotenv()
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
class State(TypedDict):
product_name: str
basic_description: str
features_benefits: str
marketing_message: str
final_description: str
def generate_basic_description(state: State) -> State:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that generates product descriptions."},
{"role": "user", "content": f"Generate a basic description for the product: {state['product_name']}."}
]
)
basic_description = response.choices[0].message.content.strip()
return {"basic_description": basic_description}
def add_features_benefits(state: State) -> State:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that adds features and benefits to product descriptions."},
{"role": "user", "content": f"Based on the basic description: {state['basic_description']}, list the features and benefits of the product."}
]
)
features_benefits = response.choices[0].message.content.strip()
return {"features_benefits": features_benefits}
# state['features_benefits'] = features_benefits Either of this will work
# return state
def create_marketing_message(state: State) -> State:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that creates marketing messages for products."},
{"role": "user", "content": f"Create a marketing message based on the features and benefits: {state['features_benefits']}."}
]
)
marketing_message = response.choices[0].message.content.strip()
return {"marketing_message": marketing_message}
def polish_final_description(state: State) -> State:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that polishes product descriptions."},
{"role": "user", "content": f"Polish the following product description and marketing message into a final description:\nBasic Description: {state['basic_description']}\nMarketing Message: {state['marketing_message']}"}
]
)
final_description = response.choices[0].message.content.strip()
return {"final_description": final_description}
def build_workflow():
"""Builds the state graph workflow for generating product descriptions using LangGraph."""
workflow = StateGraph[State, None, State, State](State)
workflow.add_node("generate_basic_description", generate_basic_description)
workflow.add_node("add_features_benefits", add_features_benefits)
workflow.add_node("create_marketing_message", create_marketing_message)
workflow.add_node("polish_final_description", polish_final_description)
### Add the edges
workflow.add_edge(START, "generate_basic_description")
workflow.add_edge("generate_basic_description", "add_features_benefits")
workflow.add_edge("add_features_benefits", "create_marketing_message")
workflow.add_edge("create_marketing_message", "polish_final_description")
workflow.add_edge("polish_final_description", END)
# compile the workflow into a chain of actions
chain = workflow.compile()
return chain
product_name = "Smart Fitness Watch"
state = State(product_name=product_name, basic_description="", features_benefits="", marketing_message="", final_description="")
chain = build_workflow()
result = chain.invoke(state)
result
from IPython.display import Image, display
try:
display(Image(chain.get_graph().draw_mermaid_png()))
except Exception as e:
print(e)
Topic
=> Generate
=> Generate Review
=> Generate_
=> Combined_Output (END)