Agent Playground
Tutorial 3: Keep trying until it works
Build a flow that tries again when something fails, waits a moment between tries, and stops by itself when the loop limit is reached.
Lots of real jobs fail the first time and work the second: a busy website, a slow supplier's system, a file that isn't ready yet. In this tutorial you'll build a loop: try, and if it fails, wait a moment and try again. Then you'll see the loop limit stop a run that would otherwise go on forever.
Time: about 10 minutes. Plan: Free. Cost of a run: a fraction of a cent.
1. A step that sometimes fails
- In Agent Playground, choose New flow and name it
Fetch the price list. - Click the Start step, name it
Fetch the price list, and set its Command to:
n=$(( $(cat tries 2>/dev/null || echo 0) + 1 )); echo $n > tries; if [ $n -lt 3 ]; then echo "The site is busy (try $n)"; exit 1; fi; echo "Got the price list on try $n"This is a stand-in for a busy website, so you can watch a loop work: it counts its tries, fails the first two times, and passes on the third. In your own flow, this would be the real fetch, like the curl command from Tutorial 1.
2. Wait, then go back
- Select Fetch the price list and click Wait in the Blocks panel (under People and rules). Name it
Wait a momentand set it to 5 seconds. - Select Fetch the price list again. Under Where next, set Failed to Wait a moment.
- Select Wait a moment. Under Where next, set Next to Fetch the price list.
That last arrow points back up to an earlier step, and that's what makes a loop. The builder draws it dashed and labels it Loop, up to 5, the most times the run may go round.
- Add an Output named
Got it, and set Passed on Fetch the price list to go to it.
Pausing between tries matters: a busy website needs a moment, and trying again instantly often fails again the same way.
3. Run it
Set the limits to Loops 5, Time 120 seconds and Spend $0.05, then choose Run.
Watch the run go round: Fetch the price list fails ("The site is busy (try 1)"), Wait a moment waits 5 seconds, and the run goes back up. On the third try it passes and finishes at Got it. The box shows ×3 for the three times it ran, and the panel on the right groups the steps by First pass, Loop 1 and Loop 2.
4. Watch the limit stop it
Now see what happens when something never works. Open the limits button and set Loops to 1, then run again.
The run tries once, waits, goes round once more, and fails again. It's allowed only one trip round the loop, so it stops there, as Stopped at a limit, with the reason: the loop limit. Nothing after the loop runs.
Set Loops back to 5 when you're done.
Picking a good loop limit
- Simulate first. The forecast tells you how often the loop limit would stop the run. If it stops a large share of simulated runs, raise Loops or improve the step that keeps failing.
- Keep it low for things that may be down for hours. If a supplier's site is down for the day, five tries five seconds apart won't change that. Stop early and try again later, for example with a schedule.
- Clean up when a limit stops the run. Select a step whose result you'd want to keep and, under Advanced, tick Go back here if a limit stops the run. A run that's stopped puts the sandbox back to how it was after that step, so a half-finished try is never left behind.
To see a limit stop a loop in a ready-made flow, run the Runaway loop recipe.
What you learned
- An arrow that points back to an earlier step makes a loop. Every trip round counts toward the Loops limit.
- A Wait step gives something slow or busy a moment before the next try.
- When a limit is reached, the run stops as Stopped at a limit and says which limit.
Next
In Tutorial 4, instead of trying one thing again and again, you'll try three things at the same time and keep the best.