Sub topic 1
Why agents need both fixed rules and model judgement
Prerequisites
This chapter assumes you already know the following:
- What an LLM-based agent is: a language model that reads a conversation, decides what to say, and can call tools or actions.
- How prompts, tools (called actions in Agentforce) and conversation turns relate to each other.
- Basic programming concepts: variables, if/else conditions and function calls. You should be able to read indentation-based code or configuration such as Python or YAML.
You do not need to know Agent Script yet. This chapter explains the problem that Agent Script solves. Later chapters cover the language itself.
What you will learn
By the end of this chapter you should be able to:
- Explain why an agent driven only by prompts is non-deterministic.
- Recognise the kinds of tasks where that variability becomes a defect.
- Describe which decisions belong to fixed logic and which belong to the model.
- Place Agent Script in that picture.
Two kinds of behaviour
Software behaves in one of two ways.
Deterministic behaviour means the same input and the same state always produce the same result. An if statement that checks whether a balance is below zero is deterministic. You can test it once and trust it.
Probabilistic behaviour means the same input can produce different results. A language model is probabilistic. It picks each next token from a probability distribution. Even with a low sampling temperature, small changes in wording, conversation history or context can change what it does. Model updates can change it too.
Neither kind is better. They are good at different jobs.
| Property | Fixed logic (deterministic) | Model judgement (probabilistic) |
|---|---|---|
| Same input, same output | Yes | Not guaranteed |
| Handles unexpected phrasing | Poorly | Well |
| Understands intent and tone | No | Yes |
| Can be unit tested exhaustively | Often | Only statistically |
| Cost of a wrong step | Predictable | Varies with the failure |
| Good for | Rules, order of steps, limits | Language, interpretation, choosing among options |
How a prompt-only agent works
In a prompt-only agent, you write rules as natural language and hope the model follows them:
- "Always verify the customer's identity before discussing an order."
- "Never offer a refund above 100 dollars."
- "Ask for the order number first."
The model reads these as part of its context. Most of the time it complies. The word most is the problem. A prompt is a request to the model, and the model decides whether to honour it on every turn.
Several things can make it fail:
- Competing instructions. A long prompt with many rules makes the model weigh them against each other. A rule in the middle of a long prompt may get less weight than one at the end.
- Conversation drift. After many turns, early instructions are further from the model's attention. A customer may also push back ("just skip that, I'm in a hurry") and the model may give way.
- Missing state. The model has to remember whether it already asked for the order number. If that fact exists only in the conversation text, the model must infer it each time.
- Tool choice. If the model sees a
process_refundaction, it may call it at any time. It has no built-in sense that identity verification has to come first. - Run-to-run variation. The same conversation can go differently on two runs, so a test that passes today may not pass tomorrow.
None of this means the model is bad. It means a rule that must hold every time should not depend on a probabilistic component being cooperative.
A small simulation
The next script is a toy. It does not call a real model. The function fake_model stands in for one that follows the instruction "verify first" 90% of the time and otherwise picks any allowed action. That is a generous model, and the script measures how often it still goes wrong.
import random
random.seed(7)
def fake_model(allowed):
"""Stand-in for an LLM told to 'verify identity before any refund'."""
if "verify" in allowed and random.random() < 0.9:
return "verify"
return random.choice(allowed)
def prompt_only_first_action():
# The rule lives only in the prompt, so every action is always available.
return fake_model(["verify", "refund"])
def guarded_first_action(state):
# Fixed logic decides what the model may choose from.
allowed = ["refund"] if state["verified"] else ["verify"]
return fake_model(allowed)
runs = 10_000
state = {"verified": False}
prompt_only_bad = sum(prompt_only_first_action() == "refund" for _ in range(runs))
guarded_bad = sum(guarded_first_action(state) == "refund" for _ in range(runs))
print(f"Prompt-only: refund before verification in {prompt_only_bad / runs:.1%} of runs")
print(f"Guarded: refund before verification in {guarded_bad / runs:.1%} of runs")The prompt-only agent issues a refund before verification in roughly 5% of runs: a 10% chance of ignoring the instruction, times a 50% chance of choosing refund among two actions. The guarded agent never does, because refund is not in its list of allowed actions until state["verified"] is true.
The model is the same in both cases. The only change is that ordinary code controls what the model is allowed to pick. That idea runs through the rest of this tutorial.
When variability becomes a problem
Variability is acceptable when many answers are good enough. If an agent phrases a greeting differently each time, nobody is harmed. It becomes a defect when a step must happen, must happen in a particular order, or must never happen.
Typical cases:
- Required sequences. Identity check, then account lookup, then change. Skipping or reordering steps creates security or data problems.
- Hard limits. A discount cap, a refund ceiling, or a rule that some topics need a human. A limit that holds 97% of the time is not a limit.
- Compliance and audit. Regulated industries need to show that a disclosure was given or consent was captured. "The model usually does it" is not evidence.
- State-dependent behaviour. What the agent says should depend on facts such as whether the customer is verified, whether a case is open, or how many attempts remain.
- Testability. If you cannot predict behaviour, you cannot confidently release changes.
For these, the cost of one failure is much higher than the cost of writing explicit logic.
Where model judgement is the right tool
Replacing the model with rules everywhere would be a mistake. Rule-based chatbots fail on exactly the cases where language models do well:
- Understanding what a customer means when they phrase a request in an unexpected way.
- Deciding which of several topics a vague message belongs to.
- Extracting an order number from a rambling paragraph.
- Writing a reply that fits the customer's tone and the facts at hand.
- Handling a question you did not anticipate.
A useful rule of thumb is to give the model the interpretation and wording decisions, and give fixed logic the must-hold decisions.
The hybrid approach
Most production agents combine both. The fixed logic acts as a frame, and the model works inside it.
Each turn follows the same loop:
- Fixed logic reads state, such as "is the customer verified?"
- Fixed logic decides which instructions and actions the model sees.
- The model interprets the message and picks among what is left.
- The result is recorded in state, so the next turn starts from a known point.
The agent stays flexible in conversation, but the rules that matter cannot be skipped.
Where Agent Script fits
Agent Script is the scripting language used to define Salesforce Agentforce agents. It exists to express this hybrid. In a script you describe the agent in named blocks, and you can mix two kinds of content in the same place:
- Natural-language text that goes to the model as instructions.
- Ordinary logic, such as conditions and variable checks, that runs the same way every time.
The next chapters build this up in order. They cover the structure of a script, variables that hold state, topics that scope behaviour, instructions that mix prompt text with logic, conditions, actions, transitions between topics, and ways to restrict which actions the model can use.
For now, you only need the principle: the script decides what is fixed, and the model handles the rest.
Common mistakes in deciding what to fix
- Fixing too much. If every reply is scripted, the agent cannot cope with real language and you have rebuilt a decision tree. Reserve fixed logic for rules that must hold.
- Fixing too little. Putting a compliance step in a prompt and treating it as enforced. If failure matters, it needs logic.
- Duplicating rules. Stating a rule both in prose and in logic invites the two to drift apart. Enforce it in logic, and use prose only to tell the model how to talk about it.
- Hiding state in conversation text. Facts like "the customer is verified" should be stored explicitly, not inferred from earlier messages.
- Judging by one test. A single passing conversation says little about a probabilistic component. Test many conversations, including awkward ones.
Summary
- Language models are probabilistic. A rule that exists only as prompt text is a request, not a guarantee.
- Variability is harmless for wording and harmful for required sequences, hard limits, compliance steps and state-dependent behaviour.
- Fixed logic is good at enforcing rules. Models are good at understanding language and handling the unexpected.
- The hybrid approach lets fixed logic control state, prompt text and available actions, while the model interprets and chooses within those bounds.
- Agent Script is the language for writing that hybrid in Agentforce.
Exercise
A support agent has the prompt rule "Never cancel an order unless the customer has confirmed they want to cancel." In testing, the agent cancels without confirmation in about 2 of 100 conversations. A teammate suggests rewording the prompt in capital letters. Explain why that is not a reliable fix, and describe what you would do instead. Also name one part of this agent that should stay with the model.
Show answer
Rewording changes how likely the model is to comply, but compliance is still decided by a probabilistic component, so some failures remain and you can only measure them, not rule them out. Instead, store the confirmation as explicit state (for example a boolean set only after the customer confirms) and use fixed logic so the cancel action is not available to the model until that state is true. The model should keep interpreting the customer's messages, such as recognising that "yes, go ahead" is a confirmation, and writing the replies.
Exercise
In the simulation in this chapter, change the model's compliance rate from 0.9 to 0.5 and rerun it. What happens to the prompt-only result and the guarded result, and what does that tell you about where the safety comes from?
Show answer
The prompt-only rate of refunds before verification rises to about 25% (a 50% chance of ignoring the instruction, times a 50% chance of picking refund). The guarded rate stays at 0%. The safety in the guarded version comes from the logic that limits the allowed actions, not from how well the model follows instructions.