By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A support run on thread thread-c91 answers the customer with $47.00 when the order total is $42.00. Nothing crashed, no retry fired and the reply reads like a normal answer. The only record of what happened is the run's trace: one span per node and tool, each carrying the state update the node returned. Your job in this lab is to read that trace, name the node that changed state incorrectly, fix the writer rather than the symptom, and lock the fix in with a regression test.
A LangGraph trace is not a log of prose. It is a timeline of supersteps, and every node span is a small diff: the node's name, the state it saw, and the partial update it returned. That shape is what makes debugging tractable — you are looking for the one delta that contradicts the state contract, not for a message that sounds wrong.
Give every state channel an owner — the one node expected to write it — and trace review becomes a table walk. For each span ask: which keys are in this node's update, and is this node allowed to write them? A write to a read-only channel is a contract break even when the value looks plausible, because the next node now reasons from data the graph never fetched. The ownership table does not need to be a separate document; it can start as a comment next to the TypedDict and grow into a reviewed artifact.
# A trimmed run trace for thread-c91: one dict per span.
TRACE = [
{"node": "model", "kind": "llm", "update": {"intent": "refund"}},
{"node": "lookup_order", "kind": "tool", "update": {"order_total": 4200}},
{"node": "compute_total", "kind": "node", "update": {"order_total": 4700}},
{"node": "draft_reply", "kind": "node", "update": {"reply": "$47.00"}},
]
# The state contract: which node owns each channel.
OWNER = {
"intent": "model",
"order_total": "lookup_order",
"total": "compute_total",
"reply": "draft_reply",
}
def contract_breaks(trace):
"""Every write whose span is not the channel's declared owner."""
breaks = []
for span in trace:
for key in span["update"]:
if OWNER.get(key) != span["node"]:
breaks.append((span["node"], key))
return breaks
found = contract_breaks(TRACE)
print("breaks:", len(found))
print("first:", found[0])The trace names compute_total as the offender: it returned order_total, a channel owned by lookup_order. The summary node is innocent — it rendered whatever the state told it to render. That distinction is the whole lab, because patching the summariser would make the answer look right while leaving corrupted state behind for the next node, the checkpointer and every replay.
The fix belongs at the write site. compute_total may read order_total, but it may return only total. In the regression test the graph is rebuilt with deterministic stand-ins: the lookup returns a fixture total, the calculator adds a fixture shipping amount, and the renderer formats the result. Three assertions are enough to pin the contract: the read value is unchanged, the computed value is correct, and the rendered reply matches the computed value.
from typing import TypedDict
import pytest
from langgraph.graph import END, START, StateGraph
class SupportState(TypedDict):
question: str
order_total: int
total: int
reply: str
def lookup_order(state: SupportState) -> dict:
# A read-only tool: order_total is this node's channel.
return {"order_total": 4200}
def compute_total(state: SupportState) -> dict:
# The fix: return only the channel this node owns.
return {"total": state["order_total"] + 500}
def draft_reply(state: SupportState) -> dict:
return {"reply": f"${state['total'] / 100:.2f}"}
@pytest.fixture
def graph():
builder = StateGraph(SupportState)
builder.add_node("lookup_order", lookup_order)
builder.add_node("compute_total", compute_total)
builder.add_node("draft_reply", draft_reply)
builder.add_edge(START, "lookup_order")
builder.add_edge("lookup_order", "compute_total")
builder.add_edge("compute_total", "draft_reply")
builder.add_edge("draft_reply", END)
return builder.compile()
def test_lookup_total_is_not_overwritten(graph):
result = graph.invoke(
{"question": "What do I owe?", "order_total": 0, "total": 0, "reply": ""}
)
assert result["order_total"] == 4200
assert result["total"] == 4700
def test_reply_renders_the_computed_total(graph):
result = graph.invoke(
{"question": "What do I owe?", "order_total": 0, "total": 0, "reply": ""}
)
assert result["reply"] == "$47.00"
def test_compute_total_returns_only_its_owned_channel():
assert compute_total({"order_total": 4200}) == {"total": 4700}Key takeaways
A trace is a sequence of state deltas, one per node or tool span, so debugging starts by pairing each delta with the node that returned it.
Declare a channel owner in the state contract; a write by any other node is the bug, even when the resulting message looks plausible.
Fix the writer, not the renderer: the node that returned an unauthorised key is wrong, and the node that rendered the corrupted state is a symptom.
A regression test asserts what state must still be true after a run — order_total unchanged, total correct — so the bad write cannot come back unnoticed.
Keep the trace as evidence: span name, returned delta and contract line together are the bug report.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.