Attempt: Retry. Fallback. Recover.

maintainedTool

Attempt for Laravel

A Laravel package for retries, exponential backoff, and fallback chains. Wrap a call, choose how it retries, and define what happens if it fails.

Source ↗packagist.org ↗

installcomposer require yannelli/attempt
Languages
Laravel, PHP

LaravelPHPError HandlingRetry LogicResilienceFault Tolerance


What it does

The package keeps retry rules and fallbacks beside the call they apply to. Wrap a closure or class, choose which failures to retry, and define a fallback if the retries fail.

Features

  • Retry selected exceptions with fixed delays, backoff, or jitter.
  • Run fallback handlers in order until one succeeds.
  • Add hooks for retries, success, and failure.
  • Wrap individual stages in Laravel’s Pipeline.
  • Use Attempt::ai() with the Laravel AI SDK to retry transient provider failures.

Install

PHP 8.4 or newer. Laravel 12 or 13.

composer require yannelli/attempt

Optional config:

php artisan vendor:publish --tag="attempt-config"

Example

This example tries PrimaryApi up to four times, with exponential backoff between retries. If those calls fail, it runs BackupApi:

use Yannelli\Attempt\Facades\Attempt;

$result = Attempt::try(PrimaryApi::class)
    ->retry(3)
    ->exponentialBackoff(base: 100, max: 5000)
    ->fallback(BackupApi::class)
    ->thenReturn();

retry(3) allows three retries after the first call. Use run() in place of thenReturn() to get an AttemptResult with the result, exception, and execution count.