---
title: Tutorial 3: Keep trying until it works · Agent Playground docs
description: Build a flow that tries again when something fails, waits a moment between tries, and stops by itself when the loop limit is reached.
url: https://www.quietrobot.ai/docs/playground/tutorials/loops
---

# 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.

```flow
{"title": "What you'll build: on Failed, wait, then try again. The arrow back up is the loop.", "nodes": [["fetch", "run", "Fetch the price list"], ["wait", "wait", "Wait a moment", "5 s"], ["done", "end", "Got it"]], "edges": [["fetch", "wait", "Failed"], ["wait", "fetch", "Next"], ["fetch", "done", "Passed"]]}
```

## 1. A step that sometimes fails

1. In **Agent Playground**, choose **New flow** and name it `Fetch the price list`.
2. Click the **Start** step, name it `Fetch the price list`, and set its **Command** to:

```bash
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](https://www.quietrobot.ai/docs/playground/tutorials/first-flow).

## 2. Wait, then go back

1. Select **Fetch the price list** and click **Wait** in the Blocks panel (under **People and rules**). Name it `Wait a moment` and set it to **5** seconds.
2. Select **Fetch the price list** again. Under **Where next**, set **Failed** to **Wait a moment**.
3. 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.

4. 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.

<figure class="shot">
  <img class="art-light" src="/docs/playground/tutorials/loops-run-light.webp" alt="A finished run: Fetch the price list ran three times, Wait a moment twice, and the run ended at Got it after 2 loops." width="1440" height="900" loading="lazy">
  <img class="art-dark" src="/docs/playground/tutorials/loops-run-dark.webp" alt="" width="1440" height="900" loading="lazy">
  <figcaption>The run's panel shows each trip round the loop: the first pass, then Loop 1 and Loop 2.</figcaption>
</figure>

## 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.

<figure class="shot">
  <img class="art-light" src="/docs/playground/tutorials/loops-limit-light.webp" alt="A run stopped at a limit: the panel says it stopped at the max loops (1) limit, and nothing ran past it." width="1440" height="900" loading="lazy">
  <img class="art-dark" src="/docs/playground/tutorials/loops-limit-dark.webp" alt="" width="1440" height="900" loading="lazy">
  <figcaption>With Loops set to 1, the run stops at the limit instead of trying forever.</figcaption>
</figure>

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](https://www.quietrobot.ai/docs/playground/tutorials/triggers).
* **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](https://www.quietrobot.ai/docs/playground/recipes/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](https://www.quietrobot.ai/docs/playground/tutorials/lanes), instead of trying one thing again and again, you'll try three things at the same time and keep the best.
