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

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article
Laranon: Reversible PII Anonymization for Laravel and LLMs image

Laranon: Reversible PII Anonymization for Laravel and LLMs

Read article
PHP 8.6 is released!  image

PHP 8.6 is released!

Read article
Laravel Warrant: Row-Level Permissions With Eloquent image

Laravel Warrant: Row-Level Permissions With Eloquent

Read article
JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43 image

JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43

Read article
Durable Workflow: Long-Running Workflows for Laravel image

Durable Workflow: Long-Running Workflows for Laravel

Read article