Basic Agent¶
The simplest agent uses AgentLoop to send a message and get a response.
Hello World¶
use Cognesy\Agents\Core\AgentLoop;
use Cognesy\Agents\Core\Data\AgentState;
$loop = AgentLoop::default();
$state = AgentState::empty()->withUserMessage('What is 2+2?');
$result = $loop->execute($state);
echo $result->finalResponse()->toString();
// "2 + 2 equals 4."
What Happens¶
AgentLoop::execute()starts the loop- The driver (
ToolCallingDriver) sends messages to the LLM - LLM responds with text (no tool calls)
- The loop detects no tool calls and stops
- Final response is available via
$result->finalResponse()
Customizing the Loop¶
Use with() to swap components on the default loop:
use Cognesy\Agents\Core\Collections\Tools;
use Cognesy\Agents\Drivers\ReAct\ReActDriver;
// Add tools
$loop = AgentLoop::default()->withTool($myTool);
// Swap driver
$loop = AgentLoop::default()->withDriver(new ReActDriver(model: 'gpt-4o'));