Skip to main content
Agent classes are the foundation of everything in LarAgent. They define how your AI assistants behave, what capabilities they have, and how they process information. Other LarAgent features like Tools, Storage, Prompts, etc all revolve around agents.

What are Agents?

In LarAgent, an Agent is a PHP class that represents an AI-powered assistant. Each agent encapsulates:
  • Instructions — The system prompt that defines the agent’s personality, role, and behavior
  • Model configuration — Which LLM to use and how it should respond
  • Tools — Functions the agent can call to perform actions or retrieve information
  • Context — How conversations (chat history) and other data models are stored and retrieved
Think of an agent as a specialized AI assistant tailored to a specific task or domain. You might have a CustomerSupportAgent for handling support tickets, a DataAnalysisAgent for processing reports, or a SchedulingAgent for managing appointments. Or one agent that ochestrates all mentioned above!

Why Agents?

LarAgent’s agent-centric architecture provides several benefits:

Reusability

Define an agent once and use it across your application with different chat sessions.

Encapsulation

Keep all AI-related configuration in one place — instructions, tools, and settings.

Testability

Test your agents in isolation with predictable inputs and outputs.

Flexibility

Override any configuration at runtime for specific use cases.

Agent Lifecycle

When you interact with an agent, here’s what happens:
1

Initialization

The agent is created with its configured instructions, model, and tools.
2

Message Processing

Your message passes through the prompt() method, where you can add context or transform the input.
3

LLM Request

The agent sends the conversation history and your message to the configured LLM.
4

Tool Execution

If the LLM requests tool calls, the agent executes them and continues the conversation.
5

Response

The final response is returned and stored in the chat history.

Quick Example

Here’s what a simple agent looks like:
namespace App\AiAgents;

use LarAgent\Agent;

class AssistantAgent extends Agent
{
    protected $model = 'gpt-4o-mini';
    
    protected $history = 'cache';

    public function instructions()
    {
        return "You are a helpful assistant. Be concise and friendly.";
    }
}
Using the agent is straightforward:
// Start or continue a conversation
$response = AssistantAgent::for('user-123')->respond('Hello, how can you help me?');

// Or for one-off interactions without persisted history
$response = AssistantAgent::ask('What is 2 + 2?');

Next Steps