Laravel AI Tasks: Queues, Logging &amp; Cost Control | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    Laravel AI Tasks: AI Orchestration with Queues, Logging, and Cost Control        On this page       1. [  What Is Laravel AI Tasks? ](#what-is-laravel-ai-tasks)
2. [  Key Features at a Glance ](#key-features-at-a-glance)
3. [  Defining a Task ](#defining-a-task)
4. [  Running Tasks Three Ways ](#running-tasks-three-ways)
5. [  Cost Tracking and Multi-Tenant Budgets ](#cost-tracking-and-multi-tenant-budgets)
6. [  Takeaways ](#takeaways)

  ![Laravel AI Tasks: AI Orchestration with Queues, Logging, and Cost Control](https://cdn.msaied.com/347/4274eb6d6025d184daaaba35cc79c1f9.png)

 [  Composer Pacakge ](https://msaied.com/articles?category=composer-pacakge) [  AI ](https://msaied.com/articles?category=ai)  #Laravel   #AI   #Packages   #Queues   #Cost Tracking   #Multi-tenant  

 Laravel AI Tasks: AI Orchestration with Queues, Logging, and Cost Control 
===========================================================================

     2 Jul 2026      3 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   What Is Laravel AI Tasks?  ](#what-is-laravel-ai-tasks)
2. [  02   Key Features at a Glance  ](#key-features-at-a-glance)
3. [  03   Defining a Task  ](#defining-a-task)
4. [  04   Running Tasks Three Ways  ](#running-tasks-three-ways)
5. [  05   Cost Tracking and Multi-Tenant Budgets  ](#cost-tracking-and-multi-tenant-budgets)
6. [  06   Takeaways  ](#takeaways)

 What Is Laravel AI Tasks?
-------------------------

[Laravel AI Tasks](https://github.com/fomvasss/laravel-ai-tasks) is a community package that sits on top of the official [Laravel AI SDK](https://github.com/laravel/ai), treating it as the transport layer while adding the operational infrastructure around it. Instead of scattering prompt logic across controllers and jobs, you define AI work as self-contained task classes and let the package handle execution, logging, and cost accounting.

Key Features at a Glance
------------------------

- **Reusable task classes** — bundle a prompt, system message, and post-processing into one object
- **Three execution modes** — synchronous, queued, and streaming with chunk callbacks
- **Built-in dashboard** at `/ai-tasks` — every run logged with token counts, cost, and full request/response detail
- **Multi-provider support** — OpenAI, Anthropic, Gemini, DeepSeek, Groq, Mistral, xAI, and Ollama, with runtime switching and fallback chains
- **Cost tracking and budgets** — per-provider pricing config and multi-tenant monthly spend limits
- **Idempotent queued tasks** — deduplicate dispatches on a unique key
- **Tool and MCP integration**, Anthropic prompt caching, and JSON mode for structured output

Defining a Task
---------------

A task extends `AiTask`. The `toPayload()` method builds the messages and options sent to the provider, and `postprocess()` lets you shape the response before it is returned to the caller.

```php
namespace App\Ai\Tasks;

use Laravel\Ai\Messages\UserMessage;
use Fomvasss\AiTasks\DTO\AiPayload;
use Fomvasss\AiTasks\DTO\AiResponse;
use Fomvasss\AiTasks\Tasks\AiTask;

class SummarizeTask extends AiTask
{
    public function __construct(private readonly string $text) {}

    public function modality(): string { return 'text'; }

    public function toPayload(): AiPayload
    {
        return new AiPayload(
            modality: $this->modality(),
            messages: [new UserMessage("Summarize: {$this->text}")],
            systemPrompt: 'Reply in 3 sentences max.',
            options: ['temperature' => 0.3],
        );
    }

    public function postprocess(AiResponse $response): AiResponse|array
    {
        return $response;
    }
}

```

Running Tasks Three Ways
------------------------

The `AI` facade exposes the same task object through three execution paths.

**Synchronous** — returns the response immediately:

```php
use Fomvasss\AiTasks\Facades\AI;

$response = AI::send(new SummarizeTask($text));
echo $response->content;

```

**Queued** — dispatches the task to a queue and returns a run ID you can track in the dashboard:

```php
$runId = AI::queue(new SummarizeTask($text));

```

**Streaming** — invokes a callback for each chunk as it arrives:

```php
$response = AI::stream(new SummarizeTask($text), function (string $chunk) {
    echo $chunk;
});

```

Queued tasks support idempotency keys, so a duplicate dispatch is deduplicated rather than run twice — useful when the same job might be triggered by multiple events.

Cost Tracking and Multi-Tenant Budgets
--------------------------------------

Pricing is configurable per provider and model. After each call completes, the package calculates the cost and records it alongside the token usage. Those figures feed into multi-tenant budget limits, letting you cap monthly spend per organization and monitor usage across accounts. Everything is browsable at `/ai-tasks`.

Takeaways
---------

- Wraps the Laravel AI SDK without replacing it — your existing provider config carries over.
- Task classes make AI prompts testable and reusable across sync, queue, and stream contexts.
- The `/ai-tasks` dashboard gives immediate visibility into token usage, cost, and errors without a separate observability tool.
- Per-tenant budget caps are built in, which matters for any SaaS product billing AI usage downstream.
- Idempotent queued tasks prevent duplicate AI calls when jobs are retried or re-dispatched.

Source: [Laravel News — Laravel AI Tasks](https://laravel-news.com/laravel-ai-tasks-an-ai-orchestration-package-for-queues-logging-and-cost-control)

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fmsaied.com%2Farticles%2Flaravel-ai-tasks-ai-orchestration-with-queues-logging-and-cost-control&text=Laravel+AI+Tasks%3A+AI+Orchestration+with+Queues%2C+Logging%2C+and+Cost+Control) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fmsaied.com%2Farticles%2Flaravel-ai-tasks-ai-orchestration-with-queues-logging-and-cost-control) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  Does Laravel AI Tasks replace the Laravel AI SDK?        No. It sits on top of the Laravel AI SDK, using it as the transport layer. Your existing provider configuration carries over, and the package adds task classes, execution modes, logging, and cost tracking around it. 

      Q02  How does idempotency work for queued AI tasks?        You can assign a unique key to a queued task. If the same key is dispatched more than once, the package deduplicates it so the AI call runs only once, preventing duplicate charges and redundant processing on retries. 

      Q03  Which AI providers does Laravel AI Tasks support?        The package supports OpenAI, Anthropic, Gemini, DeepSeek, Groq, Mistral, xAI, and Ollama, with runtime provider switching and fallback chains configurable per task. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://msaied.com/articles) 

 [ ![Commune: A Private Community for Laravel Founders and Builders](https://cdn.msaied.com/346/a188e82cf37740fad2be5b4f70efaad1.png) community founders indie makers 

### Commune: A Private Community for Laravel Founders and Builders

Commune is a private community built for founders, makers, and developers to share progress, get feedback, fin...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 2 Jul 2026     3 min read  

  Read    

 ](https://msaied.com/articles/commune-a-private-community-for-laravel-founders-and-builders) [ ![Laravel Reverb WebSocket Broadcasting: Real-Time Channels, Auth, and Scaling Patterns](https://cdn.msaied.com/345/e17d357902124a7017fb076e5e19fb14.png) laravel reverb websockets 

### Laravel Reverb WebSocket Broadcasting: Real-Time Channels, Auth, and Scaling Patterns

Go beyond the hello-world demo: learn how to structure private and presence channels, lock down authorization,...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 2 Jul 2026     4 min read  

  Read    

 ](https://msaied.com/articles/laravel-reverb-websocket-broadcasting-real-time-channels-auth-and-scaling-patterns) [ ![PHP 8.3+ Typed Enums, Backed Casts, and Readonly Properties in Modern Laravel](https://cdn.msaied.com/344/0e6a808cf916b40631d3d362a687baa8.png) laravel php8.3 enums 

### PHP 8.3+ Typed Enums, Backed Casts, and Readonly Properties in Modern Laravel

PHP 8.3 and Laravel's native enum support unlock expressive, type-safe domain models. Learn how to combine bac...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 2 Jul 2026     1 min read  

  Read    

 ](https://msaied.com/articles/php-83-typed-enums-backed-casts-and-readonly-properties-in-modern-laravel) 

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

Explore

- [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [Contact](https://msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
