AI-Framework-Langgraph

Overview

LangGraph is a framework for building stateful, multi-step AI workflows and agents on top of LangChain. It is designed for complex workflows that need deterministic control and shared state.

Common scenarios LangGraph is built for:

  • Complex agent workflows
  • Loops and decision making
  • Multi-agent systems
  • Long-running AI processes
  • Human-in-the-loop systems

LangGraph models AI workflows as a graph of nodes and edges, where nodes are functions (LLM calls, tools, logic), edges are transitions, and state is shared memory passed between nodes.

Why LangGraph was created

Traditional LangChain pipelines and simple chains are linear:

Prompt → LLM → Output

Step1 → Step2 → Step3

Real AI systems often require:

LLM decides next step
Loop until condition met
Call tools
Retry on errors
Human approval
Persist state

LangGraph enables these complex workflows.

Core concept: graph-based AI workflows

LangGraph represents workflows as graphs of nodes and transitions:

        ┌─────────┐
        │  START  │
        └────┬────┘
             │
             ▼
        ┌─────────┐
        │  Agent  │
        └────┬────┘
             │
      ┌──────┴──────┐
      ▼             ▼
  ┌─────────┐  ┌─────────┐
  │  Tool   │  │  Search │
  └────┬────┘  └────┬────┘
       │             │
       └──────┬──────┘
              ▼
          ┌───────┐
          │ Final │
          └───────┘

Each block is a node.

Installing LangGraph

pip install langgraph
pip install langchain
pip install langchain-openai

Key concepts

Concept Meaning
GraphWorkflow structure
NodeTask or function
EdgeTransition between nodes
StateShared data
Conditional edgeDecision routing
STARTEntry point
ENDWorkflow completion

1. State (core component)

State is the data shared between nodes:

from typing import TypedDict

class State(TypedDict):
    question: str
    answer: str

Example state:

{
  "question": "What is LangGraph?",
  "answer": ""
}

Each node reads and updates the state.

2. Creating nodes

Nodes are just Python functions:

def process_question(state):
    question = state["question"]
    answer = f"You asked: {question}"

    return {"answer": answer}

Node behavior:

Input: state
Output: updated state

3–8. Creating and running a graph

Define the graph structure:

from langgraph.graph import StateGraph

workflow = StateGraph(State)

Add nodes and edges:

workflow.add_node("process", process_question)

workflow.add_edge("process", "end")

workflow.set_entry_point("process")

Compile and run:

app = workflow.compile()

result = app.invoke({"question": "What is LangGraph?"})

print(result)

Complete example

from typing import TypedDict
from langgraph.graph import StateGraph

class State(TypedDict):
    question: str
    answer: str

def process_question(state):
    question = state["question"]
    return {"answer": f"Answering: {question}"}

workflow = StateGraph(State)

workflow.add_node("process", process_question)

workflow.set_entry_point("process")

workflow.set_finish_point("process")

app = workflow.compile()

result = app.invoke({"question": "Explain LangGraph"})

print(result)

Using LLMs in LangGraph

Integrate an LLM (example: OpenAI):

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")

Node with an LLM call:

def llm_node(state):
    question = state["question"]

    response = llm.invoke(question)

    return {"answer": response.content}

Conditional edges (decision making)

LangGraph supports branching logic using conditional edges.

User Question
      │
      ▼
 Check Type
  ├── math → calculator
  └── text → LLM
def route_question(state):
    question = state["question"]

    if "calculate" in question:
        return "calculator"

    return "llm"

Add conditional edges:

workflow.add_conditional_edges(
    "router",
    route_question,
    {
        "calculator": "calc_node",
        "llm": "llm_node"
    }
)

Looping (agent behavior)

LangGraph supports loops, which are common in agent behavior.

Think
Act
Observe
Repeat

Example loop shape:

agent → tool → agent → tool → end

Tool integration

def calculator(state):
    expression = state["expression"]

    result = eval(expression)

    return {"result": result}

Example: simple agent graph

Structure:

START
  ↓
Agent
  ↓
Tool
  ↓
Agent
  ↓
END

Edges:

workflow.add_node("agent", agent_node)
workflow.add_node("tool", tool_node)

workflow.add_edge("agent", "tool")
workflow.add_edge("tool", "agent")

Stop conditions are handled inside the state.

Streaming execution

LangGraph supports streaming intermediate steps:

for event in app.stream({"question": "Explain AI agents"}):
    print(event)

Useful for debugging, UI streaming, and step-by-step workflows.

Persistent state

LangGraph supports checkpointing so workflows can pause, resume later, and recover from crashes. This enables patterns like human approval gates.

User request
Agent research
Pause
Human approval
Resume

Human-in-the-loop workflows

Example workflow:

AI generates answer
      ↓
Human approves
      ↓
Final output

Multi-agent systems

LangGraph makes it easier to build multiple collaborating agents.

User
  ↓
Planner Agent
  ↓
Research Agent
  ↓
Writer Agent
  ↓
Editor Agent

Production use cases

AI assistants

  • Coding copilots
  • Enterprise assistants

Autonomous research

Question
 ↓
Search
 ↓
Analyze
 ↓
Write report

Complex RAG systems

Query
 ↓
Retriever
 ↓
Reasoning
 ↓
Multiple tool calls
 ↓
Answer

Workflow automation

Email → classify → respond

LangChain vs LangGraph

Feature LangChain LangGraph
Simple chainsYesYes
AgentsBasicAdvanced
LoopsLimitedYes
Graph workflowsNoYes
Stateful systemsBasicAdvanced
Multi-agentHardEasy

How LangChain and LangGraph work together

LangGraph uses LangChain components (tools, prompts, LLMs), so it is an extension rather than a replacement.

LangGraph
   ↓
Nodes
   ↓
LangChain tools / prompts / LLMs

Typical production architecture

Frontend (React / Next.js)
        ↓
Backend API (FastAPI)
        ↓
LangGraph Workflow
        ↓
LangChain Tools
        ↓
Vector Database
        ↓
LLM Providers

Vector DB examples: Pinecone, Weaviate, Chroma.

Advantages and limitations

Advantages

  • Deterministic control
  • Supports loops
  • Complex agents
  • Persistent state
  • Human-in-the-loop
  • Scalable workflows

Limitations

  • More complex than LangChain
  • Learning curve
  • Debugging graphs can be harder

Simple mental model

LangChain = Functions
LangGraph = Workflow Engine

Or:

LangChain → AI building blocks
LangGraph → AI orchestration system

Summary

LangGraph is a graph-based orchestration framework for building advanced AI agents and workflows. It enables multi-step reasoning, looping agents, tool use, state persistence, and multi-agent collaboration, while leveraging the ecosystem of LangChain.