Building a ReAct Chat Agent with the Laravel AI SDK
This tutorial walks through how to build a streaming, chat assistant on top of
the Laravel AI SDK (laravel/ai).
Stack: PHP 8.5 · Laravel 13 · Livewire 4 · Laravel AI SDK
1. What is the ReAct pattern?
A plain language model is a "one-shot" text predictor: you give it a question, it
gives you an answer from what it already knows. That breaks down the moment the
question depends on something the model can't know — the current time, today's
exchange rate, an exact arithmetic result, or a fact that changed after training.
ReAct (short for Reasoning + Acting) fixes this by letting the model
work in a loop instead of answering in one go:
Reason — the model thinks about what the question needs.
Act — if it needs outside information, it calls a tool (a function you
provide) instead of guessing.
Observe — your tool runs and returns a result; that result is fed back to
the model as a new observation.
Repeat — the model reasons about the observation and either calls another
tool or produces its final answer.
A worked example, "What time is it in Tokyo right now?":
User: What time is it in Tokyo right now?
Reason: I don't actually know the current time. I should call a clock tool.
Act: current_datetime(timezone: "Asia/Tokyo")
Observe: Saturday, June 6, 2026 at 11:42 PM (Asia/Tokyo)
Reason: I now have the real local time. I can answer.
Answer: It's currently 11:42 PM on Saturday, June 6, 2026 in Tokyo.
The key idea: the model never fakes the time. It recognises the gap, reaches
for a tool, and reasons over the real result. That loop — reason, act, observe,
repeat — is the whole pattern.
2. What is the Laravel AI SDK?
The Laravel AI SDK (laravel/ai) is a unified, Laravel-flavoured API for talking
to AI providers (Anthropic, OpenAI, Gemini, Groq, Ollama, and more). Instead of
hand-rolling provider-specific HTTP calls, you describe an Agent as a PHP
class and the SDK handles the rest.
The four concepts you need for this tutorial:
Agent — a class that bundles a system prompt (instructions()), a model
choice, and a set of tools. It is the thing you "prompt".
Tool — a small class implementingLaravel\Ai\Contracts\Toolthat the
model is allowed to call. Each tool advertises a name, a description, and a
parameter schema, and exposes ahandle()method that does the work.
Streaming — instead of waiting for the whole answer, you can iterate over
the response as it is generated, receiving text fragments and tool-call events
in real time.
Conversation memory — the SDK can automatically persist every turn to the
database and replay history so the agent has context on the next message.
Crucially, the SDK runs the ReAct loop for you. You define the tools and a
step limit; the SDK handles asking the model what it wants to do, executing the
chosen tool, feeding the observation back, and looping until the model is done.
Built on Prism
The Laravel AI SDK isn't talking to providers entirely on its own — it's a
higher-level abstraction built on top of
Reference: files in this walkthrough
app/Ai/Agents/ChatAgent.php— the agent
app/Ai/Tools/CurrentDateTime.php,WikipediaLookup.php,Calculator.php— custom tools
resources/views/components/chat/⚡main.blade.php— chat UI + streaming
app/Models/User.php— conversation ownership
database/migrations/..._create_agent_conversations_table.php— persistence schema
config/ai.php— provider configuration
tests/Feature/ChatTest.php,tests/Unit/*— tests
SOCIAL SHARE CARD GENERATOR