Laravel AI Tasks: An AI Orchestration Package for Queues, Logging, and Cost Control

Published on by

 Laravel AI Tasks: An AI Orchestration Package for Queues, Logging, and Cost Control image

Laravel AI Tasks is a package that sits on top of the Laravel AI SDK, treating it as the transport layer while adding the operational pieces around it.

You define AI work as reusable task classes, then run them synchronously, push them onto a queue, or stream the response — with audit logging, per-tenant budgets, and a dashboard tracking every run.

The package covers:

  • Reusable task classes that bundle a prompt, system message, and post-processing into a single object
  • Three execution modes — synchronous, queued, and streaming with chunk callbacks
  • A dashboard at /ai-tasks listing runs with token counts, costs, and request/response detail
  • Multi-provider support for OpenAI, Anthropic, Gemini, DeepSeek, Groq, Mistral, xAI, and Ollama, with runtime switching and fallback chains
  • Cost tracking and budgets with per-provider pricing and multi-tenant spend limits
  • Idempotent queued tasks that deduplicate on a unique key
  • Tool and MCP integration, Anthropic prompt caching, and JSON mode for structured output
Laravel AI Tasks dashboard showing today's total, OK, and error counts, monthly cost, and a filterable table of task runs with driver, tenant, dispatch mode, status, token counts, cost, and duration.
The Laravel AI Tasks dashboard at /ai-tasks, listing each run with token usage, cost, duration, and status.

Defining a Task

A task extends AiTask and describes its payload. The toPayload() method builds the messages and options sent to the provider, while postprocess() gives you a hook to shape the response before it is returned:

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 runs the same task object synchronously, on a queue, or as a stream. Synchronous execution returns the response directly:

use Fomvasss\AiTasks\Facades\AI;
 
$response = AI::send(new SummarizeTask($text));
echo $response->content;

Queueing returns a run ID you can track through the dashboard, and streaming invokes a callback for each chunk as it arrives:

$runId = AI::queue(new SummarizeTask($text));
 
$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.

Cost Tracking and Budgets

Pricing is configurable per provider and model, and the package calculates the cost of each call after it completes. Those figures feed multi-tenant budget limits, so you can cap monthly spend per organization and monitor usage across accounts. Every run — request, response, token usage, and cost — is recorded and browsable at /ai-tasks.

You can find the source and full documentation on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises

The latest

View all →
Building and Deploying a Laravel App With Claude Code on Zerops image

Building and Deploying a Laravel App With Claude Code on Zerops

Read article
The first hands-on AI Developer Certification image

The first hands-on AI Developer Certification

Read article
Heimdall: A Minimum Age Policy for Your Composer Dependencies image

Heimdall: A Minimum Age Policy for Your Composer Dependencies

Read article
Laravel Announces the Founders Summit, a One-Day Event for Founders image

Laravel Announces the Founders Summit, a One-Day Event for Founders

Read article
A Practical Guide to Laravel's First-Party Image Processing image

A Practical Guide to Laravel's First-Party Image Processing

Read article
RouteKey Model Attribute in Laravel 13.21 image

RouteKey Model Attribute in Laravel 13.21

Read article