AI Hints
Home›LangGraph Tutorial›Testing and Debugging
LangGraph · Lesson 105 of 120

Debugging

Tracing and history.

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

When a graph gives a wrong answer, you need to see what happened inside. Three tools help. Streaming updates, to see each step. State history, to see the state at every checkpoint. And tracing, for everything at once.

Debugging
From the video: Debugging
  • Wrong answer?
  • See inside
  • Three tools

Finding where it went wrong

Here, a customer's title was misspelled, so price fails with a key error. We catch it, then use get state to see exactly where the run stopped, and what the state held. Run it. Next shows the run stopped at price, and the values show the misspelled title that caused it.

debug.pypython
from order_graph import graph

config = {"configurable": {"thread_id": "bug-1"}}
try:
    graph.invoke({"title": "dnue", "quantity": 1}, config)
except KeyError as error:
    print("failed with KeyError:", error)

snapshot = graph.get_state(config)
print("stopped before:", snapshot.next)
print("state was:", snapshot.values)
Output
failed with KeyError: 'Dnue'
stopped before: ('price',)
state was: {'title': 'Dnue', 'quantity': 1}
Also uses order_graph.py
from typing import TypedDict
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph, START, END

PRICES = {"Dune": 9.99, "Circe": 10.99}

class Order(TypedDict):
    title: str
    quantity: int
    total: float
    receipt: str

def clean(s):
    return {"title": s["title"].strip().title()}
def price(s):
    return {"total": round(PRICES[s["title"]] * s["quantity"], 2)}
def receipt(s):
    return {"receipt": f"{s['quantity']} x {s['title']} = £{s['total']}"}

b = StateGraph(Order)
b.add_sequence([clean, price, receipt])
b.add_edge(START, "clean"); b.add_edge("receipt", END)
graph = b.compile(checkpointer=InMemorySaver())

Debugging toolkit

Get state shows where a run stopped. State history shows every step before it. Streaming shows steps live. And LangSmith tracing, turned on with environment variables, shows every node, prompt, tool call, and timing, in one view.

keyvalue
get_statewhere it stopped
Historyevery step
Streaminglive view
LangSmitheverything at once

Summary

Use get state, state history, streaming, and tracing to see inside a graph, and find bugs fast. Next, saving checkpoints to disk.

Key takeaways
  • get_state after errors
  • State history
  • LangSmith tracing
Next up: Saving to Disk — SQLite checkpointer