Enforce an AI agent's next step in code
Return the next allowed conversation state from code instead of letting the AI agent choose it.
The claim
No slot is booked until a bike the shop actually services has been recorded. The rule lives in the tool handlers, which read collected state instead of asking the model whether it collected anything. The caller may well reach the scheduling step; what they cannot do is get a booking out of it.
Why it holds
valid_steps shapes the navigation tool the model is offered. It does not constrain your webhook, so the handler is the last authority on the transition. It is also not a lock, which is why the tool at the destination checks the same state again.
How it works
Two things move the conversation, and only one of them is clamped.
The model’s path is the next_step tool, bounded by valid_steps on the current step. The other path is a handler emitting a step change, and the SDK is explicit that this bypasses the clamp. Anything the handler emits happens. Neither path makes a step a boundary. A step is a place in a flow, so the tool that books the slot checks for a recorded bike whatever route reached it.
So the handler checks first:
def start_scheduling(self, args, raw_data):
recorded = (raw_data or {}).get("global_data", {}).get("bike_type")
if recorded not in SERVICEABLE:
return FunctionResult(
"NOT_READY: no serviceable bike has been recorded yet. Ask "
"what kind of bike it is and call record_bike first."
)
return FunctionResult(...).swml_change_step("schedule")The model may call start_scheduling whenever it likes, including on the first turn. What it cannot do is make the check pass. SERVICEABLE is a set in code, not a line in the prompt, so a caller who insists cannot widen it.
The refusal is prescriptive. It names the state that is missing and the tool that fills it, so the model’s next turn is a question rather than an apology.
One naming trap: swml_change_step() is the SDK method, and the key the platform receives is change_step. Assert the key.
Limitations
This governs one transition. A flow with several gates wants the check factored out, because a rule copied into four handlers is a rule that will disagree with itself.
Reading global_data from raw_data trusts the platform’s own state, not the model’s account of it. That is the point, but it means the value has to have been written by a handler.
What to change first
Delete the recorded not in SERVICEABLE check and keep the prompt instruction. The transition becomes advisory, which is the failure this recipe exists to prevent.
Where this sits
Seen in a build
One of 4 recipes composed by Absence reporting line: the caller gives details in any order and code decides when the form is complete.
Seen in a build
One of 6 recipes composed by After-hours AI answering service: code decides whether the AI is on the call at all.
Seen in a build
One of 6 recipes composed by AI appointment reminder caller: consent, do-not-call and calling hours enforced before the dial.
Seen in a build
One of 5 recipes composed by AI bank balance line: account tools that do not exist until a handler verifies the caller.
Seen in a build
One of 5 recipes composed by AI collections agent: a payment policy the model cannot reveal or bargain over.
Seen in a build
One of 8 recipes composed by AI drive-thru ordering: a voice agent whose flow your code controls.
Seen in a build
One of 5 recipes composed by AI hiring phone screen: a voice form the candidate cannot reorder or skip.
Seen in a build
One of 6 recipes composed by AI insurance claim intake: the adjuster is briefed and accepts before the bridge completes.
Seen in a build
One of 4 recipes composed by AI lead qualification line: a lead scored from typed handler data, not the transcript.
Seen in a build
One of 6 recipes composed by AI medical appointment line: recording that starts only in the handler that records consent.
Seen in a build
One of 5 recipes composed by AI order status line: the handler holds the full record and the model sees a curated slice.
Seen in a build
One of 5 recipes composed by Give Claude a phone number: an AI phone line whose tools are also an MCP server.
Seen in a build
One of 4 recipes composed by AI restaurant reservation line: a booking the model cannot confirm until code has made it.
Seen in a build
One of 6 recipes composed by AI SMS concierge: one agent definition answering texts and calls with the same rules.
Seen in a build
One of 6 recipes composed by AI support line with callback: the issue survives the queue and the return call without the model carrying it.
Seen in a build
One of 5 recipes composed by AI travel concierge: two agents handing a caller back and forth with context and no transcript.
Seen in a build
One of 4 recipes composed by AI trivia hotline: a game graded in code with an answer key the model never sees.
Seen in a build
One of 5 recipes composed by AI utility bill payment line: card digits that never reach the model, the recording or the logs.
Seen in a build
One of 4 recipes composed by AI website chat agent: the phone agent in a chat bubble, with the API token kept off the page.
Seen in a build
One of 8 recipes composed by Appointment confirmation by SMS: a text built by the handler and a reply that calls the customer back.
Seen in a build
One of 5 recipes composed by Bilingual AI support line: English and Spanish from one code path, switching mid-sentence.
Seen in a build
One of 7 recipes composed by IVR navigator: an AI agent that works through phone menus to reach a human.
Seen in a build
One of 7 recipes composed by Video blackjack dealer: a voice AI agent that controls the browser.