Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures Gracefully
Laravel AI SDK 1.4 passes tool exceptions back to models for self-correction, enables approval testing in Agent::fake(), and splits OpenAI billing errors.
Production AI agents fail far more often on mundane tool errors—expired orders, invalid IDs, transient API rejections—than on model crashes. Laravel AI SDK v1.4.0, detailed by Laravel News, addresses that reality directly by letting agents recover from tool exceptions instead of aborting the entire run, alongside major upgrades for testing human-in-the-loop approvals, inspecting raw provider responses, and handling billing failures accurately.
Tool Exceptions Become Recoverable Model Feedback
Before v1.4.0, any exception thrown inside a tool terminated the agent loop immediately and dispatched AgentFailed. That made sense for fatal infrastructure crashes, yet it created brittle user experiences whenever a business-rule check failed—for instance, a RefundOrder tool throwing "Order 1234 can no longer be refunded."
In v1.4.0 (PR #1191 by @pascalbaljet), the SDK catches the tool's exception and feeds its message back to the model as a failed tool result (The tool call failed: ...). The run continues, allowing the model to explain the constraint to the user or select a different tool. Crucially, observability stays intact:
- Application logging: the exception is still forwarded to Laravel's exception handler via
report(). - Event hooks and streaming:
ToolFailedstill fires, and streamed runs emit aToolResultevent withsuccessfulset tofalse. - Clear failure boundary: only the tool's own exception is recovered; exceptions raised inside event listeners still abort the run.
Approved tools already followed this pattern, so all tools now share consistent semantics. Teams relying on the legacy abort-on-error behavior can restore it in one line by rethrowing $event->exception from a ToolFailed listener.
End-to-End Testing for Human-in-the-Loop Approvals
Human-in-the-loop workflows pause an agent turn until a user approves or rejects a sensitive action such as sending an email. Previously, resuming a paused run under Agent::fake() skipped executing the tool entirely, meaning nothing ran, streamed, or got stored—leaving a blind spot in automated test suites.
PR #1190 closes that gap. Faked agents now resume paused tool calls just like real providers: approved tools actually execute and persist their results, while rejected tools receive a denied outcome before the fake returns its next scripted response. One upgrade nuance to note: rejecting a tool call without a reason now ends the turn (matching real provider behavior), so tests expecting a follow-up reply should pass an explicit reason such as Decision::reject('Not now.').
Raw Responses, Billing Separation, and Cleaner Streams
Beyond agent control flow, v1.4.0 cleans up several practical pain points engineers hit when running multi-provider stacks in production:
- Expanded
rawHTTP access: Previously limited to text responses since v0.10.3, therawHTTP response property is now exposed on classification, embeddings, transcription, image, and reranking responses (PRs#1178–#1182). Developers can read unmapped provider fields like OpenRouter'susage.cost, though the property becomesnullafter serialization or when every embedding vector is served from cache. - Accurate OpenAI 429 classification: OpenAI returns HTTP
429for both throttling and billing failures (insufficient_quota,credit_balance_exhausted). Instead of treating every429asRateLimitedExceptionand triggering pointless retries, v1.4.0 throwsInsufficientCreditsExceptionwithout retries while preserving provider failover (PR#1174). - Predictable stream error broadcasts: Provider stream failures previously broadcast under dynamic provider error codes like
403orPERMISSION_DENIED, forcing frontends to guess event names. They now broadcast under the fixed nameerror, keeping the provider code inside the payloadtypeand leaving job crashes asstream_failed(PR#1208). - Observable title generation: Automatic conversation title generation now runs as an
AnonymousAgenton the turn's provider and cheapest model, firing standard agent events withparentInvocationIdlinked to the triggering turn while still falling back to prompt text on failure (PR#1203).
Provider Fixes and Upgrade Path
The release rounds out with interoperability fixes across providers: Anthropic tool calls now run when messages end with end_turn (#1188) and join streamed text across pause_turn continuations (#1186); OpenAI-compatible tool calls handle missing indexes or stop finishes (#1192); OpenAI batch embeddings preserve input order (#1172); custom voices serialize as objects (#1212); stored message IDs copy into RUN_FINISHED metadata for @ag-ui/client 1.x (#1165); resumed turns rebuild content from merged steps (#1162); and Mistral model definitions are updated (#1173).
Upgrading takes composer update laravel/ai, though teams should audit three integration points first: any catch (RateLimitedException) blocks expecting OpenAI quota failures, frontend stream listeners watching raw provider error codes instead of error, and agent event listeners that now also see background title-generation runs.
// comments (0)