Laravel AI Tasks: An AI Orchestration Package for Queues, Logging, and Cost Control
Published on by Paul Redmond
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-taskslisting 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
/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.