Agent Script Pattern: Action Chaining and Sequencing
Run multiple actions in a guaranteed sequence.
Action chaining can be implemented in multiple ways depending on when and how you need actions to execute. You can run actions sequentially in instructions, chain reasoning actions, or combine actions with transitions and conditionals.
Why Use This Pattern
Action sequencing ensures that one action can trigger another, creating reliable multi-step workflows without relying on the LLM to remember multiple steps.
Pattern Example: Get the user’s order with one action, then immediately check return eligibility for that order in another action.
Sequential Actions in Instructions
Call actions one after another in reasoning instructions. Both run deterministically before the prompt is sent to the LLM.
When you run an action in instructions, you must manually set variables for the inputs and outputs because the action is run before any reasoning takes place. See Actions Reference.
Note
Sequential Actions
1reasoning:2 instructions: ->3 # First action4 run @actions.lookup_current_order5 with member_email=@variables.member_email6 set @variables.order_summary=@outputs.order_summary78 # Next action9 run @actions.lookup_current_user10 with member_email=@variables.member_email11 set @variables.user_profile=@outputs.profile1213 | Show the user their order summary and welcome them by name.
You can also store the output of one action in a variable, then use it as the input to another action, or as part of a subsequent prompt.
Chained Actions in Reasoning Actions
Define a follow-up action that automatically runs when the LLM calls an action.
Chained Actions
1reasoning:2 instructions: ->3 | If the user wants information, use {!@actions.my_action}.45 actions:6 my_action: @actions.my_action7 with foo=@variables.Foo8 set @variables.status = @outputs.status9 run @actions.other_action10 set @variables.some_other_result=@outputs.data
Whenever the LLM calls my_action, the agent automatically runs other_action afterwards.
Run Action Then Transition
Run an action and then automatically transition to another subagent.
Action Then Transition
1reasoning:2 instructions: ->3 | Call {!@actions.validate_user_ready} to check if the user is ready.45 actions:6 validate_user_ready: @actions.validate_user_ready7 with user_id=@variables.user_id8 set @variables.is_ready=@outputs.ready9 transition to @subagent.analyze_issue
Conditional Chain
Chain actions conditionally based on the results of previous actions.
Conditional Action Chain
1reasoning:2 instructions: ->3 # First action4 run @actions.check_eligibility5 with user_id=@variables.user_id6 set @variables.is_eligible=@outputs.eligible78 # Condition9 if @variables.is_eligible == True:1011 # Conditional action12 run @actions.fetch_offer_details13 with user_id=@variables.user_id14 set @variables.offer=@outputs.offer1516 | Present the offer: {!@variables.offer}17 else:18 | Explain that the user is not eligible for this offer.
Tips
Use sequential instructions for deterministic flows: When you always want actions to run in a specific order.
Use variables for action outputs: If your second action needs an output from the first action, store the first action’s outputs in a variable and assign it as an input to the second action.