State Machines for Tool Workflows
About 40 min
Goal
By the end of this lesson, you can represent a multi-step tool workflow as explicit states and transitions and detect invalid moves, loops, and terminal outcomes.
A board game makes some moves legal only from certain positions. If your piece is still at START, you cannot claim the reward reserved for FINISH just because someone writes "finished" on a card. The game board and transition rules determine which moves are valid.
A state machine gives a software workflow that same explicit structure. A state says where the workflow currently is, an event proposes what happened next, and a transition rule decides whether that move is legal. A guard is an extra condition on an otherwise possible move—for example, approval must match the current order and refund amount before the workflow may enter an approved state.
This explicit structure matters as soon as a workflow can call more than one tool, because a free-form “model decides the next step” loop is difficult to audit.
State answers “where are we now?”
For a refund workflow, states might be:
START
ORDER_LOADED
ELIGIBILITY_CHECKED
AWAITING_APPROVAL
REFUND_REQUESTED
DONE
FAILED
An event moves the workflow between states only when a defined transition allows it.
For example:
START + order_loaded → ORDER_LOADED
ORDER_LOADED + eligible → ELIGIBILITY_CHECKED
ELIGIBILITY_CHECKED + approval_required → AWAITING_APPROVAL
The model can help choose or describe an event, but application code owns the transition table.
Invalid transitions should fail closed
If the workflow is in START, a direct refund_succeeded event should not jump to DONE. That trace skipped the lookup, eligibility check, and approval boundary.
Rejecting impossible transitions protects both reliability and security.
State carries trusted workflow facts
State can include trusted fields such as:
order_id
authenticated_user_id
approval_status
attempt_count
last_tool_result_id
Do not rebuild these solely from model prose on every turn. The application should maintain durable structured state.
Terminal states stop the loop
A workflow needs explicit stop conditions. DONE and FAILED should not keep asking the model what to do next.
A maximum-step guard is also useful when unexpected transitions repeat without reaching a terminal state.
State machines make tests concrete
Instead of testing only final text, you can test:
- which transitions are permitted;
- which state requires approval;
- which events increment retry counters;
- which tool can run in each state;
- whether terminal states reject further actions.
These checks remain deterministic even when model suggestions vary.
Guards add conditions to otherwise valid transitions
Sometimes the same event is legal only when a trusted condition holds. For example, approved may move AWAITING_APPROVAL to APPROVED only if the stored approval matches the current refund amount and order ID. That condition is a guard on the transition.
Keeping guards explicit prevents a state name from becoming misleading. A workflow should not enter APPROVED merely because the model emitted the word “approved.” The transition function should read the trusted approval record and reject mismatches.