> 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: Functions
description: Define functions with @function, configure retries, and call them from workflows
category: Python SDK
hideRightSidebar: true
---

A function is one unit of work with retries and tracing. Define it with the `@function` decorator. For a guided introduction, see [Functions](/docs/build/functions.md).

## `@function`

```python
from agnt5 import FunctionContext, function


@function
async def send_email(ctx: FunctionContext, to: str, subject: str) -> str:
    ctx.logger.info(f"Sending email to {to}")
    return f"Sent to {to}"
```

```python
function(*, name=None, retries=None, backoff=None, timeout_ms=None)
```

| Parameter | Description |
|---|---|
| `name` | The name the worker registers. Defaults to the Python function's name. |
| `retries` | Total tries including the first, as an `int`, or a `RetryPolicy`. |
| `backoff` | `"constant"`, `"linear"`, or `"exponential"`, or a `BackoffPolicy`. |
| `timeout_ms` | Fail a try that runs longer than this. Use it with `async` functions. |

```python
@function(name="send_email", retries=5, backoff="exponential")
async def send_email(ctx: FunctionContext, to: str, subject: str) -> str:
    return f"Sent to {to}"
```

When every try fails, the run fails with the last error. `ctx.attempt` is `0` on the first try, `1` on the first retry, and so on.

A function registers when its module is imported. See [Worker](/sdk/python/worker.md).

## The handler

The first parameter is always the `FunctionContext`, which the runtime passes in. The other parameters are filled from the run's JSON input by name, so a function with `to` and `subject` parameters takes the input `{"to": "...", "subject": "..."}`. The handler can be `async` or a plain function, and its return value must be JSON-serializable.

| `ctx` member | Description |
|---|---|
| `ctx.run_id` | The ID of the current run. |
| `ctx.attempt` | Which try this is, starting at `0`. |
| `ctx.logger` | A standard Python logger for the run. |

See [Context API](/sdk/python/context.md) for the rest.

## Call a function

| From | Call | Checkpointed |
|---|---|---|
| A workflow | `await ctx.step(send_email, "a@example.com", "Hi")` | Yes. On replay the saved result is returned. |
| The CLI | `agnt5 run send_email --input '{"to": "a@example.com", "subject": "Hi"}'` | Its own run. |

```python
from agnt5 import WorkflowContext, workflow


@workflow
async def welcome(ctx: WorkflowContext, email: str) -> dict:
    sent = await ctx.step(send_email, email, "Welcome!")
    return {"sent": sent}
```

See [Workflows](/sdk/python/workflows.md) for `ctx.step()` options.

## `@step`

A step is workflow-local work with the same retry settings as a function. It isn't registered as a component, so `agnt5 run` and `Client.run()` can't call it. Run it only with `ctx.step()`.

```python
from agnt5 import StepContext, WorkflowContext, step, workflow


@step(retries=3, backoff="exponential", timeout_ms=10000)
async def send_welcome_email(ctx: StepContext, to: str) -> str:
    # pass ctx.activation.idempotency_key to your email provider
    return f"Sent to {to}"


@workflow
async def onboard_user(ctx: WorkflowContext, email: str) -> str:
    return await ctx.step(send_welcome_email, email, key="welcome-email")
```

```python
step(*, name=None, retries=None, backoff=None, timeout_ms=None)
```

The parameters match [`@function`](#function). The `ctx` parameter is optional: name the first parameter `ctx` to receive a `StepContext`, which adds `ctx.activation.idempotency_key` to the members above. The key stays the same across retries of one step.

Calling the step directly raises `RuntimeError`. Durable waits such as `ctx.sleep()` must go between steps, not inside one. See [Steps](/docs/build/functions.md#steps) for when to choose a step over a function.

## Next steps

* [Workflows](/sdk/python/workflows.md): run functions as checkpointed steps.
* [Context API](/sdk/python/context.md): everything a handler can do with `ctx`.
* [Functions guide](/docs/build/functions.md): retries and timeouts in practice.
