This holds the loop from AgentCommand, now split into prompt(), runTool() and runModel(). It only sends tools and text.format when the agent has some.
New file · app/Ai/Agents/Agent.php
<?php
namespace App\Ai\Agents;
use App\Ai\Tools\Tool;
use Illuminate\Support\Facades\Http;
use function Laravel\Prompts\info;
class Agent
{
/** The conversation so far: everything we send back to the AI each time. */
protected array $history = [];
// ---------------------------------------------------------------
// Things a child agent can override
// ---------------------------------------------------------------
/** Who the agent is. Override this in a child class. */
public function persona(): string
{
return 'You are a helpful AI assistant.';
}
/** The tools this agent may call. */
public function tools(): array
{
return [];
}
/** Optional JSON schema the final answer must match (structured output). */
public function schema(): array
{
return [];
}
// ---------------------------------------------------------------
// Instructions: what the AI is told before every request
// ---------------------------------------------------------------
public function instructions(): string
{
return $this->persona();
}
public function messages(): array
{
return $this->history;
}
// ---------------------------------------------------------------
// The agent loop
// ---------------------------------------------------------------
public function prompt(string $prompt): string
{
$this->history[] = ['role' => 'user', 'content' => $prompt];
// The inner loop: the agent's "inner dialogue".
while (true) {
$response = $this->runModel();
// Record everything the AI said, including any tool requests.
$this->history = [...$this->history, ...$response['output']];
$toolCalls = collect($response['output'])
->filter(fn (array $item) => $item['type'] === 'function_call');
// No tools requested? Then this is the final answer.
if ($toolCalls->isEmpty()) {
return static::text($response);
}
$toolCalls->each(fn (array $call) => $this->runTool($call));
}
}
protected function runTool(array $call): void
{
if (app()->runningInConsole()) {
info("Running tool: {$call['name']}({$call['arguments']})");
}
$tool = collect($this->tools())
->first(fn (Tool $tool) => $tool->definition()['name'] === $call['name']);
$output = $tool
? $tool->use(json_decode($call['arguments'], associative: true) ?? [])
: "Error: unknown tool {$call['name']}";
// Send the result back, linked to the request by its call_id.
$this->history[] = [
'type' => 'function_call_output',
'call_id' => $call['call_id'],
'output' => (string) $output,
];
}
protected function runModel(): array
{
$payload = [
'model' => config('services.openai.model'),
'instructions' => $this->instructions(),
'input' => $this->history,
];
if ($this->tools()) {
$payload['tools'] = array_map(fn (Tool $tool) => $tool->definition(), $this->tools());
}
// Only ask for structured output when the agent defines a schema.
if ($this->schema()) {
$payload['text'] = [
'format' => [
'type' => 'json_schema',
'name' => 'assistant_response',
'strict' => true,
'schema' => $this->schema(),
],
];
}
return Http::withToken(config('services.openai.key'))
->timeout(120)
->post('https://api.openai.com/v1/responses', $payload)
->throw()
->json();
}
/**
* Pull every piece of text out of a response.
* Safer than $response['output'][0]['content'][0]['text'],
* because the first output item is not always a message.
*/
public static function text(array $response): string
{
return collect($response['output'] ?? [])
->where('type', 'message')
->flatMap(fn (array $message) => $message['content'] ?? [])
->where('type', 'output_text')
->pluck('text')
->implode("\n");
}
}

The loop now lives in the Agent class.