Skip to Content
0%

Architect Deterministic Conversational Flows with Agent Script

Learn how deterministic state management helps qualification agents follow business rules, maintain context, and consistently collect required information.

A generative agent that skips questions, goes off-topic, and can’t find its way back creates a bad user experience, incomplete data, and a pipeline that’s difficult to rely on. When we built a sales qualification agent using Agentforce, we ran into this challenge. Our first version, built on Agentforce Builder, performed well in structured testing. In the field, however, we found that it occasionally fell short. Questions were skipped. Leads went off-topic, and the agent couldn’t get back on track. The intermittent nature of these instances made the system unreliable, and in sales, unreliability has real costs.

This post details how we moved from a probabilistic model to a deterministic, state-managed architecture using Agent Script, and the architectural decisions that made the difference.

Understand the qualification problem before picking a pattern

Most early Agentforce use cases are reactive: a user raises an issue, the agent resolves it. Sales qualification flips that model. The agent must drive the conversation, ask questions in a specific sequence, and constrain the user to the topic at hand, while staying natural enough that the lead doesn’t drop off.

Using sales qualification as a primary example of a deterministic state machine, we designed our strategy around the Budget, Authority, Need, Timeline (BANT) framework. BANT is a classic sales methodology that evaluates prospects across those four criteria, which informed our mandatory qualification questions.

We also needed to collect additional information to enrich lead data without limiting optional enrichment opportunities. To support both goals, we organized the process into three phases:

  • Phase 1: Mandatory data enrichment. Confirm existing information and collect missing information. This phase captures must-have information such as first and last name, job role, and related data.
  • Phase 2: Mandatory qualification. Evaluate the lead against BANT criteria. We use these responses to determine if the lead is worth pursuing.
  • Phase 3: Optional enrichment. Collect secondary details, such as LinkedIn profiles and language preferences, after a lead meets the qualification threshold.

What is BANT?

Learn more about the BANT framework, including its benefits, challenges, and application.

Separating the conversation into these distinct phases is a critical architectural choice. Trying to collect all this data at once can overwhelm users and increase drop-off rates. The agent needs to know not only what to ask, but also when to ask it. 

During our initial implementation, we learned that guiding an agent through these phases in a reliable and consistent order is harder than it sounds. Enforcing progression through these phases helps maintain conversation flow and improves consistency in how the agent collects and validates information.

Understand the failure modes of legacy orchestration

The first version of our Qualification Agent used standard orchestration in legacy Agentforce Builder. For straightforward conversations, it worked, but in production, two failure modes emerged:

  • Question skipping: The LLM occasionally bypassed mandatory BANT questions when a user’s response gave it just enough signal to infer an answer. The result was incomplete CRM records and leads that entered the pipeline without a full qualification profile.
  • Off-topic instability: When a lead diverged, such as asking an unrelated question or providing an ambiguous answer, the agent lost its place. Without a state anchor, it couldn’t reliably return to the exact step where the conversation had broken down. 

Both failure modes share the same root cause: the agent had no persistent memory of where it was in the qualification sequence. At each turn, the LLM was effectively reconstructing context from scratch, inferring its place in the qualification process from conversational context alone.

Choose Agent Script when the sequence is non-negotiable

At its core, Agent Script is a scripting language within Agentforce that allows agent builders to have more control over the agent’s behavior. It enables rules to invoke subagents, implement tool chaining, and enforce deterministic execution based on state variables. In contrast, legacy Agentforce Builder orchestration relied on probabilistic reasoning and action instructions to determine the conversational path.

Design and Implement AI Agents with Agentforce

Learn how to plan, build, test, deploy, and monitor an AI agent with Agentforce.

Agent Script gives architects explicit control over multiturn logic. Instead of relying on the LLM to infer the next step from prompt context, you define transitions programmatically. The agent advances from one step to the next only when specific business conditions are met: a confirmed budget figure, a named decision-maker, and a concrete timeline.

A Practical Guide to Reliable AI Agents That Work

Learn how to build enterprise-grade AI agents that balance LLM creativity with deterministic control. This guide explores how hybrid reasoning and agent scripting ensure your agents remain trustworthy and follow business rules.

How we implemented a linear qualification path

To illustrate how the system maintains state with Agent Script, the Qualification Agent uses a current_step variable, initialized at session start, to execute a deterministic sequence. The agent moves through the following steps:

Step 1: Gather mandatory lead information

  • Collect lead info: Gather mandatory contact information to initialize the lead record.

Steps 2–5: Conduct sequential BANT qualification

  • Identify need (Step 2): Identify the primary business challenge the lead is trying to solve.
  • Verify timeline (Step 3): Confirm implementation schedule and urgency.
  • Confirm budget (Step 4): Validate financial alignment and investment capacity.
  • Determine authority (Step 5): Identify the decision-maker and key stakeholders.

Step 6: Conduct evaluation

  • Validate: Evaluate the prospect’s answers against the qualification threshold.

Step 7: Gather optional information

  • Gather: Enrich qualified profiles by capturing secondary details (industry, revenue, LinkedIn) strictly upon user consent and qualification status.

