TypedDict for agent state

Welcome! I'm Param, and in this course we are going to build a long-running, stateful agent with LangGraph. The demo is a flight booking assistant, but the patterns apply to any multi-turn flow: support triage, onboarding, scheduling, order management. What matters is the architecture, not the domain.

The moment an agent has to hold a conversation across more than one turn, state becomes the hardest problem. Where did we stop? What did the user already say? What still needs to be collected? LangGraph answers this by giving you a typed shared state that flows through every node in a graph.

Graph with shared state

Every node reads and writes the same TypedDict. Edges decide which node runs next.

booking_graph.py
python
from typing import TypedDict, Optional, List, Dict, Any


class BookingState(TypedDict, total=False):
    """Shared state passed between every node in the graph."""
    # Conversation
    messages: List[Dict[str, str]]       # [{role, content}, ...]
    user_message: str
    thread_id: str

    # Slot-filled fields
    origin: Optional[str]
    destination: Optional[str]
    depart_date: Optional[str]
    return_date: Optional[str]
    passengers: Optional[int]

    # Agent outputs
    candidates: List[Dict[str, Any]]
    chosen_flight: Optional[Dict[str, Any]]
    booking_id: Optional[str]

    # Control
    stage: str
    assistant_reply: str

A single TypedDict carries the full conversation: the transcript, the currently-known slots, any candidates the catalog returned, and the current stage. total=False means every field is optional, which lets each node write only the fields it cares about.

LangGraph is designed around TypedDict. It treats state as a plain dict at runtime, which keeps serialization cheap, makes checkpointing trivial, and lets you merge partial updates from different nodes without validation overhead on every transition. Pydantic models are great at API boundaries, but inside the graph you want something lightweight that the checkpointer can round-trip as JSON.

Quiz: Quiz

Loading practice…