Skip to content

Md-Emon-Hasan/LangGraph

Repository files navigation

LangGraph

Image

1. What is LangGraph?

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.

2. Why Use LangGraph?

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.

3. When to Use LangGraph?

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.

4. Key Advantages

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.


5. End-to-End Documentation

5.1 Installation

pip install langgraph

5.2 Core Concepts

  • 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.

5.3 Basic Example: Linear Workflow

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'}

5.4 Advanced Example: Conditional Workflow

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'}

5.5 Using with LangChain

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"])

6. Best Practices

  • 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.

7. Conclusion

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.

8. Contact Me


About

LangGraph is a powerful framework built on LangChain that enables the creation of stateful, multi-step, and agentic workflows using directed graphs. It simplifies complex LLM orchestration by allowing conditional branching, memory, and tool integrations in a visual and modular way.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors