Agent Script Pattern: Enforce Required Workflows Through Subagents In Multi Turn Conversations

Ensure your agent adheres to your subagent sequencing rules through multi-turn conversations, even when your sequencing rules are complex. Use a step variable to control which subagent the agent router selects. Within a subagent, allow the agent to evaluate the customer’s answer and set the step variable for the next step.

Why Use This Pattern 

Use this pattern when:

  • The agent must ask a long sequence of required questions and validate each answer before moving to the next question
  • The next question asked depends on the previous question’s answer
  • The agent may spend many turns in a single subagent, for example clarifying a specific salary requirement or explaining certification equivalency.

When Not to Use This Pattern 

Use the simpler pattern in Enforce Required Workflows for a Subagent when you only need a single prerequisite gate, such as verification before routing, or a one-subagent prerequisite like collecting order_id before order help.

Example: Step-Driven Interview Questions 

In this pattern, start_agent uses the currentInterviewStep variable to select the current subagent. In each subagent, the agent asks the customer a specific set of questions. Based on the customer’s response, the agent sets the value of the currentInterviewStep variable, controlling what the next subagent in the sequence will be.

For example, in the permission subagent, the agent asks the customer whether they have legal permission to work in the region.

  • If the customer does have permission, the agent sets currentInterviewStep to Eligibility, ensuring the agent router routes to the eligibility subagent.
  • If the customer doesn’t have permission, the agent sets currentInterviewStep to End, ensuring the agent router routes to the end_interview subagent.
Step-Based Agent Router
1start_agent agent_router:
2    label: "Agent Router"
3
4    description: "Welcome the user and determine the appropriate subagent based on user input"
5
6    reasoning:
7        instructions: ->
8            if @variables.currentInterviewStep == "Permission":
9                transition to @subagent.permission
10            if @variables.currentInterviewStep == "Eligibility":
11                transition to @subagent.eligibility
12            if @variables.currentInterviewStep == "Availability":
13                transition to @subagent.availability
14            if @variables.currentInterviewStep == "End":
15                transition to @subagent.end_interview
16
17
18subagent permission:
19    label: "Permission"
20
21    description: "Confirm the candidate has the legal right to work in Wonderland."
22
23    reasoning:
24        instructions: ->
25            | Confirm whether the candidate has the legal right to work in Wonderland.
26              Ask a clear yes or no question and allow the candidate to provide context if needed.
27              If the candidate confirms eligibility, acknowledge and advise the next step.
28              If the candidate is not eligible or refuses to answer, advise that the role requires work authorization.
29              If the candidate is eligible, call {!@actions.setCurrentInterviewStep} with currentInterviewStep set to "Eligibility".
30              If the candidate is NOT eligible, call {!@actions.setCurrentInterviewStep} with currentInterviewStep set to "End".
31
32        actions:
33            setCurrentInterviewStep: @utils.setVariables
34                description: "Set the CurrentInterviewStep variable"
35                with currentInterviewStep = ...

Tips 

  • Keep one responsibility per subagent: Design each subagent to validate one required answer, then move to the next step.
  • Use explicit step names: Use clear values like permission, eligibility, and availability so routing remains easy to read and maintain.
  • Let subagents own completion logic: The subagent should decide when an answer is satisfactory and continue asking follow-ups until it is.

Related Topics