LangGraph is a library built on top of LangChain designed to facilitate the creation of stateful, multi-actor applications with Large Language Models (LLMs). It enables the construction of directed graphs where nodes represent tasks or decision points, and edges define the flow of execution.
LangGraph is particularly useful for:
- Orchestrating workflows involving multiple LLM calls.
- Handling cyclic and recursive processes (e.g., agentic workflows).
- Managing state across different steps in an LLM pipeline.
LangGraph addresses key challenges in LLM-based workflows:
- State Management: Maintains context across multiple steps.
- Dynamic Control Flow: Supports loops, conditionals, and parallel execution.
- Modularity: Encourages reusable components (nodes) in workflows.
- Agentic Workflows: Ideal for autonomous agents that require back-and-forth interactions.
LangGraph is best suited for:
- Multi-step LLM workflows (e.g., retrieval-augmented generation).
- Autonomous agents (e.g., AutoGPT-style applications).
- Recursive or self-correcting processes (e.g., refining responses iteratively).
- Complex decision-making pipelines where steps depend on previous outputs.
✅ Stateful Execution – Maintains context across multiple steps.
✅ Cyclic & Recursive Workflows – Unlike DAGs, LangGraph supports loops.
✅ Modular & Reusable – Nodes can be reused across different workflows.
✅ Scalable – Works well for both simple and complex LLM pipelines.
✅ Integration with LangChain – Leverages LangChain’s existing tools and models.
pip install langgraph- Nodes: Units of work (can be a function or LangChain Runnable).
- Edges: Define transitions between nodes (conditional or unconditional).
- State: A shared data structure passed between nodes.
from langgraph.graph import Graph
# Define nodes
def node1(state):
return {"result": state["input"].upper()}
def node2(state):
return {"final_result": f"Processed: {state['result']}"}
# Build graph
workflow = Graph()
workflow.add_node("node1", node1)
workflow.add_node("node2", node2)
workflow.add_edge("node1", "node2") # node1 -> node2
workflow.set_entry_point("node1")
workflow.set_finish_point("node2")
# Compile and run
app = workflow.compile()
output = app.invoke({"input": "hello"})
print(output) # {'final_result': 'Processed: HELLO'}from langgraph.graph import Graph
from langgraph.checkpoint import MemorySaver
# Nodes
def generate(state):
return {"draft": "AI-generated text..."}
def human_review(state):
if "approve" in state["feedback"].lower():
return {"status": "approved"}
return {"status": "rejected"}
# Graph with conditions
workflow = Graph()
workflow.add_node("generate", generate)
workflow.add_node("human_review", human_review)
workflow.add_edge("generate", "human_review")
# Conditional edge
def decide_next_step(state):
if state["status"] == "approved":
return "end" # Finish
return "generate" # Retry
workflow.add_conditional_edges("human_review", decide_next_step)
workflow.set_entry_point("generate")
# Persist state (optional)
memory = MemorySaver()
app = workflow.compile(checkpointer=memory)
# Execute
output = app.invoke({"feedback": "approve"})
print(output) # {'status': 'approved'}from langgraph.graph import Graph
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
# Define LangChain components
prompt = ChatPromptTemplate.from_template("Write a short poem about {topic}.")
model = ChatOpenAI()
chain = prompt | model
# LangGraph Node
def generate_poem(state):
response = chain.invoke({"topic": state["topic"]})
return {"poem": response.content}
# Build graph
workflow = Graph()
workflow.add_node("generate_poem", generate_poem)
workflow.set_entry_point("generate_poem")
workflow.set_finish_point("generate_poem")
app = workflow.compile()
output = app.invoke({"topic": "the ocean"})
print(output["poem"])- Modularize Nodes: Keep nodes small and reusable.
- Use Checkpoints: For long-running workflows, persist state.
- Handle Errors: Implement fallback mechanisms in conditional edges.
- Optimize LLM Calls: Cache responses where possible.
LangGraph is a powerful tool for orchestrating stateful, dynamic LLM workflows. It extends LangChain by supporting cycles, conditionals, and multi-actor systems, making it ideal for autonomous agents, recursive pipelines, and complex decision-making applications.
For more details, check the official LangGraph docs.
- Email: iconicemon01@gmail.com
- WhatsApp: +8801834363533
- GitHub: Md-Emon-Hasan
- LinkedIn: Md Emon Hasan
- Facebook: Md Emon Hasan
