# Loop: a task in steps

> curva.loop runs a multi-step task on its own: at every step Curva decides which of your actions comes next, with a confidence, and stops instead of guessing when it is not sure.

Source: https://docs.tarkova.com/curva/guides/loop/

`curva.loop` runs a task that takes several steps. You give it a goal and your own actions (plain
Python functions). At every step Curva decides which action comes next, runs it, looks at the
result and decides again, until the goal is reached.

```python

def look_up_order(state):
    """Fetch the order's status."""
    state["order"] = orders.get(state["order_id"])
    return state["order"]

def refund(state):
    """Refund the order in full."""
    return payments.refund(state["order_id"])

def reply(state):
    """Send the customer the answer."""
    return mail.send(state["customer"], state)

ticket = {"ticket": "My order came late, I want my money back", "order_id": "A-104", "customer": "priya@example.com"}

run = curva.loop("Resolve this ticket", [look_up_order, refund, reply], ticket)

print(run)      # done after 3 steps: look_up_order → refund → reply
run.done        # True when Curva chose to finish
run.stopped     # "done", "unsure", "stuck", "blocked", "limit" or "failed"
```

That one line is all there is: a goal, a list of your functions, and what to work on.

## What makes it different from a free-running agent

Every step is one typed decision: a choice among your actions plus `done`, with a probability.

- **It stops when it is not sure.** If the best next action is below `min_confidence` (default
  0.8), the loop ends with `stopped == "unsure"` and nothing more is done. Hand the run to a person.
- **It cannot invent an action.** The only possible answers are the actions you listed.
- **It always ends.** `max_steps` (default 8) caps it (`"limit"`). If no model answers, or one of
  your actions raises, it ends with `"failed"` and `run.error` says why.
- **It doesn't go round in circles.** When the same action gives the same result twice in a row
  and Curva picks it again, the loop ends with `"stuck"` instead of repeating it.
- **It checks every action before it runs.** Each chosen action is checked against a policy; one
  that is not clearly allowed ends the loop with `"blocked"` and is not carried out. That is one
  extra decision per step. Without `policy=`, Curva's default applies: an action that cannot be
  undone or that moves money (delete, cancel, refund, pay, transfer, buy) needs the user's
  explicit approval, and any other action is allowed. Pass your own rules, such as
  `policy="Never cancel or delete anything unless the customer names exactly what"`, to replace it.
- **Every step is on the record.** Each decision is logged with the run's id, so the dashboard's
  **Call** button shows the whole run in order, and `node="ticket-agent"` shows it as its
  [own node](/curva/guides/nodes/).

## Details

- An action is called with the `state` you passed and may change it; the next decision sees the
  change. What it returns is kept as that step's result (anything that can be written as JSON).
- An action's name is the function's name and its description the first line of its docstring.
  To name them yourself, pass a dict: `{"refund": refund_fn}`.
- `run.steps` has each step's `action`, `confidence` and `result`.
- Curva sees the goal, the state and the latest 6 steps (`recent=`).
- Steps are asked in one order: the order of your actions often carries meaning, so the loop does
  not ask again with the options reversed. Pass `debias=True` to ask both ways anyway.
- Anything `decide` takes can be passed: `model`, a fallback chain `model=[a, b]`, `hedge`,
  `deadline_ms`, `project`, `node`.
- `done` is reserved: an action cannot have that name.

Python only for now.
