Laravel Packages

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

Published
 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.

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Laravel monitoring that doesn't bill you by your traffic image

Laravel monitoring that doesn't bill you by your traffic

Read article
Mock PHP Classes in Tests With the Double Library image

Mock PHP Classes in Tests With the Double Library

Read article
Laravel Discount: Coupon Codes, Usage Limits, and Stacking image

Laravel Discount: Coupon Codes, Usage Limits, and Stacking

Read article
Laravel Truss: Live Interactive Database ER Diagrams image

Laravel Truss: Live Interactive Database ER Diagrams

Read article
Laravel artisan dev: Run Server, Queue, Logs, and Vite image

Laravel artisan dev: Run Server, Queue, Logs, and Vite

Read article
Validate and Convert HEIC Images in Laravel image

Validate and Convert HEIC Images in Laravel

Read article