# When a run fails

> How Lumen retries a failing step, what a failed run shows, and how to make a flow answer the sender and tell your team when a run ends failed. Also the limits that stop a runaway agent.

Source: https://docs.go-lumen.ai/he/build/when-a-run-fails

A step can fail: a scan is blank, a service is down, an agent runs too long. Lumen retries what is worth retrying, stops what is not, and lets you decide what the sender and your team hear when a run ends failed.

-   Build
-   Per flow
-   YAML and console

## Attempts and retries

Every step gets up to **3 attempts**. What happens after a failed attempt depends on why it failed.

| The attempt failed because | What Lumen does |
| --- | --- |
| Something went wrong that running it again may fix: a service did not answer, a worker was lost, a step crashed. | Waits and tries again: 5 seconds before the second attempt, 10 before the third. |
| Something only you can fix: the scan is blank, a setting is missing, a flow it calls failed. | Fails the step at once. A second attempt would fail the same way. |
| The third attempt failed. | Fails the step. |

A step that fails for good ends the run as **failed**. Steps after it do not run. A step that is only waiting (for a person's answer, for a time, for a [child run](https://docs.go-lumen.ai/he/build/run-another-flow.md)) has not failed.

## What a failed run shows

The run's status is **Error**, and a banner at the top says **This run stopped**, names the step where it stopped and gives the reason in plain words. The canvas marks that step in red, and the path after it is drawn dashed because it never ran. Click the step to see its input, the error, and how many attempts it used.

![The top of a run page for the flow route-by-amount. The status is Error. A red banner reads This run stopped, Step 2, Find the details, is where this stopped, and gives the reason: no total could be found in the document. A Retry button with a menu is on the right.](https://docs.go-lumen.ai/he/img/build/when-a-run-fails-01-failed-run.png)

*A failed run: the banner names the step and the reason, and Retry is one click away.*

From here you fix the cause and run it again. **Retry** starts the run over; **Run from here** on a step reuses the steps before it and spends nothing on them. Both are covered in [Runs and retries](https://docs.go-lumen.ai/he/operate/runs-and-retries.md).

## Tell people: spec.onError

By default a failed run is visible only to someone who opens it. The sender of the email that started it hears nothing. `spec.onError` fixes that. It belongs to the flow, not to a step, so it works when the run dies before it reaches any step you placed for errors.

| Field | Type | What it does |
| --- | --- | --- |
| `reply` | boolean | Sends a short apology to the sender of the email that started the run, in the same thread (`Re: <subject>`). |
| `replyBody` | text, 1 to 1000 characters | Your wording in place of Lumen's. One paragraph per line. Needs `reply: true`. Keep technical detail out: the sender reads it. |
| `notify` | list, up to 10 | People who get an email with the run's link, the step that failed and its error. Each entry is an address, or `admins` for the workspace's admins. |

Set at least one of `reply` and `notify`. A flow with no `onError` behaves as before.

```yaml title="onerror-invoice-intake.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: onerror-invoice-intake
  namespace: tenant-acme
spec:
  displayName: Invoice intake with an error handler
  onError:
    reply: true
    replyBody: |
      We could not read your invoice.
      Our team has been told and will get back to you.
    notify: [admins, dana@acme.example]
  runLimits:
    maxTokens: 400000
    maxMinutes: 20
    pauseAfter: 3
  triggers:
    - id: email
      ref: {name: email-arrives}
      connectorRef: {name: invoices-inbox}
      next: [text]
  nodes:
    - id: text
      stepRef: {name: read-text}
      next: [details]
    - id: details
      stepRef: {name: chat-agent}
      config:
        instructions: Find these details in the invoice.
        fields: [{name: supplier}, {name: total, hint: Amount due, type: money}]
        files: {mode: ifNoText}
      next: [keep]
    - id: keep
      stepRef: {name: save-file}
      config: {destination: lumen}
```

In the console, open the flow's editor and click **When a run fails** in the top bar. The form has the same four settings.

| Console label | Field |
| --- | --- |
| **Reply to the sender** | `reply` |
| **Reply text** | `replyBody` (the console's box takes up to 250 characters; YAML takes up to 1000) |
| **Tell the workspace admins** | `admins` in `notify` |
| **Also tell** | the other addresses in `notify` |

![The When a run fails dialog. A short introduction, then four settings: Reply to the sender (a switch, off), Reply text (an empty text box), Tell the workspace admins (a switch, off) and Also tell (an empty address list). A Done button is at the bottom.](https://docs.go-lumen.ai/he/img/build/when-a-run-fails-02-on-error.png)

*The handler is a small form in the flow editor's top bar.*

### Exactly when it runs

-   **Once per run**, when the run ends failed, whatever the cause: a step that failed for good, a worker that stopped answering on its last attempt, or a wait nobody answered. Each message is queued once and delivered once, even if the delivery itself has to be retried.
-   **Never for a cancelled run.** Cancelling is a decision, not a failure.
-   **Never for a test or an evaluation run.**
-   **No reply without someone to answer.** A run that no email started has no sender, so `reply` sends nothing. Lumen also sends nothing to a no-reply or mailer-daemon address, so a reply cannot start a bounce loop.
-   **Frozen at queue time.** The run uses the handler the flow had when the run was queued. Editing it later does not change runs already in flight.

The default reply is short and polite, in Hebrew or English by the email's language: "Sorry, we could not process this email. We were alerted and will get back to you."

### With a router and child flows

When a flow [runs another flow](https://docs.go-lumen.ai/he/build/run-another-flow.md), the top-level run answers the sender, once. A child started by a [`run-flow`](https://docs.go-lumen.ai/he/reference/steps/run-flow.md) step does not reply itself when its parent's handler replies, even if the child's own handler says `reply: true`. The child still sends its own `notify`, which names the exact step that failed. If the parent has no `reply`, a child with `reply: true` replies for itself.

If the `run-flow` step says `onFailure: continue`, the parent carries on and nobody replies: your flow decided to handle the failure.

## Stop a runaway: runLimits

A flow with an AI agent can spend a long time or a lot of tokens on one document. `spec.runLimits` puts a ceiling on a run and switches the flow off when runs keep hitting it. These are YAML and API settings: the console editor has no form for them.

| Field | Range | What it caps |
| --- | --- | --- |
| `maxTokens` | 1 or more | The tokens (input and output) the run's agents use, all steps together, counting what earlier steps used. Not set: no cap. |
| `maxMinutes` | 1 to 1440 | How long the run is at work: the time its steps are running, added up. Time waiting for a person, or held for a file lock, a budget or an evaluation slot, does not count, and neither does the wait for a child run. An agent still working past it is stopped. Not set: no cap. |
| `pauseAfter` | 0 to 20, default 3 | How many runs in a row that end on a cap suspend the flow. `0` never suspends. |

A cap that is reached stops the agent and fails its step with a message that says a limit stopped it. That is an ordinary failed run, so `onError` applies. An agent's own limits (turns, cost, tokens, time) count the same way for `pauseAfter`.

When the last `pauseAfter` runs of a flow all ended on a cap, Lumen suspends the flow, tells the workspace admins, and records the reason as `runaway` in the flow's annotations. Check the last run, fix the cause, and switch the flow back on. Only runs queued after the pause count toward the next one, so a flow you resume is not suspended again for the runs that paused it.

> **Token caps and credit budgets:**
> 
> Credit [budgets](https://docs.go-lumen.ai/he/concepts/credits-and-budgets.md) do not stop a model that costs nothing per token. `maxTokens` and `maxMinutes` do.

## Next steps

[Runs and retriesRetry a run, or run from a step without redoing the earlier ones.](https://docs.go-lumen.ai/he/operate/runs-and-retries.md)[AlertsGet told about failures across your workspace.](https://docs.go-lumen.ai/he/operate/alerts.md)[Run another flowWhat happens to a parent when its child fails.](https://docs.go-lumen.ai/he/build/run-another-flow.md)[AgentsGuardrails and limits on an AI step.](https://docs.go-lumen.ai/he/ai/agents.md)
