Human in the Loop for AI Agents with Step Functions
说明与用途
文章展示了如何使用 AWS Step Functions 和 Serverless Framework 为 AI 代理构建人机交互审批流程。20 欧元和 500 欧元的阈值决定何时需要人工批准,任务令牌可防止未授权执行。
问题
AI agents that perform costly or irreversible actions need a safety mechanism to involve human approval before execution.
结果
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.
工作流
- 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.
证据
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.
- 记录日期: 2026-11-03T10:43:46.168Z