Skip to main content

Creating an Agent

The recommended way to create a new agent is using the artisan command:
This generates a new agent class in the App\AiAgents directory with all the necessary boilerplate:
App/AiAgents/MyAgent.php
Name your agents descriptively based on their purpose. CustomerSupportAgent is clearer than Agent1 or MyAgent.

Core Methods

Override these methods to customize your agent’s behavior:

instructions()

The system prompt that shapes how your agent responds. This defines the agent’s personality, role, and guidelines.
For simple, static instructions you can use the $instructions property instead of the method.
For complex or dynamic instructions, consider using Blade template files. This keeps your agent class clean and allows you to leverage Blade’s templating features like conditionals and loops.

prompt()

Process or augment the user’s message before sending it to the LLM. Useful for adding context, formatting, or implementing RAG patterns.

model()

Dynamically determine which model to use based on custom logic:

API Key and URL

Override these methods to dynamically set API credentials:

Configuration Properties

Model & Provider

LarAgent supports multiple AI providers including OpenAI, Anthropic, Gemini, Groq, and Ollama. See LLM Drivers for setup instructions and available options.

Response Settings

Control how the LLM generates responses:
Controls randomness in responses. Range: 0.0 (focused/deterministic) to 2.0 (creative/random).
Maximum number of tokens in the AI’s response.
Generate multiple completion choices. Note: You’ll be charged for all generated tokens.
Alternative to temperature. The model considers tokens with top_p probability mass.
Use either $temperature or $topP, not both.
Penalizes tokens based on their frequency in the text. Range: -2.0 to 2.0.
Penalizes tokens based on whether they’ve appeared. Range: -2.0 to 2.0.
All configuration properties can also be set via the provider settings in config/laragent.php.

History & Tools

Both Context (chat history, data models) and Tools are core features of LarAgent with extensive configuration options. The properties shown here are just the basics.
  • Learn about context management, storage drivers, and truncation strategies in the Context page
  • Explore tool creation, parameters, and execution in the Tools Section

Arbitrary Configuration

Pass custom configuration values to the driver for provider-specific settings or experimental features:
Custom configurations are passed directly to the driver, allowing you to leverage provider-specific features without modifying the agent’s core properties.

Runtime Configuration

Override any configuration at runtime using chainable methods:
All properties have corresponding chainable methods:
  • $temperaturetemperature(float $temp)
  • $maxCompletionTokensmaxCompletionTokens(int $tokens)
  • $topPtopP(float $value)
  • And so on…

Next Steps

Responses

Learn how to interact with agents and handle their responses.

Streaming

Stream responses in real-time for better user experience.

Tools

Give your agents capabilities to perform actions and retrieve data.

Chat History

Manage conversation history and context persistence.