AI Hints
Home›LangGraph Tutorial›State, Nodes and Edges
LangGraph · Lesson 5 of 120

Nodes

Functions that update state.

⏱ 2 min read▶ Video at 4:23🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

A node is just a Python function. It receives the current state, does some work, and returns a dictionary of updates. Nothing more. That makes nodes easy to write, and easy to test.

Nodes
From the video: Nodes
  • A Python function
  • State in, updates out
  • Easy to test

Nodes are plain functions

Here are two nodes for an order. One applies a member discount. The other adds delivery, which is free over twenty five pounds. Because nodes are plain functions, we can call them directly, without any graph. Run it. Each function returns just its update.

nodes.pypython
def apply_discount(state):
    if state["member"]:
        return {"total": round(state["total"] * 0.9, 2)}
    return {}

def add_delivery(state):
    fee = 0 if state["total"] > 25 else 2.99
    return {"delivery": fee}

state = {"total": 30.0, "member": True}
print(apply_discount(state))
print(add_delivery({"total": 20.0}))
print(apply_discount({"total": 20.0, "member": False}))
Output
{'total': 27.0}
{'delivery': 2.99}
{}

Node rules

A node takes the state, and returns a dictionary. Return only the keys you change. Returning an empty dictionary means, no change. Keep each node to one job, and give it a name that says what it does.

keyvalue
Inputthe state
Outputa dict of updates
Empty dictno change
One jobclear name

Summary

Nodes are plain functions that return updates. You can test them without the graph. Next, the edges that connect them.

Key takeaways
  • Function(state)
  • Returns updates
  • Test directly
Next up: Edges — what runs next