https://retellai.com/?utm_source=discord&utm_medium=seo logo
Is there a way to a counter
# ❓・ai-evy-support
c
@AI Evy Is there a way to a counter? I.e: In a conversation flow, only try 3 times to gather x, y and z. So you ask the questions, try, fails, counter = counter + 1, retry, fails counter = counter + 1. This would slowly increment.
a
Thanks for tagging me @Ciaran. I'll look through relevant sources and get back to you shortly.
### Answer Yes, you can implement a counter in Conversation Flow using dynamic variables and Code Nodes. [(1) ]() [(2) ]() Here's how to set it up: 1. Initialize a counter variable Set a default dynamic variable (e.g.,
attempt_count
) to 0 at the agent level or pass it when creating the call. [(1) ]() 2. Use a Code Node to increment the counter After each failed attempt, add a Code Node that increments your counter: [(2) ]()
Copy code
javascript
// Increment the attempt counter
const currentCount = parseInt(dv.attempt_count || 0) ;
return { new_count: currentCount + 1 };
3. Store the incremented value In the Code Node settings, use "Store Fields as Variables" to save the result back to your dynamic variable (e.g., map
new_count
from the JSON path to
attempt_count
) . 4. Add transition conditions Create equation-based transition conditions to check if the counter has reached your limit: [(3) ]() -
{{attempt_count}} < 3
→ retry the question -
{{attempt_count}} >= 3
→ move to a fallback node This approach lets you control retry logic precisely while keeping track of how many attempts have been made. [(1) ]() [(2) ]() [(3) ]()
​ To learn more, see the sources I considered: [[1] Dynamic Variables](https://docs.retellai.com/build/dynamic-variables) [[2] Code Node](https://docs.retellai.com/build/conversation-flow/code-node) [[3] Step 3: Add transition conditions](https://docs.retellai.com/build/conversation-flow/transition-condition) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
c
@User So i've got the loop. Extract variables, set variable (number) defaults to 0. It runs, new_count is 2. How can I debug this?
@AI Evy So i've got the loop. Extract variables, set variable (number) defaults to 0. It runs, new_count is 2. How can I debug this?
a
Thanks for the follow-up, @Ciaran. I'll get back to you soon.
c
@AI Evy So i've got the loop. Extract variables, set variable (number) defaults to 0. It runs, new_count is 2. How can I debug this?
a
### Answer To debug your counter loop in Conversation Flow, you can use several methods: [(1) ]() [(2) ]() 1. Test the Code Node directly Open the Code Node editor and click Run Code at the bottom to test your increment logic. [(1) ]() Use the Dynamic Variables dropdown in the editor to set test values for
attempt_count
(e.g., set it to "0", "1", "2") . The output panel will show the result and any
console.log()
output. 2. Add console.log() statements In your Code Node, add debugging output:
Copy code
javascript
const currentCount = parseInt(dv.attempt_count || 0) ;
console.log("Current attempt_count:", dv.attempt_count) ;
console.log("Parsed count:", currentCount) ;
const newCount = currentCount + 1;
console.log("New count:", newCount) ;
return { new_count: newCount };
These logs will appear in the test output panel when using Run Code, and are also available in call logs. 3. Review Call History After making a test call, check the Call History to see how the conversation transitioned through each node. [(2) ]() This helps you verify if your transition conditions are working as expected. 4. Check your variable mapping Verify that in your Code Node's Store Fields as Variables section, you're correctly mapping the JSON path
new_count
to your dynamic variable
attempt_count
. [(1) ]() If the variable name doesn't match exactly, the counter won't update properly. 5. Verify transition conditions Make sure your transition conditions are checking the correct variable name and using the right comparison operators (e.g.,
{{attempt_count}} < 3
) . [(2) ]() If
new_count
is showing as 2 when it should be incrementing from 0, check that your initial default value is actually being set to 0 and that you're storing the result back to the same variable name you're reading from. [(1) ]() [(3) ]()
​ To learn more, see the sources I considered: [[1] Code Node](https://docs.retellai.com/build/conversation-flow/code-node) [[2] Debug guide](https://docs.retellai.com/build/conversation-flow/debug-guide) [[3] Dynamic Variables](https://docs.retellai.com/build/dynamic-variables) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
c
@AI Evy working now 🙂
a
Thanks for the follow-up, @Ciaran. I'll get back to you soon.
### Answer That's great to hear! 🎉 Glad you got your counter loop working in Conversation Flow. If you run into any other issues or have questions about implementing retry logic, transition conditions, or debugging, feel free to reach out.
message has been deleted
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.