Handle Failed AI Requests Safely in Laravel | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com) - [Home](https://www.msaied.com)
- [Projects](https://www.msaied.com/projects)
- [Articles](https://www.msaied.com/articles)
- [Certificates](https://www.msaied.com/certificates)
- [About](https://www.msaied.com#about)

           [  Contact](https://www.msaied.com#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com)
- [ProjectsCase studies](https://www.msaied.com/projects)
- [ArticlesEngineering notes](https://www.msaied.com/articles)
- [CertificatesCredentials](https://www.msaied.com/certificates)
- [AboutHow I work](https://www.msaied.com#about)
- [ContactGet in touch](https://www.msaied.com#contact)

  [Start a conversation](https://www.msaied.com#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com)
2. /
3. [Articles](https://www.msaied.com/articles)
4. /
5. [Laravel](https://www.msaied.com/articles?category=laravel)
6. /
7. Decide with Jev Episode 5: Make Failed AI Requests Safe in Laravel

   [Laravel](https://www.msaied.com/articles?category=laravel) [AI](https://www.msaied.com/articles?category=ai) 

 Decide with Jev Episode 5: Make Failed AI Requests Safe in Laravel
===================================================================

 Episode 5 of Decide with Jev shows how to give every failed AI request its own explicit result in Laravel, so a stale "ready for editor" label can never survive a timed-out check.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com#person) Published 22 Nov 2026 · Updated 25 Nov 2026 · 4 min read

ShareCopy linkCopied

 ![Decide with Jev Episode 5: Make Failed AI Requests Safe in Laravel](https://cdn.msaied.com/835/85710585fb2cbe5fd3fbf12fe76c647b.png) 

  On this page +1. [The Problem: Stale AI Results That Look Like Successes](#the-problem-stale-ai-results-that-look-like-successes)
2. [The Fix: Every Check Starts a Fresh Run](#the-fix-every-check-starts-a-fresh-run)
3. [Other Topics Covered in Episode 5](#other-topics-covered-in-episode-5)
4. [Input Validation Laravel Already Handles](#input-validation-laravel-already-handles)
5. [Size Limits](#size-limits)
6. [Timeouts and What They Actually Guarantee](#timeouts-and-what-they-actually-guarantee)
7. [Logging: What Goes In and What Stays Out](#logging-what-goes-in-and-what-stays-out)
8. [Testing Without Real Requests](#testing-without-real-requests)
9. [Live Demo](#live-demo)
10. [Key Takeaways](#key-takeaways)

 The Problem: Stale AI Results That Look Like Successes
------------------------------------------------------

Imagine a content draft that passed an AI policy check and received a **"ready for editor"** label. A second check is triggered, but the request times out. If the old label stays on screen, it appears the new check also passed — but it never ran to completion. This is a subtle but serious correctness bug in any AI-assisted workflow.

Episode 5 of the *Decide with Jev* series (about seven minutes long) tackles exactly this problem inside a Laravel application.

The Fix: Every Check Starts a Fresh Run
---------------------------------------

The core change is straightforward in concept but important in practice:

- **Every new check immediately creates a run** with no answers and a `needs_review` label.
- Only a **fully completed model call** combined with a **fully completed policy check** can promote that run to a passing state.
- If either step fails — network error, timeout, service exception — the run is **saved as `failed`** and the previous result is explicitly cleared.

This means there is no window where an old "passing" result can masquerade as the outcome of a new request.

```php
// Pseudocode illustrating the pattern
$run = Run::create([
    'status' => 'needs_review',
    'answers' => null,
]);

try {
    $modelResult = $aiService->check($draft);
    $policyResult = $policyService->evaluate($modelResult);

    $run->update([
        'status' => 'passed',
        'answers' => $policyResult,
    ]);
} catch (Throwable $e) {
    $run->update(['status' => 'failed']);
    // old result is gone — no stale data survives
    throw $e;
}

```

Other Topics Covered in Episode 5
---------------------------------

### Input Validation Laravel Already Handles

The episode points out that Laravel's built-in validation already rejects a draft that consists only of whitespace — no custom code needed for that guard.

### Size Limits

Both the **brief** and the **draft** have explicit size limits enforced before the request is sent to the model, preventing unexpectedly large payloads from reaching the AI service.

### Timeouts and What They Actually Guarantee

A timeout is configured on the HTTP client, but the episode is clear: a timeout is a ceiling on how long *your code waits*, not a promise about how fast the model responds. The model may still be processing after your connection drops.

### Logging: What Goes In and What Stays Out

The episode covers a deliberate logging strategy — enough context to debug a failure without leaking sensitive draft content or API credentials into log files.

### Testing Without Real Requests

A test is shown that **fakes a service error** so the test suite never sends a real HTTP request to the AI provider. This keeps tests fast, deterministic, and free of external dependencies.

```php
// Faking a service error in a Laravel test
Http::fake([
    'api.example.com/*' => Http::response(null, 500),
]);

$response = $this->post('/checks', ['draft' => 'Some content']);

$response->assertStatus(422);
$this->assertDatabaseHas('runs', ['status' => 'failed']);

```

### Live Demo

The video ends with a short demo where the request is broken on purpose. The failed run appears in the browser immediately, confirming the UI reflects the true state of the check.

Key Takeaways
-------------

- Always initialize a new run record before calling an AI service, not after.
- A failed request must produce an explicit `failed` state — silence is not acceptable.
- Timeouts limit your wait time, not the model's processing time.
- Fake service errors in tests to keep the suite fast and side-effect-free.
- Laravel's built-in validation handles common edge cases like whitespace-only inputs.

The full code is on the `episode-5` branch of the [Decide with Jev repository](https://github.com/harris21/decide-with-jev/tree/episode-5), and the complete series is available as a [YouTube playlist](https://www.youtube.com/playlist?list=PLDT1gn3LaHTA).

---

*Source: [Decide with Jev: Make Failed AI Requests Safe in Laravel — Laravel News](https://laravel-news.com/decide-with-jev-make-failed-ai-requests-safe-in-laravel)*

- [Laravel](https://www.msaied.com/articles?search=Laravel)
- [AI](https://www.msaied.com/articles?search=AI)
- [Error Handling](https://www.msaied.com/articles?search=Error%20Handling)
- [Testing](https://www.msaied.com/articles?search=Testing)
- [HTTP Client](https://www.msaied.com/articles?search=HTTP%20Client)

 Frequently asked questions 
---------------------------

  Why should a failed AI request create an explicit failed record instead of leaving the previous result in place?Leaving the previous result in place means a stale passing label can survive a failed check, making it appear the new check succeeded when it never completed. Creating an explicit failed record ensures the UI and database always reflect the true outcome of the most recent attempt.

   Does setting a timeout on the HTTP client guarantee the AI model responds within that time?No. A timeout only controls how long your Laravel application waits for a response. The AI model may still be processing after your connection drops. The timeout prevents your application from hanging indefinitely, but it is not a performance guarantee from the model provider.

   How can you test a failed AI service call in Laravel without sending a real HTTP request?Use Laravel's Http::fake() to return a simulated error response (for example, a 500 status) for the AI provider's URL. This keeps the test fast and deterministic, and lets you assert that the run record is saved with a failed status without any real network traffic.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

[Mohamed Said](https://www.msaied.com#person)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

[About](https://www.msaied.com#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Previous articleFilament v3.3.58 Released: Bug Fixes for Schemas, Actions, and Panel Context](https://www.msaied.com/articles/filament-v3358-released-bug-fixes-for-schemas-actions-and-panel-context) [Next articleLaravel Black Friday and Cyber Monday Deals 2026](https://www.msaied.com/articles/laravel-black-friday-and-cyber-monday-deals-2026)  

   On this page
-------------

1. [The Problem: Stale AI Results That Look Like Successes](#the-problem-stale-ai-results-that-look-like-successes)
2. [The Fix: Every Check Starts a Fresh Run](#the-fix-every-check-starts-a-fresh-run)
3. [Other Topics Covered in Episode 5](#other-topics-covered-in-episode-5)
4. [Input Validation Laravel Already Handles](#input-validation-laravel-already-handles)
5. [Size Limits](#size-limits)
6. [Timeouts and What They Actually Guarantee](#timeouts-and-what-they-actually-guarantee)
7. [Logging: What Goes In and What Stays Out](#logging-what-goes-in-and-what-stays-out)
8. [Testing Without Real Requests](#testing-without-real-requests)
9. [Live Demo](#live-demo)
10. [Key Takeaways](#key-takeaways)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://www.msaied.com#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/834/bd4177ac22f4bab6c85e743e4f0051e6.png) Laravel · 3 min read### Laravel Black Friday and Cyber Monday Deals 2026

23 Nov 2026 ](https://www.msaied.com/articles/laravel-black-friday-and-cyber-monday-deals-2026) [ ![](https://cdn.msaied.com/832/68ae93269d6d7cdcbb51731f2a186f88.png) Filament · 3 min read### Filament v3.3.58 Released: Bug Fixes for Schemas, Actions, and Panel Context

21 Nov 2026 ](https://www.msaied.com/articles/filament-v3358-released-bug-fixes-for-schemas-actions-and-panel-context) [ ![](https://cdn.msaied.com/833/bdfee8891cea420aa7c92c44c372bf5c.png) Open Source · 3 min read### pgjumbo: A Cross-Platform PostgreSQL Client for macOS, Windows, and Linux

20 Nov 2026 ](https://www.msaied.com/articles/pgjumbo-a-cross-platform-postgresql-client-for-macos-windows-and-linux) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://www.msaied.com#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://www.msaied.com)
- [Articles](https://www.msaied.com/articles)
- [Certificates](https://www.msaied.com/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://www.msaied.com/sitemap.xml)