The agent initializes the current_step variable at the very start of the session. If the variable is empty, the agent defaults to Step 1: Collect Lead Info. This state persistence ensures that even if a lead takes a detour, the agent knows exactly where it left off in the qualification sequence.

The following logic is implemented within start_agent, a subagent of the Qualification Agent that acts as a router. This subagent evaluates the state at the beginning of every turn, serving as a traffic cop that routes the lead to the correct subagent based on the current value of @variables.current_step.

// Deterministic Step Management Logic
// Step 1: Initialization - Gather mandatory Lead info
if not @variables.current_step:
    set @variables.current_step = "Collect_Lead_Info"
if @variables.current_step == "Collect_Lead_Info":
    transition to @topic.Collect_Lead_Info
// Step 2-5: Sequential BANT Qualification
if @variables.current_step == "Need":
    transition to @topic.Need_Interest_Challenge_Focus
if @variables.current_step == "Time":
    transition to @topic.Timing_Assessment
if @variables.current_step == "Budget":
    transition to @topic.Budget_Assessment
if @variables.current_step == "Authority":
    transition to @topic.Authority_Assessment
// Step 6: Evaluation
if @variables.current_step == "Qualification":
    transition to @topic.Process_BANT_Answers
// Step 7: Gather optional information
if @variables.current_step == "Collect_optional_info":
    transition to @topic.Collect_Optional_Info

How we managed transitions between steps

Step completion is handled by individual subagents, not by start_agent. To illustrate how the agent moves through the different stages of the sequence, imagine the user is currently at the Timing step. 

Once @topic.Timing_Assessment successfully captures valid Timeline data, it uses a @utils.setVariables action to update @variables.current_step to “Budget” before the step concludes. This ensures the agent does not advance until the mandatory data for the current step is captured and validated.

Here is the technical implementation of this transition within the subagent’s configuration:

subagent Timing_Assessment:
    label: "Timing Assessment"
    description: “Your Goal is to do the timeline Assessment of the BANT Process and advance the user to Budget when it’s complete”
    reasoning:
        instructions: ->
		| Perform the Timeline Assessment for the Lead qualification 
		When it’s complete, advance to the next step {!@actions.advance_step}  and continue the flow.
        actions:
            advance_step: @utils.setVariables
                with current_step = "Budget"

The following diagram shows the end-to-end Agentforce step sequence, detailing the state-driven transitions, off-topic handling loops, and the logic that triggers automated agent routing.

Handling interruptions with loop-back logic

Off-topic inputs are inevitable. A lead asks about pricing midway through a timeline question. They give a vague answer that doesn’t satisfy the step’s completion condition. With a generative model and no explicit state management, these off-topic inputs often break the flow. With Agent Script, they don’t, because current_step hasn’t changed. The loop-back logic works as follows:

  • Ambiguous or off-topic input: The start_agent catches anything that doesn’t satisfy the current step’s completion condition.
  • Nurturing response: A dedicated handler provides a brief, helpful answer to the diversion, keeping the conversation natural rather than robotic.
  • Return to state: Because current_step is unchanged, the agent loops back to the unresolved question with a bridging phrase, for example, “That’s a great question. To get back to our timeline, when were you looking to start?”

The current_step variable acts as a logic anchor. The agent always knows exactly where it is, regardless of what has happened previously.

Testing deterministic state transitions

In Agent Script, a passing test is a verified state transition where the orchestrator, not the LLM, controls the execution flow. A “pass” occurs only when the agent stays on the current step until all required data has been collected and validated. To verify that step-skipping and nondeterministic branching are not possible, move beyond semantic evaluation and perform a simple sequence audit:

  • Predicate logic verification: Monitor the agent’s execution across multiple steps to observe how the current_step variable tracks progress. Audit the logs to verify deterministic behavior, ensuring that Step B is only invoked after the completion of Step A.
  • Negative path injection: Attempt to force a transition by providing “future state” data (for example, providing timeline details or off-topic content while in the “Need” state). A passing result is a hard-gate intervention where the Agent Script engine does not execute the transition and re-anchors the LLM to the current state tracked by the current_step variable.

Next step: Build your own reliable agents with Agent Script

Building a reliable, deterministic agent involves more than prompt engineering. When the sequence of a conversation represents your core business logic, you need a framework that enforces it. That is what Agent Script provides. To move from unreliable LLM outputs to a deterministic system, start with the architecture:

  1. Map the logic. Before touching any configuration, define your conversation steps, completion conditions, and transition rules on paper.
  2. Define state tracking at the step level: Define state variables to track the completion conditions of individual steps, establishing a deterministic workflow.
  3. Build a minimal proof of concept. Create a three-step flow with one interruption scenario. Validate that your logic holds firm before adding complexity.

A state-managed conversational workflow isn’t specific to BANT. Any use case that requires a strict conversational sequence, such as onboarding flows, compliance interviews, or structured intake forms, can benefit from this architecture.

The Agent Script developer guide covers the full syntax and logic patterns. Start there, then apply the three-step validation sequence above to your first proof of concept.

Build Deterministic Agents with Agent Script

Learn how to blend natural language instructions with programmatic expressions to build robust, context-aware AI agents. This developer guide provides the syntax and logic patterns needed to ensure your agents execute business rules deterministically.

Get the latest articles in your inbox.