Build a production-grade enterprise refund and cancellation agent using LangGraph that replaces fragile prompt loops with deterministic state transitions, idempotency locks, and compensating rollback handlers.
~150 minutes hands-on · FDE Track, Advanced Engineering difficulty.
An e-commerce client has suffered duplicate payouts because an autonomous agent retried failed HTTP calls. You will construct a stateful LangGraph agent that guarantees: (1) actions over ₹2,000 pause for human approval, (2) all actions carry idempotency keys, and (3) partial failures trigger automated compensating rollback transactions.
agent_graph.py: Compiled LangGraph state machine with conditional routing.tools.py: Pydantic-validated tool schemas with idempotency keys.saga_rollback.py: Compensating transaction stack restoring state upon failure.Install Dependencies:
pip install langgraph==0.1.4 pydantic==2.8.2
Define the Typed State Machine:
Model nodes for IntentClassifier, HumanApprovalGate, ReserveInventory, ProcessPayment, and RollbackHandler.
Wire Compensating Actions:
In saga_rollback.py, register an undo closure whenever a state mutation succeeds. If any subsequent node throws an unhandled exception, pop and execute the undo closures in reverse order.
Verify It Works:
Simulate a network timeout during the payment step. Verify that the agent automatically invokes release_inventory() and leaves the database in a clean, consistent state.