Nodes, Edges and State
Define a TypedDict state, write node functions that mutate it, and connect them with directed edges.
Nodes, Edges and State is a free AI Agents lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The State Object
Every LangGraph agent has a typed state — usually a TypedDict:
from typing import TypedDict
from typing_extensions import Annotated
import operator
class AgentState(TypedDict):
messages: Annotated[list, operator.add] # appends instead of replaces
plan: str
answer: strHow State Updates Work
Each node returns a partial dict. LangGraph:
- Reads current state
- Calls the node with it
- Merges the returned partial dict back
For list fields with operator.add, new items append instead of overwriting.
Defining a Node
def planner(state: AgentState) -> dict:
prompt = f'User wants: {state["messages"][-1].content}'
plan = llm.invoke(prompt).content
return {'plan': plan}Registering the Node
from langgraph.graph import StateGraph, END
graph = StateGraph(AgentState)
graph.add_node('planner', planner)Defining Edges
Static edges fire unconditionally:
graph.set_entry_point('planner')
graph.add_edge('planner', 'researcher')
graph.add_edge('researcher', 'writer')
graph.add_edge('writer', END)Conditional Edges
For branching, register a function that picks the next node:
def route(state: AgentState) -> str:
if state['needs_research']:
return 'researcher'
return 'writer'
graph.add_conditional_edges(
'planner',
route,
{'researcher': 'researcher', 'writer': 'writer'}
)END Is Special
END is the terminal node. Any edge to END terminates the graph and returns the current state.
Compiling the Graph
app = graph.compile()
result = app.invoke({'messages': [...]})
print(result['answer'])Streaming State Updates
Watch state change in real time:
for event in app.stream({'messages': [...]}):
print(event)
# Each event is the state diff from the just-completed node.Reducer Functions
For complex state merges, use a custom reducer:
def merge_facts(old, new):
return {**old, **new}
class State(TypedDict):
facts: Annotated[dict, merge_facts]Subgraphs
A node can itself be another compiled graph — handy for hierarchical agents:
subgraph = StateGraph(SubState).compile()
graph.add_node('subprocess', subgraph)Async Nodes
Nodes can be async functions:
async def fetch_node(state):
data = await fetch_async(state['url'])
return {'data': data}
result = await app.ainvoke({...})Errors in Nodes
If a node raises, LangGraph propagates the error and stops. Catch in the node if you want graceful handling:
def safe_search(state):
try:
return {'results': search(state['query'])}
except Exception as e:
return {'error': str(e), 'results': []}State Updates
What happens when a node returns {'plan': '...'} as a dict?
Recap
Define a TypedDict state, write functions that return partial updates, add nodes and edges, compile, run. That's LangGraph.
Frequently asked questions
Is the “Nodes, Edges and State” lesson free?
Yes — the full text of “Nodes, Edges and State” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Nodes, Edges and State”?
Define a TypedDict state, write node functions that mutate it, and connect them with directed edges. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Nodes, Edges and State” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.