Conditional logic for context-specific prompts
Key Takeaway
When an agent must apply a rule the same way every time, store the deciding fact in a variable and put an If/Else (Conditional) on it in the subagent's reasoning instructions. The agent resolves the conditional before it calls the LLM, so the LLM receives only the instructions from the first branch that matches.
Conditional logic lets an agent send a different prompt to the LLM depending on the current situation, such as a customer's loyalty tier or how long ago an order was placed. In Agentforce, if/else statements in a subagent's reasoning instructions are checked before the LLM is called, so the LLM receives only the instructions that fit the current context. Prompt templates in Prompt Builder have a similar beta feature that includes or leaves out content based on merge field values.
Logic and natural language prompts
Many agents send the entire prompt to the LLM on every turn, no matter the context, so they depend on complicated prompt engineering to get things right. Agentforce runs its reasoning logic first and sends only the resulting prompt, so the LLM always gets a prompt specific to the agent's current state.
A subagent's reasoning instructions combine two kinds of content: programmatic logic and natural language prompts.
| Logic | Natural language prompts | |
|---|---|---|
| What it covers | Anything the agent must do before reasoning | Anything the agent should take into account during reasoning |
| What it does | Reliably handles workflow and business rules | Guides the agent's conversational skills more flexibly |
Legacy builder instructions were written only in natural language, so they created large prompts that could exceed LLM context windows. LLMs also applied those long natural language rules inconsistently. LLMs are best at predicting the next most likely step rather than following a sequence.
Sending only the applicable instructions improves instruction adherence, reduces latency and improves accuracy, because the fewer choices an agent has to make, the more likely it makes the right ones.
So when a business rule must apply the same way every time, write it as logic, such as a conditional statement, rather than as a natural language rule the LLM may apply inconsistently.