What is LangGraph (original) (raw)

Last Updated : 14 Apr, 2026

LangGraph is an open-source framework from LangChain designed to build and manage AI agent workflows using graph-based structures. It allows developers to define workflows as nodes and edges, making complex agent interactions more structured, scalable and easier to control.

langchain_vs_langgraph_pipelines

Workflow Pipelines

LangChain uses a straightforward step-by-step pipeline where each task follows a fixed sequence from input to output while LangGraph allows flexible workflows with branching and parallel steps, making it better suited for complex, decision-based applications.

Workflow of LangGraph

The diagram below shows how LangGraph structures its agent-based workflow using distinct tools and stages.

what_is_langgraph_

Workflow of LangGraph

Here's a step by step interpretation of the flow:

  1. **Start: The process begins with the agent (Assistant) initiating an interaction or task.
  2. **Assistant: This is the central node managing the overall workflow. It controls the movement between tools and sequences based on the current state.
  3. **Enter Write Sequence: If the task requires writing assistance like generating content, the workflow enters a dedicated writing sequence.
  4. **Write Assistant: This specialized module focuses on the writing process. It may loop with tools for refining or editing before completing the sequence.
  5. **Leave Write Sequence: Once the writing task is complete the system exits the write mode.
  6. **Writer Sensitive Tools and Assistant Tools: These nodes provide specialized capabilities. Depending on the state the Assistant routes tasks to tools that enhance writing or perform sensitive operations.
  7. **End: The process concludes once the desired outcome is achieved and all necessary tools have been executed.

Components

These core components work together smoothly to help developers build, customize and manage complex AI driven workflows.

How LangGraph Scales

Building a Simple Chatbot with LangGraph

LangGraph makes it easy to build structured, stateful applications like chatbots. In this example we’ll learn how to create a basic chatbot that can classify user input as either a greet, search query and respond accordingly.

Step 1: Install the Dependencies

Installs the required dependencies,

!pip install langgraph langchain google-generativeai

`

Step 2: Setup Gemini API

To know how to access Gemini API refer to : How to Access and Use Google Gemini API Key (with Examples)

Python `

import google.generativeai as genai

genai.configure(api_key="YOUR_API_KEY")

model = genai.GenerativeModel("gemini-1.5-flash")

def ask_gemini(prompt: str) -> str: try: response = model.generate_content(prompt) return response.text except Exception as e: return "Sorry, something went wrong with the Gemini API."

`

Step 3: Define Chatbot State

We will import Optional and TypedDict for strict type checking and creates a GraphState type:

from typing import Optional from typing_extensions import TypedDict

class GraphState(TypedDict): question: Optional[str] classification: Optional[str] response: Optional[str]

`

Step 4: Classify Input

We define classify, which takes the workflow state and analyzes the user's question.

def classify(state: GraphState) -> GraphState: question = state.get("question", "").lower() if any(word in question for word in ["hello", "hi", "hey", "good morning", "good evening"]): classification = "greeting" else: classification = "search"

return {
    **state,
    "classification": classification
}

`

Step 5: Respond Using Gemini (or Greeting)

This define respond which generates appropriate output based on classification.

def respond(state: GraphState) -> GraphState: classification = state.get("classification") question = state.get("question")

if classification == "greeting":
    response = "Hello! How can I help you today?"
elif classification == "search":
    response = ask_gemini(question)
else:
    response = "I'm not sure how to respond to that."

return {
    **state,
    "response": response
}

`

Step 6: Build LangGraph Workflow

import networkx as nx import matplotlib.pyplot as plt from langgraph.graph import StateGraph

builder = StateGraph(GraphState) builder.add_node("classify", classify) builder.add_node("respond", respond) builder.set_entry_point("classify") builder.add_edge("classify", "respond") builder.set_finish_point("respond") app = builder.compile()

def visualize_workflow(builder): G = nx.DiGraph()

for node in builder.nodes:
    G.add_node(node)
for edge in builder.edges:
    G.add_edge(edge[0], edge[1])

pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, node_size=3000,
        node_color="skyblue", font_size=12, font_weight="bold", arrows=True)

plt.title("Langchain Workflow Visualization")
plt.show()

visualize_workflow(builder)

`

**Output:

LangGraph

Workflow of chatbot

Step 7: Interactive Chat Interface

print("=== Gemini-Powered Chatbot ===") print("Type your question below. Type 'exit' to quit.\n")

while True: user_input = input("You: ") if user_input.strip().lower() in ['exit', 'quit']: print("Bot: Goodbye!") break

state = {"question": user_input}
result = app.invoke(state)
print("Bot:", result["response"])

`

**Output:

langgraph-output

Output

We can see that our chatbot is working fine giving accurate results.

LangGraph vs LangChain Agents

Here a quick difference between LangGraph and LangChain Agents as they are quite similar and confusing:

Features LangGraph LangChain
Architecture Graph-based (nodes and edges with memory and branching). Sequential decision-act loop.
Workflow Control Fully customizable paths, loops and conditions. Limited control, follows predefined tool-usage cycle.
State Management Built-in persistent state across the entire graph. Implicit or external memory required.
Support for Loops Yes, supports cyclical flows and iteration. Not designed for loops or retries.
Human-in-the-Loop Built-in support for pausing and resuming with human input. Requires custom implementation.
Debugging and Observability High observability with tools like LangSmith. Limited transparency, harder to debug.

Applications