Step Functions agents: Agents on Step Functions, Not Infinite Loops

Day 33: Agents on Step Functions, Not Infinite Loops
6 views
This guide focuses on Step Functions agents for production systems, with practical trade-offs for reliability, security, and cost. A while loop in Lambda that calls Bedrock until “done” is a token incinerator without audit. Step Functions give you timeouts, maps, human approval via task tokens, and compensations finance will accept. Day 33 replaces infinite ReAct with an explicit graph.
⚡ TL;DR: Model turns as states: plan → toolChoice → tool/approve → evaluate → succeed|compensate. Cap iterations and token budget in state. Use Standard workflows for human-in-the-loop, Express for short chats.

Step Functions agents: production guidance

text
Start → Plan → ToolChoice →
  Choice:
    needs_approval → WaitForTaskToken
    safe_tool → InvokeTool → Evaluate →
      (continue | succeed | fail_compensate)
python
for i in range(100):
    out = converse(...)
    if not out.tool_calls: break
    run_tools(out.tool_calls)

Human approval with task tokens

json
{
  "Type": "Task",
  "Resource": "arn:aws:states:::lambda:invoke.waitForTaskToken",
  "Parameters": {
    "FunctionName": "requestApproval",
    "Payload": {
      "taskToken.$": "$$.Task.Token",
      "diff.$": "$.proposedPatch",
      "sessionId.$": "$.sessionId"
    }
  },
  "TimeoutSeconds": 86400,
  "HeartbeatSeconds": 3600
}
Approval posts to Slack/Jira; Approve calls SendTaskSuccess. Heartbeats detect abandoned approvals.

Compensations and budgets

If create_resources succeeds and register_dns fails, a compensating state deletes resources. Track tokens_used and iteration in state; fail closed when budgets exceed — do not rely on the model to stop.

Production checklist

  • [ ] Max iteration + token budget in state
  • [ ] waitForTaskToken for mutating actions
  • [ ] Compensating transitions on failure paths
  • [ ] Execution history retained for replay
  • [ ] Alarms on failure rate and stuck approvals
  • [ ] Game-day: force compensate path once per quarter

Series navigation

← Day 32 · Day 34 → Last updated September 11, 2026 Related: Day 76: p99 of Agents: Queueing, Not Just Model Latency; Day 82: Voice and Realtime Agents; Day 56: Supervisor Patterns on Bedrock Agents.

Watch: 100 Days of AI on YouTube

Short videos from CheatCoders, one AI engineering topic per day.

Open the playlist on YouTube · CheatCoders on YouTube

Deep-dive PDF

Get the expanded guide for this post — extra diagrams-style checklists, failure modes, and a production walkthrough. Free when you subscribe to CheatCoders.

Already subscribed? or open the subscribe page.


Discover more from CheatCoders

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a comment

No account needed. Name and email are optional.