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 action
4    run @actions.lookup_current_order
5      with member_email=@variables.member_email
6      set @variables.order_summary=@outputs.order_summary
7
8    # Next action
9    run @actions.lookup_current_user
10      with member_email=@variables.member_email
11      set @variables.user_profile=@outputs.profile
12
13    | 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}.
4
5  actions:
6    my_action: @actions.my_action
7      with foo=@variables.Foo
8      set @variables.status = @outputs.status
9      run @actions.other_action
10        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.
4
5  actions:
6    validate_user_ready: @actions.validate_user_ready
7      with user_id=@variables.user_id
8      set @variables.is_ready=@outputs.ready
9      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 action
4    run @actions.check_eligibility
5      with user_id=@variables.user_id
6      set @variables.is_eligible=@outputs.eligible
7
8    # Condition
9    if @variables.is_eligible == True:
10
11      # Conditional action
12      run @actions.fetch_offer_details
13        with user_id=@variables.user_id
14        set @variables.offer=@outputs.offer
15
16      | 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.

Related Topics