Laravel AI Tasks: Queues, Logging &amp; Cost Control | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel 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) [ Contact ](https://www.msaied.com#contact-section) 

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

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

  [ home ](https://www.msaied.com)    [ articles ](https://www.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://www.msaied.com/articles?category=composer-pacakge) [  AI ](https://www.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%2Fwww.msaied.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%2Fwww.msaied.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://www.msaied.com/articles) 

 [ ![DDD Value Objects and DTOs in Laravel Without the Bloat](https://cdn.msaied.com/450/41688537085b86f102fd8c219a35319f.png) laravel ddd php 

### DDD Value Objects and DTOs in Laravel Without the Bloat

Learn how to implement domain-driven value objects and data transfer objects in Laravel using PHP 8.3 readonly...

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

 21 Jul 2026     1 min read  

  Read    

 ](https://www.msaied.com/articles/ddd-value-objects-and-dtos-in-laravel-without-the-bloat) [ ![Laravel + Python FastAPI: Image OCR Demo](https://cdn.msaied.com/447/5f3d87146fbf19957973cbc88c6c0155.png) Laravel Python FastAPI 

### Laravel + Python FastAPI: Image OCR Demo

Learn how to call a Python image OCR script from Laravel using FastAPI as the bridge. This premium tutorial wa...

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

 20 Jul 2026     3 min read  

  Read    

 ](https://www.msaied.com/articles/laravel-python-fastapi-image-ocr-demo) [ ![Scaffold Laravel Packages with the `laravel package` Command in Installer v5.31.0](https://cdn.msaied.com/449/522e744d3c7bae1f214c179a83ae5b88.png) Laravel Installer Package Development CLI 

### Scaffold Laravel Packages with the `laravel package` Command in Installer v5.31.0

Laravel Installer v5.31.0 introduces a `laravel package` command to scaffold packages from the CLI, automated...

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

 20 Jul 2026     3 min read  

  Read    

 ](https://www.msaied.com/articles/scaffold-laravel-packages-with-the-laravel-package-command-in-installer-v5310) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://www.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://www.msaied.com)
- [Projects](https://www.msaied.com/projects)
- [Articles](https://www.msaied.com/articles)
- [Certificates](https://www.msaied.com/certificates)
- [Contact](https://www.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)
