How do I trigger the function in AI Agent step?
Hi, I have a javascript function that updates some of my variables. I want the AI agent step to keep calling this function every time a user sends a message.. How can I do that?
1 Reply
Here’s how to make sure your JavaScript Function runs on every user turn.
1. Create the Function
• Add your JS file under Functions and test that it writes the variables you need in
• Typical pattern: Listen → Agent → (Agent calls function & responds) → Listen → Agent … Alternative (code-first) pattern If you prefer a guaranteed call that doesn’t rely on the LLM, put a Function step before the Agent: Function step (runs JS) → Agent step → Listen → back to Function step That loop forces the function to run before every Agent prompt. Key point: an Agent step only runs when the flow reaches it, so you need to either loop back to the same Agent step or place the Function step in the loop to ensure it executes each turn. Docs: https://docs.voiceflow.com/docs/agent-step#functions
outputVars
.
2. Attach it to the Agent step
• Open your Agent step → Functions panel → + Add function → select your function.
• Fill the LLM description so the model knows it must call the function each time, e.g.
"Always call updateSessionVars
immediately after you read the user’s latest message. Return its results even if you don’t need to send them back to the user."
• If your function needs inputs, add them and describe them the same way (“No special input required – just call the function on every turn”).
As long as the description makes it clear the function is required every turn, the LLM will include the call automatically.
3. Keep the conversation looping
• After the Agent replies, use a Listen (or another Agent step) so the flow returns to the Agent step for the next message.• Typical pattern: Listen → Agent → (Agent calls function & responds) → Listen → Agent … Alternative (code-first) pattern If you prefer a guaranteed call that doesn’t rely on the LLM, put a Function step before the Agent: Function step (runs JS) → Agent step → Listen → back to Function step That loop forces the function to run before every Agent prompt. Key point: an Agent step only runs when the flow reaches it, so you need to either loop back to the same Agent step or place the Function step in the loop to ensure it executes each turn. Docs: https://docs.voiceflow.com/docs/agent-step#functions