By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Build Harbor Support Graph, a LangGraph application that answers order questions, routes refund intents to an approval path, and never executes a write without a human decision. Part 1 is the structural half: the state schema, the nodes, the tools, the routing and the keyless tests. No model key is needed — a deterministic plan node stands in for the model so the graph can be built and tested before a provider is involved.
TypedDict with messages (Annotated[list, add_messages]), order_id, intent, approved and attempts.lookup_order(order_id) is read-only and validates the order exists. request_refund(order_id, amount) records a proposal and never moves money.intake extracts the order id and intent from the latest message; plan is a deterministic stand-in for the model and chooses one tool call; draft_reply answers only from the last message.START → intake → plan, a conditional edge from plan, a ToolNode for the read-only tools, then draft_reply → END.from typing import Annotated, Literal, TypedDict
from langchain_core.messages import AIMessage, ToolCall
from langchain_core.tools import tool
from langgraph.graph import END, START, StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_condition
ORDERS = {"4521": {"status": "delivered", "items": ["ceramic mug"]}}
@tool
def lookup_order(order_id: str) -> str:
"""Read-only: return an order's status and items."""
order = ORDERS.get(order_id)
if order is None:
raise ValueError(f"Unknown order: {order_id}")
return f"{order_id}: {order['status']}, {', '.join(order['items'])}"
@tool
def request_refund(order_id: str, amount: float) -> str:
"""Propose a refund. This never moves money; it records a request."""
if order_id not in ORDERS:
raise ValueError(f"Unknown order: {order_id}")
return f"pending refund of {amount:.2f} for order {order_id}"
class SupportState(TypedDict):
messages: Annotated[list, add_messages]
order_id: str | None
intent: str | None
approved: bool | None
attempts: int
def intake(state: SupportState) -> dict:
"""Extract the order id, then the intent, from the latest message."""
text = state["messages"][-1].text.lower()
order_id = next(
(
word.strip(".,!?#")
for word in text.split()
if word.strip(".,!?#").isdigit()
),
None,
)
intent = "refund" if "refund" in text else "order_status"
return {"order_id": order_id, "intent": intent}
def plan(state: SupportState) -> dict:
"""Deterministic stand-in for the model: choose one tool call."""
if state["intent"] == "refund":
call = ToolCall(
name="request_refund",
args={"order_id": state["order_id"], "amount": 84.0},
id="call_refund",
)
else:
call = ToolCall(
name="lookup_order",
args={"order_id": state["order_id"]},
id="call_lookup",
)
return {"messages": [AIMessage(content="", tool_calls=[call])]}
def draft_reply(state: SupportState) -> dict:
"""Answer using only what the tools actually returned."""
last_result = state["messages"][-1].text
return {"messages": [AIMessage(content=f"Update: {last_result}")]}
builder = StateGraph(SupportState)
builder.add_node("intake", intake)
builder.add_node("plan", plan)
builder.add_node("tools", ToolNode([lookup_order, request_refund]))
builder.add_node("draft_reply", draft_reply)
builder.add_edge(START, "intake")
builder.add_edge("intake", "plan")
builder.add_conditional_edges("plan", tools_condition)
builder.add_edge("tools", "draft_reply")
builder.add_edge("draft_reply", END)
graph = builder.compile()from support_graph import graph
INPUTS = {
"messages": [{"role": "user", "content": "Where is order 4521?"}],
"order_id": None,
"intent": None,
"approved": None,
"attempts": 0,
}
def test_compiled_graph_has_the_expected_nodes():
assert {"intake", "plan", "tools", "draft_reply"} <= set(
graph.get_graph().nodes
)
def test_order_question_runs_the_lookup_tool():
result = graph.invoke(INPUTS)
tool_messages = [
message for message in result["messages"] if message.type == "tool"
]
assert tool_messages and "delivered" in tool_messages[0].text
assert "delivered" in result["messages"][-1].text
def test_refund_request_is_queued_not_executed():
inputs = dict(INPUTS)
inputs["messages"] = [
{"role": "user", "content": "Please refund order 4521"}
]
result = graph.invoke(inputs)
tool_messages = [
message for message in result["messages"] if message.type == "tool"
]
assert "pending refund" in tool_messages[0].text
assert "pending refund" in result["messages"][-1].textadd_messages, plus order_id, intent, approved and attempts.intake is the only node that parses the customer message, and draft_reply is the only node that writes the final answer.get_graph().nodes contains every node you expect, and pytest -q is green with no key.pytest -q passes with no environment variables set. You can draw the graph on a whiteboard from memory and point to where an order lookup and a refund proposal each go. The refund tool is reachable from the graph, but nothing in part 1 writes to a real order — that is part 2's approval path.
Key takeaways
Part 1 is the structural half: schema, nodes, tools, routing and keyless tests.
A deterministic plan node stands in for the model so the graph can be built before a provider exists.
Read-only lookups may run freely; a refund is a proposal that part 2 will gate behind an interrupt.
Part 1 is done when structure and path tests pass with no API key.
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.