Human in the Loop for AI Agents with Step Functions
Description and use
The article demonstrates how to build a human‑in‑the‑loop approval workflow for an AI agent using AWS Step Functions and the Serverless Framework. Thresholds of €20 and €500 dictate when manual approval is required, and the task token safeguards against unauthorized execution.
Problem
AI agents that perform costly or irreversible actions need a safety mechanism to involve human approval before execution.
Outcome
The workflow ensures that only amounts below 20 € are auto‑credited, larger amounts require human approval, and any attempt to exceed 500 € is blocked. Escalation to a team lead after 4 h protects against missed approvals.
Workflow
- 1. Agent calls "request credit" tool, starting a Step Functions execution with order details.
- 2. Choice rule evaluates amount: auto‑credit up to 20 €, team approval for 20.01–500 €, or stop for >500 €.
- 3. AskTeam step invokes a Lambda with .waitForTaskToken, stores token in DynamoDB, and publishes an SNS message with approval ID.
- 4. Human approves or rejects via a signed request to a second Lambda, which retrieves the token and calls SendTaskSuccess or SendTaskFailure.
- 5. If no response within 4 h, TimeoutSeconds triggers a Catch that routes to AskLead for escalation.
- 6. If lead also does not respond, workflow ends with ApprovalExpired and no credit is booked.
Evidence
With the “wait for callback” pattern, Step Functions can wait up to a year for an answer, and the waiting itself costs nothing.
A task token is a long random string that identifies exactly one waiting step. Anyone who knows it and holds the IAM permission states:SendTaskSuccess can complete the step and pass any data as its result.
If there is no answer within four hours, TimeoutSeconds kicks in. A Catch routes to the AskLead step, which asks the team lead.
- Record date: 2026-11-03T10:43:46.168Z