Back to Blog
AILaravelClaudeSSE

Building a Streaming AI Chat with Claude and Laravel

A step-by-step look at how we wire Anthropic's Claude API to a Laravel backend and stream responses to the browser via SSE.

The chat widget on mo3ty.com streams responses from Claude directly to the browser. This post walks through the architecture.

The stack

  • Frontend: Next.js API route proxies the SSE stream to the browser
  • Backend: Laravel controller calls Anthropic API, streams chunks
  • Model: claude-sonnet for speed, claude-opus for complex queries

Server-Sent Events in Laravel

Laravel's response()->stream() method keeps the connection open while we iterate over Anthropic's streaming response. Each chunk is emitted as an SSE event:

echo "data: " . json_encode(['delta' => $chunk]) . "

";
ob_flush(); flush();

Nginx requires proxy_buffering off for SSE to work through a reverse proxy — without it, the chunks are buffered and the stream appears to freeze.

Conversation memory

Each chat session is persisted in the database. We send the last 20 messages as context on every request. This keeps costs low while maintaining coherent conversations across page reloads.

System prompt

The system prompt grounds the assistant in Mo3ty's real capabilities, pricing philosophy, and portfolio. It answers "what can you build?" accurately rather than giving generic AI responses.

The result is a chat that feels native to the product — not a generic ChatGPT wrapper bolted on.

Building a Streaming AI Chat with Claude and Laravel | Mo3ty