Interview question
Implement a bounded tool workflow with authorization and human approval
Builds a resumable tool workflow that validates model proposals, authorizes every action, requires approval for side effects, and remains idempotent under retries.
TL;DR
Builds a resumable tool workflow that validates model proposals, authorizes every action, requires approval for side effects, and remains idempotent under retries.
Workflow state, tool contracts, least privilege, authorization, approval binding, idempotency, retry recovery, and audit evidence.
Practice the problem like a real interview: restate, reason, implement, and test.
Implement a workflow that may draft and send a customer refund request. The model may propose a tool call, but application code must validate arguments, load authoritative order data, authorize the actor, and require human approval before the external side effect. The workflow must resume safely after a crash or duplicate delivery without issuing the refund twice.
Model proposes refund(order_id=812, amount=50, reason="duplicate charge")
Application loads order 812, derives refundable amount, checks actor permission, and stores a pending action
Approver sees the exact normalized action and approves action version 3
Worker executes with idempotency key workflow-44:action-3; duplicate delivery returns the same outcome.
async def handle_proposal(workflow_id: str, proposal: ToolProposal, actor: Principal):
contract = TOOL_REGISTRY.require(proposal.name)
arguments = contract.schema.validate(proposal.arguments)
order = await orders.load_for_tenant(arguments.order_id, actor.tenant_id)
authorization.require(actor, "refund:propose", order)
normalized = refund_policy.normalize(order, arguments)
return await workflows.store_pending_action(workflow_id, normalized)
async def execute_approved(action_id: str, approval: Approval):
action = await workflows.claim_if_approved(
action_id,
approved_version=approval.action_version,
approver_id=approval.actor_id,
)
if action.is_terminal:
return action.result
key = f"{action.workflow_id}:action:{action.version}"
try:
result = await payments.refund(
payment_id=action.payment_id,
amount=action.amount,
idempotency_key=key,
timeout_seconds=8,
)
except AmbiguousTimeout:
await workflows.mark_reconciliation_required(action.id)
raise
return await workflows.complete_once(action.id, result.external_id)
Each transition performs bounded indexed reads/writes plus at most one external action attempt. Step, time, and spend budgets cap workflow growth; durable history grows O(number of transitions).
Next in AI Production Scenarios & Labs: Diagnose agent loop