Laravel AI Tasks: A New Orchestration Package for Queued, Logged AI Calls
The Laravel AI SDK gave the ecosystem a provider-agnostic way to call OpenAI, Anthropic, and friends from a Laravel app. Laravel AI Tasks, a package by developer fomvasss, sits on top of it and adds the operational scaffolding teams tend to end up building by hand: reusable task objects, queued and streamed execution, per-tenant cost tracking, and a dashboard for watching it all happen.
Task classes instead of ad-hoc SDK calls
Instead of calling the AI SDK directly wherever a prompt is needed, you define the work as a class extending AiTask:
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 SummarizeTicketTask 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;
}
}
That single object can then run three different ways through the AI facade:
use Fomvasss\AiTasks\Facades\AI;
// Synchronous
$response = AI::send(new SummarizeTicketTask($text));
// Queued — returns a run ID you can track in the dashboard
$runId = AI::queue(new SummarizeTicketTask($text));
// Streamed, chunk by chunk
$response = AI::stream(new SummarizeTicketTask($text), function (string $chunk) {
echo $chunk;
});
Queued tasks support idempotency keys, so a duplicate dispatch — a retried webhook, a double form submission — gets deduplicated instead of billed and run twice.
Cost tracking and multi-tenant budgets
Every run is logged with token counts, cost, and duration, browsable at /ai-tasks. Pricing is configurable per provider and model, and those figures feed into per-tenant budget limits — useful for a PhpArchitect-style SaaS product billing customers for AI usage, where one tenant going over budget shouldn't be discovered at the end of the month.
The package supports OpenAI, Anthropic, Gemini, DeepSeek, Groq, Mistral, xAI, and Ollama, with runtime provider switching and fallback chains if a primary provider errors or times out. It also layers in tool/MCP integration, Anthropic prompt caching, and a JSON mode for structured output — the pieces most teams end up rebuilding themselves after the first few weeks of using the raw AI SDK directly.
Why this matters beyond the demo
The interesting design decision here isn't the AI calls themselves — it's treating an AI call like any other unreliable, billable external operation: something that gets a queue, a retry policy, an audit trail, and a budget, the same way you'd treat a payment gateway call. For teams already comfortable with Laravel's queue and job infrastructure, that framing is probably the more durable takeaway than any specific provider integration.
Laravel AI Tasks is open source under the MIT license. Installation and full configuration details are in the GitHub repository.