Assignment: LangGraph Foundations & State Management
Assignment Metadata
| Field | Description |
|---|---|
| Assignment Name | LangGraph Foundations & State Management |
| Course | LangGraph and Agentic AI |
| Project Name | langgraph-chatbot-agent |
| Estimated Time | 90 minutes |
| Framework | Python 3.10+, LangGraph, LangChain, OpenAI API |
Learning Objectives
By completing this assignment, you will be able to:
- Understand LangGraph architecture and the role of Messages in State
- Implement State Management with messages-centric pattern using TypedDict
- Build Nodes and Edges with LangChain message types
- Configure MemorySaver checkpointer for conversation persistence
- Create a complete chatbot workflow with conditional routing
Problem Description
You are tasked with building a conversational AI agent using LangGraph that can:
- Maintain conversation history across multiple turns
- Use proper message types (HumanMessage, AIMessage, SystemMessage)
- Implement conditional routing based on user intent
- Persist state using MemorySaver for multi-session conversations
Technical Requirements
Environment Setup
- Python 3.10 or higher
- Required packages:
langgraph>= 0.2.0langchain>= 0.1.0langchain-openai>= 0.1.0
API Requirements
- OpenAI API key configured as environment variable
Tasks
Task 1: State Definition (20 points)
-
Define an AgentState using TypedDict that includes:
messages: UsingAnnotated[List[AnyMessage], add_messages]patternuser_name: Context field for personalizationsession_id: Context field for tracking
-
Implement proper message handling that:
- Uses
add_messagesreducer for automatic message merging - Handles all message types (HumanMessage, AIMessage, SystemMessage)
- Uses
Task 2: Node Implementation (30 points)
-
Create a chatbot node that:
- Reads messages from state
- Adds a personalized system message if not present
- Calls the LLM with message history
- Returns new AIMessage to state
-
Create a routing node that:
- Analyzes the last user message
- Routes to appropriate handler based on intent (help, general, goodbye)
Task 3: Graph Construction (30 points)
-
Build the StateGraph with:
- Entry point set to chatbot node
- Conditional edges based on routing logic
- Proper END state configuration
-
Configure MemorySaver checkpointer for state persistence
-
Implement multi-turn conversation that maintains context across invocations
Task 4: Testing & Validation (20 points)
-
Create test conversations demonstrating:
- Multi-turn dialogue with context retention
- Different routing paths (help, general, goodbye)
- Session persistence (same thread_id across calls)
-
Debug output showing message history at each step
Submission Requirements
Required Deliverables
- Source code in Python script or Jupyter notebook
-
README.mdwith setup instructions - Screenshots showing multi-turn conversation output
- Graph visualization (using
get_graph().draw_mermaid_png())
Submission Checklist
- State uses messages-centric pattern with
add_messages - All node functions follow the correct signature
(state) -> dict - Conditional routing works correctly
- MemorySaver enables conversation persistence
- Code runs without errors
Evaluation Criteria
| Criteria | Points |
|---|---|
| State definition with messages | 20 |
| Node implementation | 30 |
| Graph construction & routing | 30 |
| Testing & validation | 15 |
| Code quality and documentation | 5 |
| Total | 100 |
Hints
tip
- Use
state["messages"][-1]to access the most recent message - The
add_messagesreducer automatically handles message deduplication - Use
config = {"configurable": {"thread_id": "unique-id"}}for session tracking - Reference the knowledge document for node function patterns