Skip to main content

Requirements

Before installing LarAgent, make sure your environment meets the following requirements:

Installation

You can install LarAgent via Composer:
composer require maestroerror/laragent
After installing the package, publish the configuration file:
php artisan vendor:publish --tag="laragent-config"
This will create a config/laragent.php file in your application.

Configuration

OpenAi

If you are using OpenAI API, just set your API key in your .env file and you are good to go:
OPENAI_API_KEY=your-openai-api-key

Configuration file

The published configuration file contains the following settings and default providers:
Configuration file provided below is an example, please refer to the latest version of config file.
// config for Maestroerror/LarAgent
return [
    'default_driver' => \LarAgent\Drivers\OpenAi\OpenAiCompatible::class,
    'default_chat_history' => \LarAgent\History\InMemoryChatHistory::class,

    'namespaces' => [
        'App\\AiAgents\\',
        'App\\Agents\\',
    ],

    'providers' => [
        'default' => [
            'label' => 'openai',
            'api_key' => env('OPENAI_API_KEY'),
            'driver' => \LarAgent\Drivers\OpenAi\OpenAiDriver::class,
            'default_truncation_threshold' => 50000,
            'default_max_completion_tokens' => 10000,
            'default_temperature' => 1,
        ],

        'gemini' => [
            'label' => 'gemini',
            'api_key' => env('GEMINI_API_KEY'),
            'driver' => \LarAgent\Drivers\OpenAi\GeminiDriver::class,
            'default_truncation_threshold' => 1000000,
            'default_max_completion_tokens' => 10000,
            'default_temperature' => 1,
        ],

        'gemini_native' => [
            'label' => 'gemini',
            'api_key' => env('GEMINI_API_KEY'),
            'driver' => \LarAgent\Drivers\Gemini\GeminiDriver::class,
            'default_truncation_threshold' => 1000000,
            'default_max_completion_tokens' => 10000,
            'default_temperature' => 1,
        ],

        'groq' => [
            'label' => 'groq',
            'api_key' => env('GROQ_API_KEY'),
            'driver' => \LarAgent\Drivers\Groq\GroqDriver::class,
            'default_truncation_threshold' => 131072,
            'default_max_completion_tokens' => 131072,
            'default_temperature' => 1,
        ],

        'claude' => [
            'label' => 'claude',
            'api_key' => env('ANTHROPIC_API_KEY'),
            'model' => 'claude-3-7-sonnet-latest',
            'driver' => \LarAgent\Drivers\Anthropic\ClaudeDriver::class,
            'default_truncation_threshold' => 200000,
            'default_max_completion_tokens' => 8192,
            'default_temperature' => 1,
        ],

        'openrouter' => [
            'label' => 'openrouter',
            'api_key' => env('OPENROUTER_API_KEY'),
            'model' => 'openai/gpt-oss-20b:free',
            'driver' => \LarAgent\Drivers\OpenAi\OpenRouter::class,
            'default_truncation_threshold' => 200000,
            'default_max_completion_tokens' => 8192,
            'default_temperature' => 1,
        ],

        'ollama' => [
            'label' => 'ollama',
            'driver' => \LarAgent\Drivers\OpenAi\OllamaDriver::class,
            'default_truncation_threshold' => 131072,
            'default_max_completion_tokens' => 131072,
            'default_temperature' => 0.8,
        ],
    ],

    'fallback_provider' => null,
];

Custom Providers

You can configure additional providers with custom settings:
// Example custom provider with all possible configurations
'custom_provider' => [
    // Just name for reference, changes nothing
    'label' => 'custom',
    'api_key' => env('PROVIDER_API_KEY'),
    'api_url' => env('PROVIDER_API_URL'),
    // Defaults (Can be overriden per agent)
    'model' => 'your-provider-model',
    'driver' => \LarAgent\Drivers\OpenAi\OpenAiDriver::class,
    'chat_history' => \LarAgent\History\InMemoryChatHistory::class,
    'default_truncation_threshold' => 15000,
    'default_max_completion_tokens' => 100,
    'default_temperature' => 0.7,
    // Enable/disable parallel tool calls
    'parallel_tool_calls' => true,
    // Store metadata with messages
    'store_meta' => true,
    // Save chat keys to memory via chatHistory
    'save_chat_keys' => true,
],

Creating Your First Agent

Using Artisan Command

The quickest way to create a new agent is using the provided Artisan command:
php artisan make:agent WeatherAgent
This will create a new agent class in the App\AiAgents directory with all the necessary boilerplate code.

Basic Usage

Simple Response

use App\AiAgents\WeatherAgent;

// Create an instance for a specific user or chat session
$agent = WeatherAgent::for('user-123');

// Get a response
$response = $agent->respond("What's the weather like in Boston?");

echo $response; // "The weather in Boston is currently..."

Adding Tools

Tools allow your agent to perform actions and access external data:
namespace App\AiAgents;

use LarAgent\Agent;
use LarAgent\Attributes\Tool;

class WeatherAgent extends Agent
{
  // ... other properties

  #[Tool('Get the current weather in a given location')]
  public function getCurrentWeather($location, $unit = 'celsius')
  {
      // Call a weather API or service
      return "The weather in {$location} is 22 degrees {$unit}.";
  }
}

Next Steps

Now that you have LarAgent set up, you can explore more advanced features:

Agents

Learn more about creating and configuring agents

Tools

Discover how to create powerful tools for your agents

Structured Output

Get responses in structured formats like JSON

Streaming

Stream responses in real-time for better UX