> For the complete documentation index, see [llms.txt](/llms.txt).
> A full single-fetch corpus is available at [llms-full.txt](/llms-full.txt).
---
last_verified: 2026-10-09
title: Workflows
description: Define workflows with @workflow, checkpoint steps, run work concurrently, keep state, and schedule runs
category: Python SDK
hideRightSidebar: true
---

A workflow is a durable program made of checkpointed steps. When a worker restarts, the workflow replays and each completed step returns its saved result instead of running again. For a guided introduction, see [Workflows](/docs/build/workflows.md).

## `@workflow`

```python
from agnt5 import FunctionContext, WorkflowContext, function, workflow


@function
async def double(ctx: FunctionContext, n: int) -> int:
    return n * 2


@workflow
async def double_twice(ctx: WorkflowContext, n: int) -> dict:
    once = await ctx.step(double, n)
    twice = await ctx.step(double, once)
    return {"once": once, "twice": twice}
```

```python
workflow(*, name=None, cron=None)
```

| Parameter | Description |
|---|---|
| `name` | The name the worker registers. Defaults to the Python function's name. |
| `cron` | A cron expression that starts the workflow on a schedule. |

```python
@workflow(name="nightly_report", cron="0 2 * * *")
async def nightly_report(ctx: WorkflowContext) -> dict:
    ...
```

Pass workflows to the worker's `workflows` list. See [Worker](/sdk/python/worker.md).

## Steps

### `ctx.step(fn, *args, key=None)`

Runs a function with the given arguments and checkpoints its result. On replay, a completed step returns the saved result without running the function again.

```python
total = await ctx.step(fetch_total, order_id)
```

To checkpoint an awaitable under a name, pass the name first: `await ctx.step("load_data", load_data())`.

`key` gives a step a stable identity. Use it when the same function runs in a loop or concurrently, so each call replays the right result:

```python
doubled = [await ctx.step(double, n, key=f"n-{n}") for n in (1, 2, 3)]
```

`ctx.task(fn, *args)` runs a function as a checkpointed step too, and `ctx.run(fn, *args)` is the same as `ctx.step(fn, *args)`.

### Run steps concurrently

`ctx.parallel()` waits for several awaitables and returns their results in order; `ctx.gather()` returns them by name:

```python
a, b = await ctx.parallel(
    ctx.step(double, 1, key="a"),
    ctx.step(double, 2, key="b"),
)

totals = await ctx.gather(
    sales=ctx.step(fetch_sales, report_id, key="sales"),
    inventory=ctx.step(fetch_inventory, report_id, key="inventory"),
)
# totals["sales"], totals["inventory"]
```

## Durable sleep

`ctx.sleep(seconds, name=None)` pauses the workflow durably:

```python
await ctx.sleep(24 * 60 * 60, name="wait_24h")
```

## Workflow state

`ctx.state` is run-scoped state that survives replay:

```python
ctx.state.set("phase", "started")
phase = ctx.state.get("phase")
```

## Wait for a person

`await ctx.wait_for_user(question, input_type="text", options=None, allow_custom=False, skippable=False)` pauses the workflow until someone answers. See [Context API](/sdk/python/context.md#wait-for-a-person).

## Next steps

* [Context API](/sdk/python/context.md): everything a workflow can do with `ctx`.
* [Functions](/sdk/python/functions.md): define the work that steps run.
* [Workflows guide](/docs/build/workflows.md): durability, replay, and idempotent side effects in practice.
