What Is Laravel AI Tasks?
Laravel AI Tasks is a community package that sits on top of the official Laravel AI SDK, 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.
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:
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:
$runId = AI::queue(new SummarizeTask($text));
Streaming — invokes a callback for each chunk as it arrives:
$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-tasksdashboard 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