[1.x] Opt-in eager tool calls when streaming output

Closed
EX
21d ago
+281-2

Problem

When streaming an agent response that invokes tools mid-step the current implementation waits until the entire step is done streaming before calling the tool(s).

This can cause wait times to go long when tool calls take up time on their own, which it might as well do while the agent is outputting, effectively making the model output and tool call run in parallel.

        0s      2s      4s      6s      8s      10s     12s     14s     16s     18s     20s     22s     24s
        |-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|-------|
Today   =step 1=============                =step 2=========    =step 3===========
  tools                     1111111111112233                4455                  667777777777777788  23.0 s

Eager   =step 1=============      =step 2=========  =step 3===========
  tools           1111111111112233            44  55          66  7777777777777788  18.5 s

        = model streaming a step
        1-8 = tool calls in the order the model wrote them
              1 is a 3 s lookup, 7 a 3.5 s sub-agent, the rest take 500 ms

// Timeline is illustrative only.

Solution

An opt-in attribute for agent classes that allows them to invoke tools in-between text deltas which may, in some cases, shorten the waiting time for the final output.

The result is most noticeable when using sub-agents as tools.

Note: only providers that emit each tool call as soon as it is complete benefit from this (Anthropic, OpenAI, Azure OpenAI, xAI and Bedrock).

Usage example

use Laravel\Ai\Attributes\ExecuteToolsEagerly;

#[ExecuteToolsEagerly]
class SupportAgent implements Agent, HasTools
{
    use Promptable;

    public function instructions(): string
    {
        return 'You help customers with their orders.';
    }

    public function tools(): iterable
    {
        return [new LookupCustomer(), new SuggestReply()];
    }
}
foreach ((new SupportAgent())->stream('Where is order #1234?') as $event) {
    // ToolResult events now arrive right after their ToolCall,
    // instead of after the model has finished the whole step.
}

A tool or sub-agent will not be run early if they require approval.

An unknown tool will also not be called early and will be left to be fixed by either RepairToolCalls or the default thrown exception.

It will also have no effect on agents called with the ->prompt() method.

Tool calls on the last step won't be called early.

Trade-offs

Text deltas from the stream will be delayed if the tool call runs long, but overall the total runtime of the agent should be shorter in most cases.

A failed step can run tools once more.

Compatibility

This is purely opt-in and shouldn't break existing code anywhere.

Tests

Tests have been added to verify the behavior in tests/Unit/Gateway/TextGenerationLoopTest.php

Docs

If this gets merged then I'll happily add a PR to expand the docs at https://laravel.com/framework/docs/ai-sdk

(timeline and code example written using AI)