Agent Script Pattern: Fetch Data Before Reasoning
Run actions to retrieve data before the LLM begins reasoning.
Place action calls at the top of your reasoning instructions to fetch data before the prompt is constructed. This ensures the LLM has access to current, accurate information when generating responses.
Fetching before reasoning ensures the LLM has accurate, current data. For example, you can store the output of an action in a variable and use it to personalize instructions. Or you can create a filter based on the variable to refine the prompt that's sent to the LLM. Actions inside reasoning instructions execute before the prompt is sent to the LLM.
Pattern Example: Look up the user's current order before the conversation starts so the agent can greet them with their order status and personalized recommendations.
The pattern:
- Check if data has already been fetched
- If not, run the lookup action
- Store results in a variable
- Reference the variable in the prompt
Fetch data and immediately check it to determine what options to present.
- Avoid unnecessary calls: Always check if data exists (
if @variables.data == "") before making a call to fetch data to avoid running actions unnecessarily.
- Reference: Reasoning Instructions
- Reference: Actions
- Reference: Variables