LangChain LangGraph Basics

Chains run in a straight line and simple agents run a fixed loop. Many real applications need more control: repeat a step until a quality check passes, branch based on user intent, pause for a human, or resume after a crash. LangGraph gives you that control. It models an application as a graph, which is a map of steps connected by arrows.

Subway Map Analogy

A subway map shows stations and the lines that link them. A passenger starts at one station, follows the lines, switches trains at junctions, and reaches a destination. A LangGraph app works the same way: stations are steps, lines are connections, and the passenger is your data.

 START → [Understand request] → [Search notes] → [Write answer] → END

Why Not Just Use a Chain

  • A chain cannot loop back to an earlier step.
  • A chain has no built-in way to pause and resume.
  • A chain keeps no shared state between steps beyond the values passed along.
  • Complex decisions turn into tangled code.

A graph handles all four needs with a clear structure.

The Three Building Blocks

State

State is the shared notebook that every step can read and write. It holds the user question, intermediate results, and the final answer.

Nodes

A node is a Python function that receives the state and returns updates. Each node does one job.

Edges

An edge connects one node to the next. Edges decide the order of work.

        STATE (shared notebook)
   +----------------------------+
   | question | notes | answer  |
   +----------------------------+
        ↑  ↓         ↑  ↓
     [Node A] ──edge──→ [Node B] ──edge──→ [Node C]

Installation

pip install langgraph

Your First Graph

This graph holds two steps. The first step writes a greeting, and the second step makes it shout.

from typing import TypedDict
from langgraph.graph import StateGraph, START, END

class State(TypedDict):
    name: str
    message: str

def greet(state: State):
    return {"message": "Hello, " + state["name"]}

def shout(state: State):
    return {"message": state["message"].upper() + "!"}

builder = StateGraph(State)
builder.add_node("greet", greet)
builder.add_node("shout", shout)

builder.add_edge(START, "greet")
builder.add_edge("greet", "shout")
builder.add_edge("shout", END)

graph = builder.compile()

result = graph.invoke({"name": "Asha"})
print(result["message"])   # HELLO, ASHA!

Reading the Code

  • StateGraph(State) starts a graph that follows your state shape.
  • add_node registers a function under a name.
  • add_edge draws an arrow between two nodes.
  • START and END mark where the run begins and finishes.
  • compile checks the structure and produces a runnable graph.

A Node Returns Only the Changes

A node does not return the whole state. It returns a dictionary with the keys it wants to update. LangGraph merges those updates into the state and leaves other keys untouched.

Using a Model Inside a Node

def answer(state: State):
    reply = model.invoke(state["question"])
    return {"answer": reply.content}

Any LangChain component fits inside a node: models, retrievers, chains, and tools. LangGraph orchestrates them and LangChain supplies the parts.

Seeing the Graph

Draw the structure to check your design.

print(graph.get_graph().draw_mermaid())

The output is a Mermaid diagram that renders in many documentation tools.

When to Pick LangGraph

  • The workflow loops, branches, or retries.
  • A human needs to approve steps.
  • Several agents must cooperate.
  • Long tasks must survive restarts.

Key Points

  • LangGraph models an app as a graph of nodes and edges.
  • State is a shared notebook that nodes read and update.
  • Nodes are plain functions that return state changes.
  • Edges set the order, and START and END mark the boundaries.
  • compile turns the design into a runnable graph.