1. Core LangGraph Concepts
LangGraph models agents as state machines. There are four fundamental concepts:- State: The central database or memory structure shared across the graph. Any node can read variables from this state and output key-value updates to write back to it.
- Nodes: Python functions representing independent operations or agents. A node receives the current state, processes it (e.g., calls an LLM or runs a tool), and returns a dictionary with state updates.
- Edges: Control flow rules. Normal Edges define a fixed sequential route (e.g. from node A to node B). Conditional Edges use a router function to dynamically decide which node to visit next based on state data.
- Checkpointers (Memory): Databases that automatically save a snapshot of the graph’s state after every node execution. This allows the graph to resume or remember conversations across separate API requests.
2. Available APIs & Key Classes
To build stateful agents, LangGraph provides the following key classes and decorators:StateGraph: The primary builder class used to construct graphs. It takes a typed dictionary (schema) defining the structure of the state.add_messages: An accumulator reducer function. In LangGraph, returning a key updates the state. For lists of messages, we don’t want to overwrite previous history. UsingAnnotated[list, add_messages]tells the graph to append new messages instead of replacing the list.MemorySaver: A built-in, in-memory checkpointer class that preserves thread states.thread_id: A config key passed during graph execution. Any queries sharing the samethread_idshare the same conversation history in the checkpointer.
3. Practical Step-by-Step Implementation
Step 1: Define the Graph State Schema
We define a Python class inheriting fromTypedDict containing the keys that will be shared between our agents:
Step 2: Create a Verification Tool
We write a tool that the analyzer agent can invoke to cross-check candidate skills:Step 3: Define Agent Node Functions
Nodes receive the shared state, call the LLM, and return updates:Step 4: Assemble Graph Nodes & Edges
We add nodes and map the route sequentially:Step 5: Compile with Checkpointer Memory
We attachMemorySaver to compile our final application graph:
4. Combined Running Code Project
Here is the complete, self-contained Python script ready to copy and run:5. Practice Exercises
Practice 1: Adding a Feedback Evaluation Node
Extend the stateful graph by adding a third node calledfeedback_evaluator that runs after the planner node. The node should format a short summary checklist of topics to check off during the interview.
Instructions:
- Update
AgentStateto include aneval_checklistkey. - Define a function
feedback_evaluator_node(state: AgentState)that takes theinterview_planand creates a checklist. - Add the node to the graph and update the edges:
plannerevaluatorEND.
Solution
Solution