Laravel Daily's

Building Smarter Agents: Laravel AI SDK's Deferred Tool Discovery and Tool Choice Control

hero image

Building intelligent agents requires precise control over how models interact with your application code. As applications scale, tool catalogs expand rapidly. Passing every single function definition in every prompt bloats token usage and degrades model accuracy.

Laravel AI SDK v0.10.x introduces two major features for managing agent toolsets: Deferred Tool Discovery (ToolSearch) and Tool Choice Control (ToolChoice). These additions give you granular control over large tool libraries and force deterministic execution paths when building AI features with a robust php web framework.

Deferred Tool Discovery: Managing Large Tool Catalogs

Deferred Tool Discovery Illustration

When your agent has access to dozens of database queries, API integrations, and internal utility functions, sending all tool signatures upfront creates unnecessary token overhead. ToolSearch solves this by allowing models to discover relevant tools dynamically.

Instead of registering fifty tools in your agent definition, you declare a searchable tool repository. The model queries the catalog when it needs specialized capabilities. This optimization keeps context windows lean and fast.

Here is how you configure deferred tool discovery in your agent class:

use Laravel\Ai\Agent;
use Laravel\Ai\Tools\ToolCatalog;

class SupportAgent extends Agent
{
    public function tools(): iterable
    {
        return ToolCatalog::make()
            ->searchable()
            ->include([
                RetrieveUserBilling::class,
                SearchKnowledgeBase::class,
                ResetUserPassword::class,
                // Dozens of other enterprise tools...
            ]);
    }
}

By leveraging modern php developer tools, your agents fetch only the tool definitions required for the specific user query. Token efficiency improves immediately without sacrificing capability.

Large codebases often accumulate expansive utility layers. Without tool search, models suffer from attention dilution when evaluating forty function schemas at once. Deferred discovery indexes tool descriptions efficiently at the gateway level, returning only matching candidates during the reasoning phase.

Tool Choice Control: Enforcing Deterministic Execution

Tool Choice Control Illustration

Models sometimes hesitate or select suboptimal functions when presented with ambiguous user prompts. ToolChoice gives you explicit control over execution flow by forcing specific tool calls or restricting fallback behavior.

You can set policies requiring the model to invoke a tool, disable tools entirely, or lock execution onto a mandatory function. This removes non-deterministic guesswork in critical business logic workflows.

Consider this example enforcing a mandatory verification tool before any support action executes:

use Laravel\Ai\Agent;
use Laravel\Ai\Support\ToolChoice;

class VerifiedSupportAgent extends Agent
{
    public function toolChoice(): ToolChoice
    {
        return ToolChoice::required(VerifyCustomerIdentity::class);
    }
}

When a user requests sensitive modifications, the agent must trigger VerifyCustomerIdentity first. This level of deterministic control safeguards your backend systems against accidental model drift.

In production environments, predictability is paramount. Allowing an LLM to freely decide whether to run compliance checks introduces vulnerability. Enforcing strict tool choices guarantees that critical interceptors execute every single time.

Practical Implementation: Build Rest API with PHP

Build REST API with PHP Illustration

Integrating these agent controls into a production environment involves exposing your agent via clean API endpoints. You can easily build rest api with php using standard controllers coupled with the new SDK features.

Here is a complete controller snippet handling an agent request with deferred tools and enforced tool choice:

namespace App\Http\Controllers;

use App\Ai\Agents\VerifiedSupportAgent;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class AgentController extends Controller
{
    public function handle(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'prompt' => ['required', 'string', 'max:1000'],
        ]);

        $response = VerifiedSupportAgent::make()
            ->prompt($validated['prompt'])
            ->send();

        return response()->json([
            'status' => 'success',
            'response' => $response->text(),
            'tools_invoked' => $response->invokedTools(),
        ]);
    }
}

This setup routes user input directly into your AI workflow while maintaining strict API response contracts. Your frontend applications consume structured JSON effortlessly.

When pairing these controllers with Laravel starter kits or SPA frontends, latency reduction from ToolSearch becomes immediately noticeable. API response times drop because payload sizes remain optimized.

Architecture and Performance Considerations

Optimizing agent runtimes requires understanding how tool definitions interact with underlying provider APIs. When using ToolSearch, the SDK caches tool embeddings and description indices.

This caching layer prevents redundant database queries during high-concurrency requests. Monitoring these operations via tools like Nightwatch ensures your system remains responsive under load.

Developers migrating existing agents to v0.10.x should review tool descriptions carefully. Because ToolSearch relies on semantic matching against descriptions, clear and concise docstrings directly improve tool retrieval accuracy.

Wrapping Up

Managing growing tool sets no longer requires compromising on token limits or deterministic reliability. Deferred Tool Discovery and Tool Choice Control provide the exact primitives needed for enterprise-grade AI applications.

Explore the official Laravel documentation to test these features in your next project. We would love to hear how you build smarter agents in the community.

Previous
From Routes to Realtime: Building Full-Stack SPAs with Laravel, Vue 3, and Inertia 3.x
Next
Graceful Data Loading in Inertia 3.x: Rescue Slots & Deferred Props in Laravel + Vue SPAs