<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[fomvasss]]></title><description><![CDATA[fomvasss]]></description><link>https://fomvasss.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/6a35b7db0424a7e216c8d088/787a40c1-d713-4b02-9e29-e4045e444815.png</url><title>fomvasss</title><link>https://fomvasss.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Wed, 28 Oct 2026 12:04:57 GMT</lastBuildDate><atom:link href="https://fomvasss.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Laravel AI Tasks: Queue, Log, and Track AI Calls in Laravel]]></title><description><![CDATA[A production orchestration layer built on top of laravel/ai. Task classes, audit logging, cost tracking, multi-tenant budgets, streaming, faking, and a…]]></description><link>https://fomvasss.hashnode.dev/laravel-ai-tasks-queue-log-and-track-ai-calls-in-laravel</link><guid isPermaLink="true">https://fomvasss.hashnode.dev/laravel-ai-tasks-queue-log-and-track-ai-calls-in-laravel</guid><category><![CDATA[Laravel]]></category><category><![CDATA[PHP]]></category><category><![CDATA[webdev]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[AI]]></category><category><![CDATA[AI Development Services]]></category><dc:creator><![CDATA[Vas]]></dc:creator><pubDate>Fri, 19 Jun 2026 22:06:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6a35b7db0424a7e216c8d088/dc865399-7063-4894-9104-39029f4b916f.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>You add your first AI call. It's one line, a synchronous <code>openai()-&gt;chat()</code>, and it works perfectly.</p>
<p>Six months later you have seventeen of those scattered across the codebase, no idea which ones are slow, no retry logic, a tenant who blew through $300 in one night, and a queue that processes bulk image generation at the same priority as real-time chat responses.</p>
<p>Making AI work in production Laravel apps involves more than calling an API. You need queues, audit logs, cost tracking, retry logic, and tests that do not hit real providers. That is what <a href="https://github.com/fomvasss/laravel-ai-tasks"><code>fomvasss/laravel-ai-tasks</code></a> is for.</p>
<p>It sits on top of <a href="https://laravel.com/docs/13.x/ai-sdk"><code>laravel/ai</code></a> (the official Laravel AI SDK) and adds the orchestration and observability layer you actually need in production.</p>
<h2>What you get</h2>
<ul>
<li><p>Task classes with a generator command (<code>ai:make-task</code>)</p>
</li>
<li><p>Sync, queued, and streaming execution via the <code>AI</code> facade</p>
</li>
<li><p>Every run logged to <code>ai_runs</code> table (request, response, tokens, cost, status)</p>
</li>
<li><p>Driver routing with fallback chains</p>
</li>
<li><p>Multi-tenant budget tracking</p>
</li>
<li><p>Prompt caching support for Anthropic</p>
</li>
<li><p>Built-in dashboard at <code>/ai-tasks</code></p>
</li>
<li><p><code>AI::fake()</code> with assertions for testing</p>
</li>
<li><p>Events at every stage of execution</p>
</li>
<li><p>Text, image, embeddings, TTS, and transcription modalities</p>
</li>
</ul>
<h2>Requirements</h2>
<ul>
<li><p>PHP 8.3+</p>
</li>
<li><p>Laravel 12 or 13</p>
</li>
<li><p><code>laravel/ai</code> ^0.8</p>
</li>
</ul>
<h2>Install</h2>
<pre><code class="language-bash">composer require fomvasss/laravel-ai-tasks
php artisan vendor:publish --tag=ai-tasks-config
php artisan vendor:publish --tag=ai-migrations
php artisan migrate
</code></pre>
<h2>Define a task</h2>
<pre><code class="language-php">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:     'text',
            messages:     [new UserMessage("Summarize: {$this-&gt;text}")],
            systemPrompt: 'Reply in 3 sentences max.',
            options:      ['temperature' =&gt; 0.3],
        );
    }

    public function postprocess(AiResponse $response): AiResponse|array
    {
        return $response;
    }
}
</code></pre>
<h2>Run it</h2>
<pre><code class="language-php">// Sync
\(response = AI::send(new SummarizeTask(\)text));

// Queued
\(runId = AI::queue(new SummarizeTask(\)text));

// Streaming (no timeout, good for long outputs)
AI::stream(new SummarizeTask(\(text), fn(\)chunk) =&gt; print($chunk));

// Runtime driver override
AI::send(new SummarizeTask($text), drivers: 'anthropic');
</code></pre>
<h2>Driver routing with fallback</h2>
<pre><code class="language-php">// config/ai-tasks.php
'routing' =&gt; [
    'summarize' =&gt; ['openai', 'anthropic'], // tries openai, falls back to anthropic
],
</code></pre>
<h2>Cost tracking</h2>
<pre><code class="language-php">'anthropic' =&gt; [
    'model' =&gt; 'claude-sonnet-4-6',
    'price' =&gt; ['in' =&gt; 3.00, 'out' =&gt; 15.00],
],
</code></pre>
<p>Query spend by tenant:</p>
<pre><code class="language-php">AiRun::where('tenant_id', $tenantId)-&gt;where('status', 'ok')-&gt;sum('cost');
</code></pre>
<h2>shouldRun() guard</h2>
<p>Prevent stale queued tasks from consuming tokens:</p>
<pre><code class="language-php">public function shouldRun(): bool
{
    return Product::find($this-&gt;productId)?-&gt;needs_analysis ?? false;
}
</code></pre>
<h2>Testing without real API calls</h2>
<pre><code class="language-php">$fake = AI::fake([
    'summarize' =&gt; 'This is a summary.',
    '*'          =&gt; 'Default fallback.',
]);

$fake-&gt;assertSent(SummarizeTask::class);
$fake-&gt;assertSentCount(1);
</code></pre>
<h2>Supported providers</h2>
<p>OpenAI, Anthropic, Gemini, DeepSeek, Groq, Mistral, xAI, Ollama, ElevenLabs, and any <code>laravel/ai</code>-compatible provider.</p>
<hr />
<ul>
<li><p>GitHub: <a href="https://github.com/fomvasss/laravel-ai-tasks">https://github.com/fomvasss/laravel-ai-tasks</a></p>
</li>
<li><p>Packagist: <a href="https://packagist.org/packages/fomvasss/laravel-ai-tasks">https://packagist.org/packages/fomvasss/laravel-ai-tasks</a></p>
</li>
</ul>
<hr />
<h2>Support</h2>
<p>If this package is useful to you, consider supporting its development:</p>
<ul>
<li><p>☕ Ko-fi: <a href="https://ko-fi.com/fomvasss">https://ko-fi.com/fomvasss</a></p>
</li>
<li><p>🏦 Monobank: <a href="https://send.monobank.ua/jar/5xsqtHvVrY">https://send.monobank.ua/jar/5xsqtHvVrY</a></p>
</li>
<li><p>💚 USDT TRC20: <code>THLgp6DxiAtbNHvgnKV56vk1L38UuUagKf</code></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>