---
title: "Fail Fast on Permanent Errors"
description: "Use this pattern when a provider call can fail in two ways: a transient error that a retry may fix, and a permanent error that no retry can fix."
canonical_url: "https://docs.superdurable.io/design-patterns/failure-handling/fail-fast-on-permanent-errors/"
source: "docs/content/design-patterns/failure-handling/fail-fast-on-permanent-errors.mdx"
locale: "en"
language: "python"
---

> Index of every page for AI agents: https://docs.superdurable.io/llms.txt

# Fail Fast on Permanent Errors

Code on this page is in Python. Other languages: [Go](https://docs.superdurable.io/design-patterns/failure-handling/fail-fast-on-permanent-errors.go.md) · [Java](https://docs.superdurable.io/design-patterns/failure-handling/fail-fast-on-permanent-errors.java.md) · [TypeScript](https://docs.superdurable.io/design-patterns/failure-handling/fail-fast-on-permanent-errors.typescript.md) · [Rust](https://docs.superdurable.io/design-patterns/failure-handling/fail-fast-on-permanent-errors.rust.md)

Use this pattern when a provider call can fail in two ways: a transient error that a retry may fix, and a permanent error that no retry can fix.

An example is an expense claim paid through a payment provider. A timeout is transient: the next attempt may succeed. An invalid bank account is permanent: the provider has rejected the request, and every retry gets the same answer.

Step retry retries every error that Step code raises, under the Step's retry policy. In the example the retry scheduling budget is one hour, so a permanent error that `Execute` raises keeps being retried until that budget runs out. Classify the error in `Execute` instead, and route a permanent error at once.

**Flow graph: ExpenseReimbursementFlow**

**Steps**

1. `SubmitPayout` (start Step): Submit the payout; retry a timeout and route a rejection at once. Next: `RecordRejectedClaim`, or the Flow completes. Writes `PayoutOutcome`.
2. `RecordRejectedClaim`: Record the rejected claim and its reason. Next: the Flow completes. Writes `PayoutOutcome`.

Example: [examples/python/dex_examples/patterns/fail-fast/expense_reimbursement_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/fail-fast/expense_reimbursement_flow.py)

## How it works

- Make provider writes in `Execute`. `Execute` sees the provider's error and decides what it means. A read-only call in `WaitFor` hands a permanent error to `Execute` instead; see [WaitFor and Execute](https://docs.superdurable.io/concepts/step.md#waitfor-and-execute).
- Let a transient error fail the attempt. Throw it or return it. Step retry runs the call again under the Step's own retry policy.
- Catch a permanent error and return a Step decision. Go to a failure-handling Step that records the business failure. Return `ForceFail` instead when the whole Flow must be marked failed at once.
- Treat an error as permanent only when the provider confirms it, such as rejected input or an invalid request. Leave an unknown error transient, so retry still covers it.
- Size the retry policy for transient errors only. A routed permanent error never consumes it.
- Prefer to put calls that can fail in `Execute`. `Execute` returns the decision that routes the failure, in the same call that saw it.
- Once the rejection decision commits, that error never retries `SubmitPayout`. The failure-handling Step is a Step of its own, so Dex can still retry it independently.
- Keep the failure record free of sensitive input. The example records a fixed reason and never the bank account number.

In the example:

1. `ExpenseReimbursementFlow` starts `SubmitPayout` with the claim. The Flow ID is the claim ID and the provider's idempotency key.
2. `SubmitPayout` calls the payment provider. On success, it records the payout in the `PayoutOutcome` Attribute and completes the Flow.
3. When the provider times out, `SubmitPayout` lets the error fail the attempt. Step retry calls the provider again with backoff, until the one-hour retry scheduling budget runs out.
4. When the provider rejects the bank account, `SubmitPayout` goes to `RecordRejectedClaim` with a fixed reason. `SubmitPayout` is not retried for this error.
5. `RecordRejectedClaim` records the rejection in `PayoutOutcome` and completes the Flow.

## Example

The fake provider accepts only a bank account of eight ASCII digits and rejects any other. It times out on the first submission of every valid claim, then pays it. Run the HTTP sample from the [examples playground](https://github.com/superdurable/dex/tree/main/examples/playground).

**Python**

```python
class RecordRejectedClaim(Step[ClaimRejection]):
    def execute(self, context: Context, input: ClaimRejection) -> StepDecision:
        PAYOUT_OUTCOME.set(
            context, PayoutOutcome("REJECTED", input.reason, input.submit_attempt)
        )
        return graceful_complete(f"rejected: {input.reason}")


class SubmitPayout(Step[ExpenseClaim]):
    def __init__(self, provider: FakePaymentProvider) -> None:
        self.provider = provider

    def get_step_options(self) -> StepOptions:
        return StepOptions(
            execute_retry=RetryPolicy(
                initial_interval=timedelta(seconds=1),
                backoff_coefficient=2.0,
                maximum_interval=timedelta(minutes=5),
                total_duration=timedelta(hours=1),
            )
        )

    def execute(self, context: Context, input: ExpenseClaim) -> StepDecision:
        try:
            payout_id = self.provider.submit_payout(context.flow_id, input)
        except PayoutRejectedError as rejection:
            return go_to(
                RecordRejectedClaim, ClaimRejection(str(rejection), context.attempt)
            )
        PAYOUT_OUTCOME.set(context, PayoutOutcome("PAID", payout_id, context.attempt))
        return graceful_complete(payout_id)
```

Example: [examples/python/dex_examples/patterns/fail-fast/expense_reimbursement_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/fail-fast/expense_reimbursement_flow.py)

## Test both routes

Test with the Step's real retry policy. A test that shortens the policy no longer tests the route.

- Make a fake provider return a permanent error. Assert the terminal state and that the outcome came from the first attempt of `SubmitPayout`. When the test can observe the fake provider, also assert that it was called exactly once.
- Make the fake provider return a transient error once. Assert that the Step retries and then succeeds on the second attempt.

The examples read the attempt from the `PayoutOutcome` Attribute, which records the attempt that produced the outcome.

## Compared with Execute Failure Recovery

[Execute Failure Recovery](https://docs.superdurable.io/design-patterns/failure-handling/execute-failure-recovery.md) handles a failure that is still there after the retry policy is exhausted. Its `ExecuteFailure` policy moves the Flow to a recovery Step, which receives the failed Step's input.

Fail Fast on Permanent Errors decides before any retry. `Execute` itself returns the decision, so the failure-handling Step receives whatever input `Execute` builds, such as the rejection reason.

Use both together when a Step needs both: fail fast on a confirmed permanent error, and add an `ExecuteFailure` route for transient errors that outlast the retry policy.

## Next

- [Manual Recovery](https://docs.superdurable.io/design-patterns/failure-handling/manual-recovery.md)
- [Retry and failure policies](https://docs.superdurable.io/reference/python/failure-and-retries.md#retry-and-failure-policies)
- [Failure Handling](https://docs.superdurable.io/design-patterns/failure-handling.md)
