An AI agent that only reads data is easy to trust. An agent that can refund a payment, delete a record or email a customer is a different story. Two things make that kind of agent safe to ship with the Laravel AI SDK:
In this post we’ll build a support agent that can look up orders freely but needs a manager’s approval before a large refund, handles refusals gracefully, and is covered by tests.
Why not tell the model “be careful with refunds” in the system prompt? Because a prompt is a suggestion, not a guarantee. Models misread context, get manipulated by text inside user messages, and occasionally hallucinate an order ID. An approval gate is enforced in code, so it holds no matter what the model was talked into.
The model still decides what it wants to do. Your code decides whether it happens. Approval is per tool and opt-in: read-only tools keep running instantly.
Approval needs persisted conversation history, because a d agent resumes from stored state:
composer require laravel/ai
php artisan migrate
Configure your provider key in config/ai.php. Upgrading an older install? Human-in-the-loop arrived in v0.10.0 and added a nullable approval_state column to the conversation messages table; custom ConversationStore implementations must provide storeApprovalResults().
Implement Approvable and use InteractsWithApprovals. Define needsApproval() so small refunds run immediately and large ones with a reason the reviewer will see:
use Laravel\Ai\Approvals\Approval;
use Laravel\Ai\Concerns\InteractsWithApprovals;
use Laravel\Ai\Contracts\Approvable;
use Laravel\Ai\Contracts\Tool;
class IssueRefund implements Approvable, Tool
{
use InteractsWithApprovals;
public function handle(Request $request): Stringable|string
{
$order = Order::findOrFail($request['order_id']);
if ($order->delivered_at->lt(now()->subDays(30))) {
throw new RuntimeException(
"Order {$order->id} can no longer be refunded: it's outside the 30-day window."
);
}
$order->refund($request['amount']);
return "Refunded {$request['amount']} on order {$order->id}.";
}
protected function needsApproval(Request $request): Approval|bool
{
return $request['amount'] <= 100
? false
: Approval::required('Refunds over $100 need a manager.');
}
// description() and schema() as for any tool
}
The agent must be Conversational with persisted history, which RemembersConversations handles:
use Laravel\Ai\Concerns\RemembersConversations;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\Conversational;
use Laravel\Ai\Promptable;
class SupportAgent implements Agent, Conversational
{
use Promptable, RemembersConversations;
public function tools(): iterable
{
return [
(new LookUpOrder)->withoutApproval(),
new IssueRefund,
];
}
}
Want every refund reviewed? Use (new IssueRefund)->requireApproval('Every refund gets reviewed.').
$response = (new SupportAgent)
->forUser($customer)
->prompt('My headphones on order 4192 arrived broken. I want my $249 back.');
if ($response->hasPendingApprovals()) {
foreach ($response->pendingApprovals as $approval) {
PendingRefund::create([
'conversation_id' => $response->conversationId,
'tool_call_id' => $approval->id,
'tool' => $approval->tool,
'arguments' => $approval->arguments,
'reason' => $approval->reason,
]);
}
}
Each pending approval carries the tool call ID, tool name, the arguments the model chose, and your reason. That table is your approval queue.
A manager needs to see what the agent wants to do and why:
@foreach ($pendingRefunds as $refund)
<div class="card">
<p>Order #{{ $refund->arguments['order_id'] }}: refund ${{ $refund->arguments['amount'] }}</p>
<p class="muted">{{ $refund->reason }}</p>
<form method="POST" action="{{ route('refunds.decide', $refund) }}">
@csrf
<button name="decision" value="approve">Approve</button>
<button name="decision" value="reject">Reject</button>
</form>
</div>
@endforeach
Protect the route with a policy. The SDK checks that a decision belongs to the right conversation, but who may approve is your job.
use Laravel\Ai\Approvals\Decision;
use Laravel\Ai\Approvals\Decisions;
public function decide(Request $request, PendingRefund $refund)
{
$this->authorize('approve', $refund);
$decision = $request->input('decision') === 'approve'
? Decision::approve()
: Decision::reject('This order is outside the return window.');
(new SupportAgent)
->continue($refund->conversation_id, as: $refund->customer)
->prompt(Decisions::from([$refund->tool_call_id => $decision]));
$refund->update(['decided_by' => $request->user()->id]);
return back();
}
Every pending call needs a decision; missing or unknown IDs throw ApprovalMismatchException. For several d calls, approveRemaining() and rejectRemaining() set a default.
Before 1.4, an exception inside a tool ended the run and fired AgentFailed. Now the SDK sends the message back to the model as a failed tool result. If the approved refund hits the 30-day rule in Step 1, the model receives:
The tool call failed: Order 4192 can no longer be refunded: it's outside the 30-day window.
It can then explain that to the customer, or offer a replacement. You keep full visibility: the exception is still reported, ToolFailed still fires, and in a streamed run the failure arrives as a ToolResult event with successful set to false.
That changes how you write tools, because exception messages are now read by the model, and maybe passed to a user:
To keep the old crash-the-run behaviour, rethrow from a listener (listener exceptions still end the run):
Event::listen(ToolFailed::class, fn (ToolFailed $event) => throw $event->exception);
Also new in 1.4: under Agent::fake(), resuming a d call now behaves like a real provider. Approved tools run and store their results; rejected ones get a denied result. So you can finally test approvals end to end:
SupportAgent::fake([
new ToolCall('call-1', 'IssueRefund', ['order_id' => 4192, 'amount' => 249]),
'Your refund is on its way.',
]);
$d = (new SupportAgent)->forUser($user)->prompt('Refund order 4192');
expect($d->hasPendingApprovals())->toBeTrue();
$response = (new SupportAgent)
->continue($d->conversationId, as: $user)
->prompt(Decisions::from(['call-1' => Decision::approve()]));
expect($response->text)->toBe('Your refund is on its way.');
A rejection with no reason now ends the turn under the fake too, so pass one if your test expects a reply: Decision::reject('Not now.').
insufficient_quota) now throw InsufficientCreditsException instead of RateLimitedException, and aren’t retried. Alert on it and fall back to a human queue; running out of credits is an ops problem, not a transient error.raw responseserror`` parentInvocationId to tell it apart from user turns.
Upgrade with composer update laravel/ai, then update any code that catches RateLimitedException for billing errors.
$request->toolCallId() as an idempotency key so a retry never refunds twice.ToolApprovalRequested; listen for it and ping managers. tool_approval_request event, so a chat UI can render an inline approval card.
What is human-in-the-loop in the Laravel AI SDK?
An opt-in, per-tool approval API. Tools implementing Approvable the agent until your code passes an approve or reject decision for each pending call.
What happens when a tool throws in Laravel AI SDK 1.4?
The run continues. The exception message is sent to the model as a failed tool result, and the exception is still reported and fires ToolFailed.
Which tools should need approval?
Any tool that moves money, deletes data or contacts a customer. Read-only tools can run freely.
Rule of thumb: anything that touches money, deletes data or contacts a customer should be approvable, and every tool should fail with a message a model can act on. With needsApproval(), clear exceptions and 1.4’s testable fakes, agents with real write access become safe to ship.
Are you running AI agents with write access in production yet? Tell me in the comments how you keep them in check.