Agent Script Pattern: Using List Variables

With list variables (also called collection variables), your agent can store and iterate over a collection of values. For example, create a list of questions that an interview agent must ask, or a list of objects to store an action’s output. A list can store any supported type, such as strings, booleans, numbers, or objects. Use a list item anywhere you use a custom variable.

To reference an item in a list, use json_path or bracket [] syntax. json_path syntax returns the value enclosed in [""] characters, while [] syntax returns the raw value.

SyntaxExampleExample Return Value
@variables.<variable_name>[<index>].data.<field_name>The name of the fourth account in a list of accounts:
@variables.all_accounts[3].data.Name
Acme Corp
json_path(@variables.<variable_name>, "$[<index>].data.<field_name>")The name of the fourth account in a list of accounts:
json_path(@variables.all_accounts, "$[3].data.Name")
["Acme Corp"]

Lists are zero-indexed. To get the first item in your list, use [0].

Advance through a list one item per turn by using an index variable to track the current list item. Increment the variable in after_reasoning to ensure determinism — after_reasoning is run deterministically (it isn’t sent to the LLM), and is guaranteed to run every turn (the subagent has no transitions).

This example shows a TestList1 subagent that asks one question per turn, and a Variables block that declares the index and question variables. The subagent’s responsibilities are spread across two sub-blocks:

  • reasoning.instructions: runs first. It reads the question specified by the current index variable, then builds the prompt sent to the LLM.
  • after_reasoning: runs after the LLM returns its response. It compares the current index to the length of the questions list, then increments the index if more questions remain.

Use this pattern when:

  • The agent must walk through a list in order (checklist steps, survey questions, records) and reference exactly one item in each turn’s prompt.
  • Ordering must be deterministic, not subject to LLM reasoning.

This agent preview shows how the pattern works.

Output of list pattern 1 agent

This pattern doesn’t show how to handle the workflow after the agent asks the last question, because that logic is agent-specific. Your agent can transition to the next subagent, set a state variable so the subagent isn’t called again, or provide specific instructions in the agent router.

This example shows how to read fields from a list of objects by using both json_path and bracket [] syntax. The subagent calls an action that returns a list of Account objects, stores the returned list in the all_accounts list variable, and extracts two account names from the list.

Use this pattern when:

  • An action returns a list of objects (accounts, cases, orders) and later reasoning or logic needs to reference specific field values.
  • The customer uploads a complex list, and you reference specific values.
  • Lists are zero-indexed. To reference the first item, use [0].
  • json_path returns the matched value wrapped in [""] characters (for example, ["Acme Corp"]), while bracket [] syntax returns the raw value (for example, Acme Corp). Choose the syntax that matches the value you want to store or display.
  • To advance through a list deterministically, increment the index variable in after_reasoning instead of reasoning.instructions, so the update isn’t left to the LLM.
  • Use len(@variables.<list>) to check a list’s length before you reference an item, so your agent doesn’t index past the end of the list.