Laravel AI Agents: Streaming, Token Budgets &amp; Typed Output | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    Production AI Agents in Laravel: Streaming, Token Budgets, and Structured Output Contracts        On this page       1. [  The Gap Between Demo and Production ](#the-gap-between-demo-and-production)
2. [  1. Streaming Responses Over SSE ](#1-streaming-responses-over-sse)
3. [  2. Token Budget Middleware ](#2-token-budget-middleware)
4. [  3. Structured Output Contracts ](#3-structured-output-contracts)
5. [  Key Takeaways ](#key-takeaways)

  ![Production AI Agents in Laravel: Streaming, Token Budgets, and Structured Output Contracts](https://cdn.msaied.com/385/7cc838eef4b97e19a20cbbfd52799f52.png)

  #laravel   #ai   #llm   #php  

 Production AI Agents in Laravel: Streaming, Token Budgets, and Structured Output Contracts 
============================================================================================

     7 Jul 2026      4 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   The Gap Between Demo and Production  ](#the-gap-between-demo-and-production)
2. [  02   1. Streaming Responses Over SSE  ](#1-streaming-responses-over-sse)
3. [  03   2. Token Budget Middleware  ](#2-token-budget-middleware)
4. [  04   3. Structured Output Contracts  ](#3-structured-output-contracts)
5. [  05   Key Takeaways  ](#key-takeaways)

 The Gap Between Demo and Production
-----------------------------------

Most Laravel AI tutorials stop at `Http::post('https://api.openai.com/v1/chat/completions', [...])`. That works for a demo. It falls apart in production when a single runaway prompt burns your monthly token budget in an afternoon, a slow model response blocks a PHP-FPM worker for 45 seconds, or a hallucinated JSON blob crashes your downstream pipeline.

This article covers three concrete patterns that close that gap: **streaming with SSE**, **token budget middleware**, and **structured output contracts with readonly DTOs**.

---

1. Streaming Responses Over SSE
-------------------------------

OpenAI's streaming API sends `text/event-stream` chunks. Laravel's `StreamedResponse` lets you forward them to the browser without buffering the entire completion in memory.

```php
// routes/web.php
Route::get('/chat/stream', ChatStreamController::class);

// app/Http/Controllers/ChatStreamController.php
final class ChatStreamController
{
    public function __invoke(ChatRequest $request, AgentService $agent): StreamedResponse
    {
        return response()->stream(
            function () use ($request, $agent) {
                foreach ($agent->stream($request->validated('message')) as $chunk) {
                    echo "data: {$chunk}\n\n";
                    ob_flush();
                    flush();
                }
                echo "data: [DONE]\n\n";
            },
            200,
            [
                'Content-Type' => 'text/event-stream',
                'X-Accel-Buffering' => 'no', // critical for nginx
                'Cache-Control' => 'no-cache',
            ]
        );
    }
}

```

Inside `AgentService::stream()`, use a generator that reads the chunked HTTP response line by line:

```php
public function stream(string $prompt): \Generator
{
    $response = Http::withToken(config('services.openai.key'))
        ->withOptions(['stream' => true])
        ->post('https://api.openai.com/v1/chat/completions', [
            'model' => 'gpt-4o',
            'stream' => true,
            'messages' => [['role' => 'user', 'content' => $prompt]],
        ]);

    $body = $response->toPsrResponse()->getBody();

    while (! $body->eof()) {
        $line = trim($body->read(512));
        if (str_starts_with($line, 'data: ') && $line !== 'data: [DONE]') {
            $data = json_decode(substr($line, 6), true);
            $content = $data['choices'][0]['delta']['content'] ?? '';
            if ($content !== '') {
                yield $content;
            }
        }
    }
}

```

> **Note:** Set `fastcgi_buffering off` in nginx and ensure PHP-FPM output buffering is disabled for the relevant location block.

---

2. Token Budget Middleware
--------------------------

Token budgets belong at the pipeline layer, not scattered across controllers. A dedicated middleware class can inspect the request, estimate prompt tokens, and reject or downgrade the model before the HTTP call is made.

```php
final class EnforceTokenBudget
{
    // Rough heuristic: 1 token ≈ 4 chars
    private const CHARS_PER_TOKEN = 4;
    private const HARD_LIMIT = 8_000;

    public function handle(AgentPayload $payload, Closure $next): mixed
    {
        $estimated = (int) ceil(
            strlen($payload->systemPrompt . $payload->userMessage) / self::CHARS_PER_TOKEN
        );

        if ($estimated > self::HARD_LIMIT) {
            throw new TokenBudgetExceededException($estimated, self::HARD_LIMIT);
        }

        // Downgrade model for large-but-acceptable prompts
        if ($estimated > 4_000) {
            $payload = $payload->withModel('gpt-4o-mini');
        }

        return $next($payload);
    }
}

```

Wire it into a custom `AgentPipeline`:

```php
$result = app(Pipeline::class)
    ->send(new AgentPayload($system, $user))
    ->through([
        EnforceTokenBudget::class,
        SanitizeUserInput::class,
        InjectRagContext::class,
    ])
    ->thenReturn();

```

---

3. Structured Output Contracts
------------------------------

OpenAI's `response_format` with `json_schema` mode guarantees the model returns a specific shape. Pair it with a PHP 8.3 readonly DTO and a custom cast for end-to-end type safety.

```php
readonly final class ProductSummary
{
    public function __construct(
        public string $name,
        public string $oneLinePitch,
        /** @var list */
        public array $keyFeatures,
        public int $estimatedPriceUsd,
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            name: $data['name'],
            oneLinePitch: $data['one_line_pitch'],
            keyFeatures: $data['key_features'],
            estimatedPriceUsd: $data['estimated_price_usd'],
        );
    }
}

```

Pass the schema to OpenAI:

```php
$response = Http::withToken(config('services.openai.key'))
    ->post('https://api.openai.com/v1/chat/completions', [
        'model' => 'gpt-4o',
        'response_format' => [
            'type' => 'json_schema',
            'json_schema' => [
                'name' => 'product_summary',
                'strict' => true,
                'schema' => [
                    'type' => 'object',
                    'properties' => [
                        'name' => ['type' => 'string'],
                        'one_line_pitch' => ['type' => 'string'],
                        'key_features' => ['type' => 'array', 'items' => ['type' => 'string']],
                        'estimated_price_usd' => ['type' => 'integer'],
                    ],
                    'required' => ['name', 'one_line_pitch', 'key_features', 'estimated_price_usd'],
                    'additionalProperties' => false,
                ],
            ],
        ],
        'messages' => [['role' => 'user', 'content' => $prompt]],
    ]);

$summary = ProductSummary::fromArray(
    json_decode($response->json('choices.0.message.content'), true)
);

```

Because `strict: true` is set, OpenAI will never return extra keys or omit required ones. Your `fromArray` factory becomes a safe assertion, not defensive guesswork.

---

Key Takeaways
-------------

- Use `response()->stream()` with a generator and `X-Accel-Buffering: no` for true SSE streaming in Laravel.
- Enforce token budgets in a pipeline middleware layer, not inside controllers or service methods.
- `json_schema` with `strict: true` eliminates hallucinated keys; map the response to a readonly DTO immediately.
- Estimate prompt tokens early (chars ÷ 4 is good enough for budget checks) to avoid surprise costs.
- Model downgrade logic belongs in the pipeline, keeping your agent service stateless and testable.

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fproduction-ai-agents-in-laravel-streaming-token-budgets-and-structured-output-contracts-2&text=Production+AI+Agents+in+Laravel%3A+Streaming%2C+Token+Budgets%2C+and+Structured+Output+Contracts) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fmsaied.com%2Farticles%2Fproduction-ai-agents-in-laravel-streaming-token-budgets-and-structured-output-contracts-2) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  Does Laravel Octane break SSE streaming?        Octane's Swoole driver handles streaming natively via its response object. Replace `response()-&gt;stream()` with `$server-&gt;push()` patterns or use the FrankenPHP driver, which supports streamed responses out of the box. Test with a real client; Octane workers do not buffer the way PHP-FPM does. 

      Q02  How accurate is the chars-divided-by-4 token estimate?        It is a conservative heuristic suitable for budget guards. For precise counts, use the tiktoken-php library or call OpenAI's tokenizer endpoint. The heuristic intentionally over-counts to provide a safety margin before hitting hard model context limits. 

      Q03  Can I use structured output with streaming enabled simultaneously?        Yes. OpenAI supports both `stream: true` and `response_format: json_schema` together. The JSON is streamed as partial tokens, so you must buffer the full stream before parsing the DTO. Only enable streaming for structured output if you need progress indication; otherwise a standard blocking call is simpler. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://msaied.com/articles) 

 [ ![HTTP Query Method Support in Laravel 13.19](https://cdn.msaied.com/394/4c917aad559beddb5eeb9a7a1e107f4c.png) Laravel Laravel 13 HTTP Client 

### HTTP Query Method Support in Laravel 13.19

Laravel 13.19 adds Http::query() for the HTTP QUERY verb, query/queryJson testing helpers, a reduceInto() coll...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 8 Jul 2026     3 min read  

  Read    

 ](https://msaied.com/articles/http-query-method-support-in-laravel-1319) [ ![Blackfire & Xdebug Profiling in Laravel: Finding Real Bottlenecks in Production-Like Environments](https://cdn.msaied.com/393/a8dfc4ec52c4febc3ef41a491eb452ea.png) laravel performance profiling 

### Blackfire &amp; Xdebug Profiling in Laravel: Finding Real Bottlenecks in Production-Like Environments

Stop guessing where your Laravel app is slow. This guide walks through practical Blackfire and Xdebug profilin...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 8 Jul 2026     4 min read  

  Read    

 ](https://msaied.com/articles/blackfire-xdebug-profiling-in-laravel-finding-real-bottlenecks-in-production-like-environments) [ ![Laravel Cloud Security Defaults Behind Every Deploy](https://cdn.msaied.com/395/28df8f542fbe81ecee3ba4ad897f36d6.png) Laravel Cloud Security PHP 

### Laravel Cloud Security Defaults Behind Every Deploy

Laravel Cloud ships WAF rules, DDoS mitigation, automatic PHP patching, SOC 2 Type II compliance, and deploy-t...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 8 Jul 2026     4 min read  

  Read    

 ](https://msaied.com/articles/laravel-cloud-security-defaults-behind-every-deploy) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

Explore

- [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [Contact](https://msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
