By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Build the Harbor Helper data layer, tools and guardrails for the Harbor Supply store. This part is pure Python on purpose: no model call, no key. When these pieces and their tests are solid, part 2 wires them into an agent. The data is deliberately tiny, so your time goes into tool design, validation and the approval path.
ORDERS fixture with status, items and total, plus the policy text the agent may quote.lookup_order(order_id) and refund_policy(reason), each with a docstring, type hints and explicit validation errors.create_replacement(order_id, item_id) exists for the approved executor only; it must refuse to run from the agent's normal tool path.propose_write(...) returns a pending record with the exact tool, arguments and an idempotency key; it never executes the write.pytest and no key.from dataclasses import dataclass
from langchain.tools import tool
ORDERS = {
"4521": {"status": "delivered", "items": ["ceramic mug"], "total": 84.0},
}
WRITE_TOOLS = {"create_replacement"}
@tool
def lookup_order(order_id: str) -> dict:
"""Read-only: look up an order by id.
Args:
order_id: Numeric order id, for example "4521".
"""
order = ORDERS.get(order_id)
if order is None:
raise ValueError(f"Unknown order: {order_id}")
return {"order_id": order_id, **order}
@tool
def refund_policy(reason: str) -> str:
"""Read-only: return the refund rule for a damage reason."""
return "Damaged on arrival: refund or replacement within 30 days."
@tool
def create_replacement(order_id: str, item_id: str) -> str:
"""Write action: only the approved executor may call this."""
raise RuntimeError("create_replacement must run through the approval queue")
@dataclass
class PendingWrite:
"""A write action waiting for a human decision."""
tool: str
args: dict
idempotency_key: str
approved: bool = False
def propose_write(tool_name: str, args: dict, idempotency_key: str) -> PendingWrite:
"""Queue a write for a human; this function never executes it."""
if tool_name not in WRITE_TOOLS:
raise ValueError(f"{tool_name!r} is not a gated write tool")
if args.get("order_id") not in ORDERS:
raise ValueError(f"Unknown order: {args.get('order_id')}")
return PendingWrite(tool=tool_name, args=args, idempotency_key=idempotency_key)
def execute_approved(pending: PendingWrite) -> str:
"""The only code path allowed to perform the write."""
if not pending.approved:
raise PermissionError("write requires a human approval")
return f"replacement queued for order {pending.args['order_id']}"import pytest
from support_agent.harbor import execute_approved, lookup_order, propose_write
def test_unknown_order_raises():
with pytest.raises(ValueError):
lookup_order.invoke({"order_id": "not-an-order"})
def test_write_cannot_execute_without_approval():
pending = propose_write(
"create_replacement",
{"order_id": "4521", "item_id": "mug"},
idempotency_key="ticket-4521-replacement-1",
)
with pytest.raises(PermissionError):
execute_approved(pending)
def test_approved_write_reports_the_order():
pending = propose_write(
"create_replacement",
{"order_id": "4521", "item_id": "mug"},
idempotency_key="ticket-4521-replacement-1",
)
pending.approved = True
assert execute_approved(pending) == "replacement queued for order 4521"pytest -q is green with no environment variables set. You can point at the write tool and explain, in one sentence, why the agent cannot execute it directly. The approval record would be enough for a human reviewer to approve or reject without reading the transcript — exact tool, exact arguments, one idempotency key.
Key takeaways
Build the data layer, tools and guardrails before any model call.
Read-only tools may be model-callable; write tools belong behind an approval queue.
A pending write must carry the exact tool, arguments and an idempotency key.
Part 1 is done when keyless tests prove validation and the approval refusal.
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.