Replace the file with this. Read it in this order: the outer while, the inner while, finding function calls, running them, adding the outputs, and going round again.
Replace file · app/Console/Commands/AgentCommand.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use function Laravel\Prompts\info;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\text;
// Lesson 3: an outer loop (you) and an inner loop (the agent using tools).
class AgentCommand extends Command
{
protected $signature = 'agent';
protected $description = 'Chat with an agent that can use tools';
protected array $history = [];
public function handle(): void
{
// OUTER loop: your conversation with the agent.
while (true) {
$prompt = text('What is on your mind?', required: true);
$this->history[] = ['role' => 'user', 'content' => $prompt];
// INNER loop: the agent keeps going until it has an answer.
while (true) {
$response = spin(fn () => $this->runModel(), 'Hmm... thinking about that.');
$this->history = [...$this->history, ...$response['output']];
// Find every tool the AI wants us to run (there can be several).
$functionCalls = collect($response['output'])
->filter(fn ($item) => $item['type'] === 'function_call');
// None? Then the AI has its final answer.
if ($functionCalls->isEmpty()) {
info($this->outputText($response));
break;
}
$functionCalls->each(function (array $call) {
info("Running tool: {$call['name']}");
$output = match ($call['name']) {
'get_current_time' => now()->toIso8601String(),
'read_file' => file_get_contents(
base_path(json_decode($call['arguments'])->path)
),
default => "Error: unknown tool {$call['name']}",
};
// Send the result back, matched to the request by call_id.
$this->history[] = [
'type' => 'function_call_output',
'call_id' => $call['call_id'],
'output' => (string) $output,
];
});
}
}
}
protected function runModel(): array
{
return Http::withToken(config('services.openai.key'))
->post('https://api.openai.com/v1/responses', [
'model' => config('services.openai.model'),
'instructions' => 'You are a helpful assistant.',
'input' => $this->history,
'tools' => [
[
'type' => 'function',
'name' => 'get_current_time',
'description' => 'Get the current server time as an ISO 8601 string.',
],
[
'type' => 'function',
'name' => 'read_file',
'description' => 'Read the contents of a file, relative to the project root.',
'parameters' => [
'type' => 'object',
'properties' => [
'path' => ['type' => 'string', 'description' => 'The relative path to the file.'],
],
'required' => ['path'],
'additionalProperties' => false,
],
'strict' => true,
],
],
])
->throw()
->json();
}
// The first output item is not always a message (it can be a tool call),
// so collect the text from every message item instead of using [0].
protected function outputText(array $response): string
{
return collect($response['output'])
->where('type', 'message')
->flatMap(fn ($message) => $message['content'])
->where('type', 'output_text')
->pluck('text')
->implode("\n");
}
}
The small outputText() helper replaces output[0].content[0].text. Once tools are involved, the first output item is often a function call rather than a message.

Adding a tools array to the request: get_current_time.

Answering a tool call with a function_call_output and its call_id.

A tool with parameters: read_file takes a path.

Collecting every function_call with collect()->filter().