> ## Documentation Index
> Fetch the complete documentation index at: https://docs.roark.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Flow sequences

> Run several flows in order for the same customer: book an appointment, then move it, then cancel it

Some things your agent handles take more than one conversation. A customer books an appointment, calls back to move it, then calls again to cancel. To test that, the second call has to start after the first one has ended, and come from the same customer.

The flows on a [run plan](https://docs.roark.ai/documentation/simulation-testing/run-plans.md) can't promise that. Each flow is its own test case and they run in no particular order, so with iterations and concurrency the "move my appointment" call can start before that customer's booking exists. A **flow sequence** runs its flows in order, for one simulated customer at a time.

## When to use one

Use a flow sequence when a later conversation depends on what an earlier one left behind in your systems: a booking, a ticket, an order, a change to an account.

* Book, move, cancel an appointment
* Open a support ticket, then follow up on it
* Place an order, then ask where it is, then return it

When the conversations stand on their own, attach the flows as usual. A plan can hold both: plain flows and flow sequences run side by side in the same run.

## Building a sequence in the app

Sequences live in the **Flows** section of the create-run page and the plan editor, next to the flows you attach. A sequence shows as a card tagged **SEQUENCE**, with its flows in order and a one-line summary of its customers, wait and what happens if a flow doesn't complete.

![The Flows section of a run plan, 2 attached: an Opening hours flow card with its happy path, and a collapsed Book, reschedule, cancel card tagged SEQUENCE with 3 flows in order and the summary line Book an appointment, Reschedule the appointment, Cancel the appointment, 3 customers, 30 s between flows, stops if a flow does not complete. Below them are the Add or remove flows and Add a sequence of flows buttons.](https://mintcdn.com/roark/4ho-MUzD8QzcA_Lq/images/simulation-testing/flow-sequence-flows-section.png?fit=max\&auto=format\&n=4ho-MUzD8QzcA_Lq\&q=85\&s=8c3f6eb86bc861d3eb80b055e26714d0)

**1. Pick the flows, in order**

Click **+ Add a sequence of flows**. The picker opens as **New sequence of flows**: tick the flows in the order one customer goes through them, and the tick boxes number them as you go. A tick runs the flow's happy path; open a row to pick one of its edge cases instead. Click **Add sequence** once you have at least two.

![The New sequence of flows picker with the hint Pick flows in the order one customer goes through them. Book an appointment, Reschedule the appointment and Cancel the appointment are ticked and numbered 1, 2 and 3, each running its happy path; Opening hours is unticked. The footer reads 3 flows in order. Customers and the wait are set on the sequence, with an Add sequence button.](https://mintcdn.com/roark/4ho-MUzD8QzcA_Lq/images/simulation-testing/flow-sequence-picker.png?fit=max\&auto=format\&n=4ho-MUzD8QzcA_Lq\&q=85\&s=9027de111def714e764b47072211e1c9)

**2. Name it and add the customers**

The new sequence opens expanded. Give it a name, then add the customers who go through it. Each customer is a **Persona** and their own values for the flows' `{{variables}}`, such as an account number. **Calls from** shows the number the customer calls from: the persona's own number, or **Pool number, kept for the sequence**.

**3. Set the wait and what happens when a flow doesn't complete**

**Wait between flows** is the pause, in seconds, between one call ending and the next one starting: time for your systems to save what the last call changed. The rail shows it between the flows ("then, 30 s after this call ends"). **If a flow does not complete** chooses **Stop this sequence** (the default) or **Carry on**. You can reorder the flows with the arrows or by dragging, and swap a flow's variant from its menu.

![An expanded Book, reschedule, cancel sequence: three flows on a numbered rail, each on its happy path, with then, 30 s after this call ends between them. The Customers table lists Sarah Bennett, Jordan Ellis and Arjun Mehta, each calling from a pool number kept for the sequence, with their own preferredDay and accountNumber values. Below are Wait between flows set to 30 seconds, If a flow does not complete set to Stop this sequence, and the line 3 customers × 1 run × 3 flows = 9 calls. Up to 2 sequences at a time.](https://mintcdn.com/roark/4ho-MUzD8QzcA_Lq/images/simulation-testing/flow-sequence-card.png?fit=max\&auto=format\&n=4ho-MUzD8QzcA_Lq\&q=85\&s=8ce45d7e9311877f9c7eb0f9a025ac60)

On the **Flow adherence** and **Conversation quality** templates, the flows sit in the template's own panel (**Expected flows** on Flow adherence). A sequence there is a ticked row tagged **SEQUENCE** at the top of the list that opens to the same settings. Untick it to leave it out of this run without losing its setup.

![The Expected flows panel of the Flow adherence template: a ticked Book, reschedule, cancel row tagged SEQUENCE, reading 3 flows in order, 3 customers, 30 s between flows, sits above four unticked flows. At the bottom are the Add a sequence of flows and Define a new flow buttons.](https://mintcdn.com/roark/4ho-MUzD8QzcA_Lq/images/simulation-testing/flow-sequence-flow-adherence.png?fit=max\&auto=format\&n=4ho-MUzD8QzcA_Lq\&q=85\&s=8c2ad4d56b89bf076a9e35e4da4eed10)

The run settings count the calls a plan places, sequences included, for example "40 calls planned: 36 in sequences, 4 from 1 single flow". The plan won't save until every sequence has a name, at least two flows and a persona for each customer.

> [!NOTE]
>
> Sequences are available on the Blank, Flow adherence and Conversation quality templates. They can't be combined with a [property sweep](https://docs.roark.ai/documentation/simulation-testing/templates#property-sweeps).

## How a sequence runs

Each customer going through the sequence once is one **sequence run**. Within a sequence run:

* **Flows run in order.** Each flow is its own call, and it starts only after the one before it has ended and the wait has passed. Nothing overtakes it: not another flow, not a retry, not a later iteration of the same customer.
* **One caller number.** Every call in a sequence run comes from the same number, so your agent sees the same customer calling back. A persona with its own number always uses it. Otherwise the first call takes a number from Roark's pool and the sequence run keeps it; if that number is busy, the next call waits for it rather than switching. A later iteration may call from a different number.
* **One persona and one set of values.** The customer's persona plays every flow, with that customer's values.
* **A failed check never stops a sequence.** Checks grade a call after it ends, and a booking can succeed on a call that fails a tone check, so the sequence carries on.
* **A flow that doesn't complete stops the sequence by default.** "Doesn't complete" means the call didn't properly happen: no answer, busy, declined, your agent never spoke, the call hit its maximum duration, or it was invalidated, timed out or cancelled. Under **Stop this sequence** the remaining flows are skipped for that customer and never dial; other customers carry on. Under **Carry on** they still run, which suits flows that don't depend on each other.

**Concurrency counts sequence runs, not calls.** At a concurrency of 2, at most two customers are part-way through a sequence at once, whatever flow each is on. Different customers run in parallel, and one customer's sequence runs never overlap.

**Iterations repeat each customer's whole sequence.** A customer's iterations don't have to finish in order (iteration 4 can end before iteration 2 starts), but each one runs its flows in order.

A sequence adds **customers × flows × iterations** test cases for each agent you test. Flows skipped after a stop place no call, so a run can place fewer calls than it has test cases. If the plan retries calls where your agent never spoke, a retried flow places one more call before the sequence moves on.

## Rules and limits

| Setting | Limit |
| :- | :- |
| Flows in a sequence | 2 to 10 |
| Customers in a sequence | Up to 50. With none, the sequence runs as its first flow's persona |
| Sequences in a plan | Up to 20 |
| Wait between flows | 0 to 900 seconds, default 0 |

* **Variables layer.** A customer's value wins over a value set on the flow in the sequence, both win over the plan's, and values given when the run starts win over all of them.
* **A flow can appear more than once** in the same sequence, for example to check the booking at the start and again at the end.
* **A sequence can set its own iteration count** over the API or in config as code. Otherwise it follows the plan's.

## Reading the results

### While the run is going

**Live now** nests a sequence under one header with its name, the customer and the iteration, and lists its flows on a numbered rail. The flow on a call shows that call's live line. Flows that ran show their result and call time, a flow that just ended shows **Collecting metrics**, and later ones read **Waiting** or **Next**. A flow waiting out the time between flows counts down ("Starts in 0:18").

![Live now during a run with two Book, reschedule, cancel sequences. Sarah Bennett, iteration 2 of 4: Book an appointment completed in 2m 04s, Reschedule the appointment in call at 01:46, Cancel the appointment waiting. Arjun Mehta, iteration 2 of 4: Book an appointment completed, Reschedule the appointment collecting metrics, Cancel the appointment next.](https://mintcdn.com/roark/4ho-MUzD8QzcA_Lq/images/simulation-testing/flow-sequence-live-now.png?fit=max\&auto=format\&n=4ho-MUzD8QzcA_Lq\&q=85\&s=ff47d94590b4888a3080caa123f19036)

### The calls list

In **All calls**, each sequence run is one row: the sequence name, the customer, the iteration, a trail with a dot per flow, and one result: **Passed**, **Failed checks**, **Completed**, or **Stopped at step N** with the reason underneath. Open the row to see its calls in order. The flow that didn't complete is marked with a red X, and a skipped flow stays in its place, dimmed, and opens nothing. Filter by sequence, or by **Single flows** for the plain ones.

![All calls with the summary 3 sequence runs: 1 completed, 2 stopped. 3 steps were skipped and not billed. Filters include one for the Book, reschedule, cancel sequence and one for Single flows. Arjun Mehta's sequence run is open and Stopped at step 2: Book an appointment completed, Reschedule the appointment timed out at its maximum duration and is marked with a red X, and Cancel the appointment is dimmed as Skipped. Below are a plain Opening hours call, Sarah Bennett's run Stopped at step 1, and Jordan Ellis's run Completed, open to its three completed calls.](https://mintcdn.com/roark/4ho-MUzD8QzcA_Lq/images/simulation-testing/flow-sequence-all-calls.png?fit=max\&auto=format\&n=4ho-MUzD8QzcA_Lq\&q=85\&s=0e733dc42da286c32648cc9f2ed0fa81)

A line under **All calls** sums it up, for example "3 sequence runs: 1 completed, 2 stopped. 3 steps were skipped and not billed."

> [!NOTE]
>
> **A stopped sequence shows in the run even without checks.** With checks on the plan, a stopped sequence fails the run's coverage, because the skipped flows still count as expected calls. A run whose every sequence stopped, and which has no plain flows, ends as Failed.

## Config as code

Declare sequences on a [`kind: simulationPlan`](https://docs.roark.ai/documentation/config-as-code/simulation-plans#flow-sequences) under `flowSequences`. Flows and personas are named the way the rest of your config names them, and a step runs the flow's happy path unless `edgeCase` names one of its edge cases.

```yaml theme={"theme":{"light":"everforest-light","dark":"everforest-dark"}}
# yaml-language-server: $schema=https://roark.ai/roark-config.schema.json
kind: simulationPlan
name: book-reschedule-cancel
direction: INBOUND
agentEndpoints:
  - agent: frontdesk
    value: '+15551234567'
maxDurationSeconds: 300
metrics: [agent_expectations_check]
iterations: 2
maxConcurrentJobs: 2
flowSequences:
  - name: Book, reschedule, cancel
    steps:
      - flow: book-appointment
      - flow: reschedule-appointment
      - flow: cancel-appointment
    waitBetweenStepsSeconds: 30
    onStepFailure: stop # or continue
    customers:
      - persona: sarah-bennett
        variables: { accountNumber: BSD-10482, preferredDay: Tuesday }
      - persona: jordan-ellis
        variables: { accountNumber: BSD-11937, preferredDay: Thursday }
```

That plan has 12 test cases: 2 customers × 3 flows × 2 iterations. Sarah's and Jordan's sequences run side by side. Each reschedule call starts 30 seconds after that customer's booking call ends, from the same number. If Jordan's reschedule call isn't answered, that customer's cancel call is skipped, and their second iteration still runs. To run a flow's edge case instead of its happy path, add `edgeCase` with the edge case's name.

## API

`POST /v1/simulation/plan` takes `flowSequences`, as does the inline `plan` of `POST /v1/simulation/run`. A plan needs `flows`, `flowSequences`, or both. Each step names its flow by `id` (or a Roark flow by `slug`), with an optional `edgeCase` by `id` or `slug`.

```bash theme={"theme":{"light":"everforest-light","dark":"everforest-dark"}}
curl -X POST "https://api.roark.ai/v1/simulation/plan" \
  -H "Authorization: Bearer $ROARK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Book, reschedule, cancel",
    "direction": "INBOUND",
    "agentEndpoints": [{ "id": "endpoint-uuid" }],
    "maxSimulationDurationSeconds": 300,
    "metrics": [{ "slug": "agent_expectations_check" }],
    "iterationCount": 2,
    "maxConcurrentJobs": 2,
    "flowSequences": [
      {
        "name": "Book, reschedule, cancel",
        "steps": [
          { "id": "book-flow-uuid" },
          { "id": "reschedule-flow-uuid" },
          { "id": "cancel-flow-uuid" }
        ],
        "waitBetweenStepsSeconds": 30,
        "onStepFailure": "STOP",
        "customers": [
          { "personaId": "sarah-persona-uuid", "variables": { "accountNumber": "BSD-10482" } },
          { "personaId": "jordan-persona-uuid", "label": "Jordan E.", "variables": { "accountNumber": "BSD-11937" } }
        ]
      }
    ]
  }'
```

Read `testCaseCount` on the created plan before you start it.

**Updating.** `GET /v1/simulation/plan/{planId}` always returns `flowSequences` (`[]` when there are none). `PUT /v1/simulation/plan/{planId}` replaces the whole list: leave `flowSequences` out to keep the sequences, send `[]` to remove them all, and send each sequence you keep back with the `id` the read gave it. A sequence sent without an `id` is created as a new one.

**Results.** `GET /v1/simulation/plan/job/{jobId}` and `GET /v1/simulation/plan/job/{jobId}/results` return `flowSequenceRuns` (one entry per sequence run, with its `status`, `stoppedAtStep`, `stopReason` and each step's `simulationJobId` and `outcome`) and `flowSequenceSummary`:

```json theme={"theme":{"light":"everforest-light","dark":"everforest-dark"}}
"flowSequenceSummary": { "total": 4, "completed": 3, "stopped": 1, "running": 0, "pending": 0, "cancelled": 0, "skippedSteps": 1 }
```

| Sequence run `status` | Meaning |
| :- | :- |
| `PENDING` | Its first flow hasn't started |
| `RUNNING` | Part-way through |
| `COMPLETED` | Every flow ran |
| `STOPPED` | A flow didn't complete and the rest were skipped. `stopReason` says why, for example "Step 2 did not complete: no answer." |
| `CANCELLED` | The run was cancelled |

A step's `outcome` is `WAITING`, `IN_PROGRESS`, `COMPLETED` (the call happened, whatever its checks scored), `DID_NOT_COMPLETE` or `SKIPPED`. A skipped step is a simulation job with status `CANCELLED` and `flowSequence.skipped: true`, and it placed no call, so don't count it as a call that failed or that someone cancelled. Every job's `flowSequence` gives its `flowSequenceRunId` and `step`, or `null` outside a sequence.

Errors to handle:

| Status | When |
| :- | :- |
| `400` | A sequence has fewer than 2 or more than 10 steps, more than 50 customers, or a wait over 900 seconds; a plan has more than 20 sequences or nothing to run; a step names its flow by both `id` and `slug` |
| `400` | `flow_sequence_with_sweep_unsupported`: a sequence on a plan that sweeps a property |
| `400` | `flow_sequence_step_unresolved`: a step doesn't resolve to exactly one conversation |
| `404` | An unknown flow or edge case slug |

## Next steps

> [card] [Run Plans](https://docs.roark.ai/documentation/simulation-testing/run-plans.md)
>
> Everything else a plan holds, and how to run it

> [card] [Customer Flows](https://docs.roark.ai/documentation/simulation-testing/customer-flows.md)
>
> Author the flows a sequence runs

> [card] [Variables](https://docs.roark.ai/documentation/simulation-testing/variables.md)
>
> Give each customer their own values

> [card] [Running Simulations](https://docs.roark.ai/documentation/simulation-testing/running-simulations.md)
>
> Watch runs live and read the report when they settle


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.