<!-- Sema documentation — std.agent_loop
     Sema is a new AI-native, neurosymbolic programming language most models
     have not been trained on. Full corpus: https://sema.49.12.246.95.sslip.io/llms-full.txt
     Install language support: https://sema.49.12.246.95.sslip.io/install-skill/ -->

# std.agent_loop

> Reflected API reference for the Sema standard-library module std.agent_loop.

> Generated by `sema doc` from `stdlib/sema/agent_loop.sema`. Import with `from std.agent_loop import …`. For a narrative introduction see [std.agent_loop](/stdlib/agent_loop/).

# `agent_loop`

std.agent_loop — the agentic loop as a reusable combinator (design ④).

`loop_until(init, max_iters, step, done)` runs `step` (a pure state -&gt; state
function) until `done(state)` or `max_iters`. Replaces the ~300-line hand-rolled
`_run_deep` while loop; the termination policy is data. `step`/`done` are pure
lambdas over an immutable state (Sema forbids a lambda mutating captured state,
which keeps the loop parallel-safe).

# `loop_until`

```sema
def loop_until(init: any, max_iters: int, step: fn, done: fn) -> any !{}
```

**Parameters**

| name | type |
|---|---|
| `init` | `any` |
| `max_iters` | `int` |
| `step` | `fn` |
| `done` | `fn` |

**Returns** `any`

**Effects** `!{}`
