# While

Repeat one or more directly connected actions while a condition remains true. The node has explicit **Loop**, **Done**, and **Timeout** outputs and enforces both iteration and duration limits.

## Outputs

| Output | When it runs |
| --- | --- |
| **Loop** | The condition is true. Every node connected directly to this output runs once for the current iteration. |
| **Done** | The condition becomes false, including before the first iteration. |
| **Timeout** | The maximum iteration count or total duration is reached while the condition is still true. |

## Configuration

| Field | Description |
| --- | --- |
| Condition | A structured expression evaluated before each iteration, for example `input.output_text != "ready"`, `state.attempts < 5`, or `loop.iteration < 10`. The loop continues while it is true. |
| **Max iterations** | Required safety limit. Default 10, hard runtime cap 1,000. |
| **Delay between iterations** | Pause before the next pass. Default 1 second; values below 0.1 second are clamped. |
| **Maximum total duration** | Wall-clock limit for the whole loop. Default 300 seconds, hard cap 24 hours. |

Use `last_output` for the current Loop-body result. When that result is JSON, append the property path, for example `last_output.choice != "ready"` or `last_output.items.0.status == "done"`.

The panel displays a maximum body-execution estimate. Real API/model usage depends on how many costly nodes are connected to **Loop**.

## Complete example: draw a weekday until Monday

This example draws a random day. The workflow repeats the Code node until its JSON output contains `"lundi"`.

### 1. Code node

Add a **Code** node after **Start**, then use:

```python
import json
import random

days = ["lundi", "mardi", "mercredi"]
response = {"choice": random.choice(days)}
print(json.dumps(response, ensure_ascii=False))
```

The printed JSON becomes the workflow output of the Code node, for example:

```json
{"choice": "mardi"}
```

### 2. While condition

Configure the While node with:

```text
Continue while: last_output.choice != "lundi"
Max iterations: 10
Delay between iterations: 1 second
Maximum total duration: 20 seconds
```

`last_output.choice` reads the `choice` property from the latest JSON result produced by Code. The condition means: **continue looping while the selected day is not Monday**.

Do not use `input.output_text.choice` here. `input.output_text` is the raw text received by the workflow and is not the current structured output of the loop body.

### 3. Connections

```text
Start → Code → While
          ↑        ├─ Loop ──┘
                   ├─ Done → End
                   └─ Timeout → Alert or End
```

- **Loop → Code** draws a new day, then Code sends its new result back to While.
- **Done → End** is followed as soon as Code returns `{"choice": "lundi"}`.
- **Timeout** is followed if Monday was not drawn before a configured safety limit was reached.

### 4. Execution walkthrough

For the sequence `mardi → mercredi → lundi`:

1. Code returns `{"choice": "mardi"}`. The condition is true, so **Loop** runs Code again.
2. Code returns `{"choice": "mercredi"}`. The condition is still true, so the loop continues.
3. Code returns `{"choice": "lundi"}`. The condition becomes false, so While exits through **Done**.

The trace can therefore show several **While · Loop** and **Code · Iteration** entries before the final **Done** entry. This is expected.

## Polling example

Wait for an asynchronous job to become ready:

```text
Start → HTTP Request (create job) → While
                                      ├─ Loop → HTTP Request (GET status)
                                      ├─ Done → Agent → End
                                      └─ Timeout → Alert → End
```

Use:

```text
Condition: input.output_text != "ready"
Max iterations: 20
Delay: 2 seconds
Maximum duration: 120 seconds
```

## Runtime traces

Workflow testing shows every iteration separately, including its number, body-node input/output, wait duration, elapsed time, exit reason, and whether the final branch is Done or Timeout.

## Compatibility and cautions

- Existing legacy While nodes still run with their previous stop-condition syntax (`empty`, `not_empty`, `contains:value`). Editing them migrates the node to the new continue-while behavior and explicit outputs; reconnect the branches if necessary.
- Put a meaningful recovery path on **Timeout** (Alert, Send Email, fallback response, or End).
- Retries can multiply API calls and cost. Keep the limits low and the delay appropriate for the upstream service.
- State written inside the body remains available for the rest of the same workflow run.

---

*Source: https://agentbuilder.systalink.sn/docs/node-while — human documentation.*
*Other language: [/docs-md/fr/node-while.md](/docs-md/fr/node-while.md).*
*Machine-readable index: [/llms.txt](/llms.txt).*
