Your First LangGraph Graph: State, Nodes and Edges

JS
Jai Singh
12 Nov, 2026 4 min read 19 views

LangGraph describes a program as a graph. A shared state object holds the data, nodes are functions that read the state and return updates, and edges say which node runs next. You build the graph, compile it, and call invoke. Starting with no language model makes the mechanics clear before the model enters.

Install

pip install langgraph

Define the state

State is usually a TypedDict. Each key is a channel that nodes can update.

from typing import TypedDict

class State(TypedDict):
    text: str
    word_count: int
    shout: str

Write nodes

A node takes the current state and returns a dictionary with only the keys it wants to change. LangGraph merges that update into the state.

def count_words(state: State) -> dict:
    return {"word_count": len(state["text"].split())}

def make_shout(state: State) -> dict:
    return {"shout": state["text"].upper()}

Connect them with edges

from langgraph.graph import StateGraph, START, END

builder = StateGraph(State)
builder.add_node("count_words", count_words)
builder.add_node("make_shout", make_shout)

builder.add_edge(START, "count_words")
builder.add_edge("count_words", "make_shout")
builder.add_edge("make_shout", END)

graph = builder.compile()

result = graph.invoke({"text": "agents are just graphs"})
print(result)

The output is a dictionary with all three keys: {'text': 'agents are just graphs', 'word_count': 4, 'shout': 'AGENTS ARE JUST GRAPHS'}. START and END are special markers for the entry and exit of the graph. Nothing runs until you call compile(), which checks that the graph is valid.

Why not just call functions?

For this example plain function calls would be simpler, and that is fine. The graph form pays off when you add features that are hard to write by hand:

  • Checkpoints: save the state after each node and resume later.
  • Streaming: receive an update as each node finishes.
  • Branching and loops: choose the next node from the state.
  • Interrupts: pause for a human decision, then continue.
  • Visualization: graph.get_graph().draw_mermaid() returns a diagram definition of the structure.

Add a model node

A node can call a model just like any function. The graph does not care what is inside:

from dotenv import load_dotenv
from langchain.chat_models import init_chat_model

load_dotenv()
model = init_chat_model("anthropic:claude-sonnet-5-5")

class SummaryState(TypedDict):
    text: str
    summary: str

def summarize(state: SummaryState) -> dict:
    reply = model.invoke(f"Summarize in one sentence: {state['text']}")
    return {"summary": reply.content}

b = StateGraph(SummaryState)
b.add_node("summarize", summarize)
b.add_edge(START, "summarize")
b.add_edge("summarize", END)
print(b.compile().invoke({"text": "LangGraph models workflows as graphs."}))

Common mistakes

  • Returning the whole state from a node instead of just the changes. It works, but hides what each node does.
  • Returning a key that is not in the state schema.
  • Forgetting to connect a node to END, which gives a graph with a dead end.
  • Mutating the input state in place. Always return updates.

The next post adds branching and loops, which is where graphs begin to behave like agents.

Reducers: what happens when two nodes write the same key

By default a node's update overwrites the old value of a key. That is right for a counter or a label, but wrong for a list that should grow, such as a message history. For those keys you attach a reducer with Annotated. A reducer is a function that combines the old value and the new one. LangGraph provides add_messages for chat history, and you can write your own, such as operator.add to append lists. A later post covers this in depth; for now remember that the default is overwrite.

import operator
from typing import Annotated, TypedDict

class LogState(TypedDict):
    log: Annotated[list[str], operator.add]

def first(state: LogState) -> dict:
    return {"log": ["first ran"]}

def second(state: LogState) -> dict:
    return {"log": ["second ran"]}

With the operator.add reducer, running these two nodes in sequence leaves a log containing both entries, instead of only the last one.

Debugging a graph

Use graph.stream(input, stream_mode="updates") to watch each node's output as it happens. It shows exactly which node produced which change. If a run ends early, check that every node has an outgoing edge. If a key is missing at the end, check that some node actually returns it.

When a graph is the right tool

Use LangGraph when your program has real structure: several steps, decisions, loops or pauses. If your whole agent is one model with a few tools, create_agent already builds that graph for you, and you do not need to draw it yourself. Reach for a hand-built StateGraph when you want to control exactly which step may follow which, or when different branches need different prompts, tools or models.

Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.