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

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
What We Know About Laravel 14 image

What We Know About Laravel 14

Read article
Difflock: Lint Laravel Migrations and Diff Your Schema image

Difflock: Lint Laravel Migrations and Diff Your Schema

Read article
Fresh Package: Laravel Package Skeleton with Testbench, CI, and Boost Integration image

Fresh Package: Laravel Package Skeleton with Testbench, CI, and Boost Integration

Read article
Inertia DevTools Now Available for Firefox image

Inertia DevTools Now Available for Firefox

Read article
Laravel Scalpel Scans for Filesystem Intrusion Evidence image

Laravel Scalpel Scans for Filesystem Intrusion Evidence

Read article
Mercure Broadcasting in Laravel 13.32 image

Mercure Broadcasting in Laravel 13.32

Read article