﻿# Prefect retry conditions filter failed task attempts

Prefect retry_condition_fn decides whether a failed task may retry; remaining attempts, delay and a returned business rejection are separate parts of the execution contract.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/guide-prefect-retry-condition-failed-state/
Language: en

## Classify the failure before spending another attempt

A document-fetch task can fail because its service is temporarily unavailable or because the requested identifier is invalid. Repeating those cases has different value. Prefect's [retry guide](https://www.prefect.io/docs/v3/how-to-guides/workflows/retries) provides `retry_condition_fn` for deciding dynamically whether another attempt is appropriate. Begin with the failures the application can actually distinguish, rather than treating every unsuccessful document request as the same transient event.

## Read the callback's failure state

The [task reference](https://reference.prefect.io/prefect/tasks/) describes a callback receiving the task, task run and state. The state carries the failure being considered. The guide demonstrates examining an exception obtained from that state and returning a Boolean decision. Define which error classes are retryable in this application's environment. An absent document may be permanent in an immutable catalog but temporary during an explicitly documented indexing delay.

## Keep eligibility separate from the remaining budget

The current [task engine](https://raw.githubusercontent.com/PrefectHQ/prefect/main/src/prefect/task_engine.py) requires both remaining retries and a permitting condition. Returning true cannot replenish an exhausted budget. Returning false can stop further attempts while budget remains. Delay and jitter configure timing after eligibility is established. This separation lets a reviewer understand whether execution ended because the error was excluded or because the allowed attempts had already been consumed.

## Do not conceal failure inside ordinary output

A plain result describing an unsupported document is a business outcome unless the task deliberately represents it through a failure mechanism. The condition is not a general validator that scans every returned dictionary for unhappy wording. Choose whether unsupported input should end normally with an explicit rejection or should fail the workflow. [Task contracts](/journal/learn-codex-task-contract/) help make that distinction visible to the downstream consumer before retry rules are added.

## Keep the predicate simple enough to trust

The engine catches an exception raised inside the retry-condition check, logs it and declines the retry. A broken predicate can therefore remove the recovery opportunity it was intended to control. Use a small, explicit classification and preserve the original error alongside the predicate decision. [Tool observations](/journal/learn-codex-tool-observation/) should show actual attempts and their outcomes, so a final failure is explainable without inferring retry behavior from a configured count alone.
