AWS Builder Center
Pause the AI, Not the User: Building Human-in-the-Loop Workflows with AWS Step Functions

Pause the AI, Not the User: Building Human-in-the-Loop Workflows with AWS Step Functions

Explore how AWS Step Functions enables human-in-the-loop workflows using callback tokens. Learn to build secure, asynchronous AI approval systems with Lambda, DynamoDB, SNS, API Gateway, and Cognito.

Pause the AI, Not the User: Building Human-in-the-Loop Workflows with AWS Step Functions

Introduction

Artificial intelligence is becoming capable of handling increasingly complex tasks. From processing financial transactions to reviewing documents and generating recommendations, AI-powered applications are changing how businesses operate.
However, should every decision made by an AI system be executed automatically?
Consider an AI-powered expense management application. It can analyze receipts, detect unusual spending, and recommend whether a reimbursement should be approved. But when a reimbursement exceeds a certain amount, a human manager should have the final say.
The challenge is not simply adding an approval button. It is designing a system that can pause a process, securely wait for a decision, and resume execution without keeping the user waiting or running unnecessary infrastructure.
This is where AWS Step Functions becomes particularly useful.

1. The Architecture Behind Human Approval

A human-in-the-loop workflow introduces a human decision into an otherwise automated process.
Instead of implementing approval logic inside a single Lambda function, we can distribute responsibilities across managed AWS services.
The proposed architecture uses:
  • Amazon API Gateway: Receives requests from the application.
  • AWS Lambda: Performs initial validation and AI-related processing.
  • AWS Step Functions: Orchestrates the entire workflow.
  • Amazon DynamoDB: Stores approval requests and their associated metadata.
  • Amazon SNS: Sends notifications to the appropriate reviewer.
  • Amazon Cognito: Authenticates reviewers before they submit decisions.
The most important component is AWS Step Functions because it allows the workflow to wait for an external decision without continuously executing application code.

2. Understanding the Workflow

Let's consider an expense reimbursement application.
A user submits a reimbursement request for ₹75,000.
The workflow follows these steps:
  1. The application sends the request through API Gateway.
  1. Lambda validates the submitted information.
  1. An AI component evaluates the request and generates a recommendation.
  1. Step Functions checks whether manual approval is necessary.
  1. If approval is required, the workflow pauses and sends a notification to the manager.
  1. The manager reviews the request and approves or rejects it.
  1. The workflow resumes and executes the appropriate action.
The interesting part is step six.
The workflow does not need to repeatedly check whether the manager has responded. Instead, Step Functions can wait for an external callback.

3. The Important Feature: Wait for Task Token

AWS Step Functions provides a callback integration pattern called  waitForTaskToken .
When a task reaches this state, Step Functions generates a unique task token and passes it to the integrated service.
The workflow remains paused until an external process returns that token using either:
  •  SendTaskSuccess 
  •  SendTaskFailure 
For our approval system, the approval decision is returned through  SendTaskSuccess , with the decision included in the response.
This allows the workflow to continue without repeatedly invoking Lambda functions just to check approval status.

4. Implementation Flow

A simplified state machine can look like this:
{
"StartAt": "EvaluateRequest",
"States": {
"EvaluateRequest": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "EvaluateExpenseRequest"
},
"Next": "CheckApproval"
},
"CheckApproval": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.requiresApproval",
"BooleanEquals": true,
"Next": "WaitForManager"
}
],
"Default": "ProcessRequest"
},
"WaitForManager": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke.waitForTaskToken",
"TimeoutSeconds": 86400,
"Parameters": {
"FunctionName": "CreateApprovalRequest",
"Payload": {
"TaskToken.$": "$$.Task.Token"
}
},
"Next": "CheckDecision"
},
"CheckDecision": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.approved",
"BooleanEquals": true,
"Next": "ProcessRequest"
}
],
"Default": "RejectRequest"
},
"ProcessRequest": {
"Type": "Pass",
"End": true
},
"RejectRequest": {
"Type": "Pass",
"End": true
}
}
}
This is a simplified illustration of the workflow. Production implementations must also handle Lambda response structure, callback payloads, permissions, and timeout paths.

5. How Does the Approval Reach Step Functions?

When the workflow enters the approval state, the Lambda function receives the task token.
It then:
  1. Creates an approval record in DynamoDB.
  1. Associates the approval request ID with the task token.
  1. Sends a notification using Amazon SNS.
  1. Waits for the reviewer to take action.
The manager opens the application and submits an approval decision.
API Gateway receives the request and invokes another Lambda function.
That function authenticates the reviewer, checks that the request is still pending, retrieves the associated task token, and calls the Step Functions  SendTaskSuccess  API.
The callback returns a decision such as:
{
"approved": true,
"reviewedBy": "manager-123"
}
Step Functions receives the result and moves to the next state.

6. Security and Reliability Considerations

A working approval system must account for more than just successful execution.
Protect task tokens
Task tokens should be treated as secrets. Store them securely, restrict access through IAM, and never expose them directly in browser URLs or email notifications.
Prevent duplicate approvals
A manager might accidentally click the approval button twice. Use conditional DynamoDB updates to ensure that only one decision can change a pending request.
Handle expired approvals
A manager might never respond. Configure task timeouts and define what should happen when the approval period expires.
Authenticate reviewers
Use Amazon Cognito or another trusted identity provider to ensure that only authorized users can approve requests.
Maintain an audit trail
Record who approved or rejected a request, when the decision occurred, and what information was reviewed.
These controls help make the workflow secure, traceable, and suitable for real-world applications.

7. Where Can This Architecture Be Used?

The same design can support several business scenarios:
  • Financial applications: Require human approval for high-value transactions.
  • Healthcare platforms: Allow professionals to review AI-generated recommendations.
  • Document processing: Route sensitive documents for manual verification.
  • E-commerce: Require approval for exceptional refunds or high-value orders.
  • AI agents: Allow an agent to prepare an action while a human authorizes its execution.
This approach is particularly valuable when automation is desirable but unrestricted execution is not.

Conclusion

Building intelligent applications is not just about making AI systems more capable. It is also about deciding where automation should stop and human judgment should begin.
AWS Step Functions provides a practical way to connect automated processing with human decisions through callback-based workflows.
By combining Step Functions, Lambda, DynamoDB, SNS, API Gateway, and Cognito, developers can create approval systems that are asynchronous, secure, and easier to maintain.
The bigger engineering lesson is simple:
A good automated system should know when to act independently and when to wait for a human.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article