# Run another flow

> Hand a document to a second flow with the Run another flow step, wait for it to finish, and go on with what it found. This is how a router flow works.

Source: https://docs.go-lumen.ai/he/build/run-another-flow

One flow can start another on the same documents and wait for it. A router flow uses this to read an email, decide what it is, hand it to the flow that handles that kind, and go on with the result. Each flow keeps its own steps, settings and cost.

-   Build
-   Two flows
-   Same workspace

Two things make it work. The flow that calls has a **Run another flow and wait** step ([`run-flow`](https://docs.go-lumen.ai/he/reference/steps/run-flow.md)). The flow that is called starts with the **Another flow starts it** trigger ([`flow-called`](https://docs.go-lumen.ai/he/reference/triggers/flow-called.md)). A flow without that trigger cannot be called.

*Diagram: A router flow calling one of two child flows.* An email arrives and starts the router flow. A classify step decides what kind of email it is, and a branch sends the run down one of two paths. On the first path, for invoices, a Run another flow step starts the invoice flow on the same documents. On the second path, for statements, a Run another flow step starts the statement flow. Each Run another flow step waits while its child flow runs. The child flows each start with the Another flow starts it trigger. When the child ends, the router run wakes up, reads the child's status and output, and goes on with the last step, which uses that result.

*A router flow decides, hands the email to a child flow, waits for that child run to end and goes on with what it produced.*

## Make a flow callable

Give the flow you want to call a `flow-called` trigger in place of its email trigger. Name the trigger `email` if the flow already reads `{email.from}` or `{email.subject}`: the trigger hands over the email the parent run started with, so every `{email.*}` placeholder keeps working. It also adds two outputs, `parentRun` and `parentFlow`, which say who called.

```yaml title="runflow-invoice-handler.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: runflow-invoice-handler
  namespace: tenant-acme
spec:
  displayName: Handle an invoice
  triggers:
    - id: email
      title: Another flow starts it
      ref: {name: flow-called}
      config: {accepts: email}
      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}
```

`accepts` takes one value today, `email`. A flow can have its own email trigger and a `flow-called` trigger side by side, so it still starts from the inbox when you want it to.

## Call it from a router flow

The router reads the email, decides, and hands it on. Each `run-flow` step names one flow of the same workspace, by name. The name is fixed: it cannot hold a placeholder.

```yaml title="runflow-mail-router.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: runflow-mail-router
  namespace: tenant-acme
spec:
  displayName: Route supplier mail
  triggers:
    - id: email
      ref: {name: email-arrives}
      connectorRef: {name: invoices-inbox}
      next: [kind]
  nodes:
    - id: kind
      title: What kind of email is it?
      stepRef: {name: classify}
      config:
        categories:
          - {name: invoice, description: A supplier invoice}
          - {name: statement, description: A monthly account statement}
      next: [which]
    - id: which
      title: Which flow?
      branch:
        - when: {field: kind.category, op: eq, value: invoice}
          next: [invoice]
        - when: {field: kind.category, op: eq, value: statement}
          next: [statement]
    - id: invoice
      title: Handle the invoice
      stepRef: {name: run-flow}
      config:
        flow: runflow-invoice-handler
        timeoutHours: 24
        onTimeout: fail
        onFailure: continue
      next: [tell-invoice]
    - id: statement
      title: Handle the statement
      stepRef: {name: run-flow}
      config:
        flow: runflow-statement-handler
      next: [tell-statement]
    - id: tell-invoice
      title: Tell the team
      stepRef: {name: notify-in-app}
      config:
        to: admins
        title: Invoice handled
        body: "Status: {invoice.status}. Error: {invoice.error}"
    - id: tell-statement
      title: Tell the team
      stepRef: {name: notify-in-app}
      config:
        to: admins
        title: Statement handled
        body: "Status: {statement.status}"
---
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: runflow-statement-handler
  namespace: tenant-acme
spec:
  displayName: Handle a statement
  triggers:
    - id: email
      ref: {name: flow-called}
      next: [keep]
  nodes:
    - id: keep
      stepRef: {name: save-file}
      config: {destination: lumen}
```

The router's classify step is the only AI in it, and only the path it picks runs. A branch of plain rules (sender, subject, file type) would route for free: use that first, and keep the classify step for what the rules cannot place.

### The step's settings

| Setting | YAML key | Default | What it does |
| --- | --- | --- | --- |
| Flow to run | `flow` | none, required | The flow to start, by name. It must have an **Another flow starts it** trigger. |
| Wait at most (hours) | `timeoutHours` | 168 (7 days) | How long to wait for the child. From 1 to 720. |
| If it takes too long | `onTimeout` | `fail` | `fail` fails the step. `continue` goes on without the result. |
| If it fails | `onFailure` | `fail` | `fail` fails the step. `continue` goes on and says the child failed. |

The step costs nothing itself. The child is billed as any run of its own flow, with its own credit lines and its own budget.

## What the child receives

The child gets the parent's documents as they arrived: the same email, the same files, the same arrival. Nothing is ingested again and nothing is copied. It does not get the router's own fields, such as `kind.category`, so the child behaves exactly as if the email had been sent to it directly.

A child is a run of its own. It has its own steps, its own attempts, its own page and its own `runLimits`. If it asks a person a question, the parent keeps waiting, and the person's reply goes to the child, never to the parent.

## What comes back

When the child ends, the parent run wakes up and the step adds these fields, named after the step's id (here `invoice`):

| Field | Holds |
| --- | --- |
| `{invoice.status}` | `succeeded`, `failed`, `timedOut` or `held`. |
| `{invoice.outcome}` | How the child ended, for example `succeeded` or `filtered`. |
| `{invoice.error}` | The child's error, when it failed. |
| `{invoice.timedOut}` | `true` when the deadline passed first. |
| `{invoice.childRun}` | The child's run id. Empty when the child could not start. |
| `{invoice.flow}` | The child's flow name. |
| `{invoice.output}` | The child's final fields as one object, nested by step: `{invoice.output.details.supplier}`. |

Branch on `invoice.status` to treat a failure differently from a success. See [Data and placeholders](https://docs.go-lumen.ai/he/concepts/data-and-placeholders.md) for how fields are named.

### When the child does not finish well

| What happened | `onFailure` or `onTimeout` is `fail` | It is `continue` |
| --- | --- | --- |
| The child failed or was cancelled | The step fails, naming the child's flow, its failed step and its error, with a link to the child run. A failure the step reports is not retried. | The step succeeds with `status` set to `failed` and the reason in `error`. |
| The child is not done in time | The step fails, saying how long it waited. | The step succeeds with `status` set to `timedOut` (or `held`, if the child was still held, for example for a budget) and `timedOut` set to `true`. |
| The child could not start (no such flow, it has no `flow-called` trigger, too deep, would run itself) | The step fails. | The step succeeds with `status` `failed` and an empty `childRun`. |

Lumen does not cancel the child when the parent gives up. A parent that timed out leaves its child running, and the child's result stays on the child.

## Limits and safety

-   **Depth.** A tree of flows goes at most 3 deep, the first included: a router, the flow it calls, and one more. A flow at the third level cannot call another.
-   **No loops.** A flow cannot call itself, directly or through others. Lumen refuses to save such a flow and checks again before it starts a child.
-   **Same workspace only.** A step names a flow of its own workspace. Calling one grants nothing the flow's editor did not already have: the child runs with its own settings, connections and guardrails.
-   **Not inside a repeat.** A `run-flow` step in a [repeat for each item](https://docs.go-lumen.ai/he/build/repeat-for-each-item.md) body is not supported, because the child takes the trigger's documents, not the item.
-   **One child per step.** The child's id comes from the step, so when the step is retried after a worker is lost, Lumen finds the child it already started and waits for it again. It never starts a second one.
-   **A frozen copy.** The child's flow is copied into the parent's snapshot when the parent run is queued. Editing the child while a router run waits changes neither.

The waiting does not use up the parent's time limit: `maxMinutes` counts only the time a run's steps are working. The child's time counts against the child's own limit.

## See both runs

A `run-flow` step shows on the parent's run page like any step. Open it, and a card names the child flow, shows its status, the credits it used and a link to its run. While the child runs, the parent shows **Waiting**.

The child's page says who started it, in the line under its run id: **Started by** the parent flow, and the step that called it.

Retrying a child directly is a run of its own. Its result does not flow back to the router. Retrying or running from a point in the parent offers to reuse the child's earlier result or to start a new child: see [Runs and retries](https://docs.go-lumen.ai/he/operate/runs-and-retries.md).

## Test it

Testing a `run-flow` step in the editor is a dry run: it says which flow it would start and how long it would wait, and starts nothing. In an [evaluation](https://docs.go-lumen.ai/he/build/test-and-evaluate.md) the child runs as a test run too, and the evaluation scores the router's run.

## Next steps

[Branches and filtersRoute by rules before you reach for AI.](https://docs.go-lumen.ai/he/build/branches-and-filters.md)[When a run failsRetries, and what a flow does when a run ends failed.](https://docs.go-lumen.ai/he/build/when-a-run-fails.md)[Test and evaluateCheck a router against a table of test emails.](https://docs.go-lumen.ai/he/build/test-and-evaluate.md)[Runs and retriesRead a run, retry it, or run from a step.](https://docs.go-lumen.ai/he/operate/runs-and-retries.md)
