Overview
Chat history is managed through theChatHistoryStorage class, which automatically:
- Persists messages across requests
- Maintains message order
- Supports multiple storage drivers (cache, file, database, session)
- Provides lazy loading for performance
- Tracks dirty state to avoid unnecessary writes
Configuration
LarAgent provides multiple levels of configuration, from global defaults to per-agent customization.Global Configuration
Set default history storage drivers for all agents inconfig/laragent.php:
Per-Provider Configuration
Configure history storage for specific providers:Per-Agent Configuration
Set storage drivers directly in your agent class using the$history property:
- Using String Aliases
- Using Driver Classes
- Method Override
Available Storage Aliases
Driver Chain (Fallback Pattern)
LarAgent supports configuring multiple drivers in a chain. The first driver is the primary, and subsequent drivers serve as fallbacks:- Reading: Tries the primary driver first. If it returns no data, falls back to the next driver.
- Writing: Writes to all drivers in the chain to keep them synchronized.
- Removing: Removes from all drivers.
Storage Drivers Reference
Learn about all available storage drivers, their configuration options, and custom model examples.
Working with Chat History
Accessing Chat History
Adding Messages Manually
Metadata Storage
By default, only message content is stored. Enable metadata storage to persist additional information like agent name, model used, and custom data.Enable via Property
Enable via Provider Config
Metadata Contents
Clearing Chat History
Check out the Context Facade for more on using the
Context facade.Manual Read/Save Operations
Force Read/Save Flags
Control when chat history is synchronized with storage:Default Behavior
By default, LarAgent uses lazy loading for reading and end-of-request saving:- Reading: Chat history is loaded from storage only when first accessed
- Saving: Chat history is saved automatically when the request ends
Long-Running Processes
In long-running environments (Laravel Octane, FrankenPHP, Swoole), agent instances may persist across requests. Use both flags to ensure data freshness:Truncation Strategies
When conversations exceed the model’s context window, truncation strategies automatically reduce the conversation size while preserving important context.Enabling Truncation
- Global Config
- Per-Provider
- Per-Agent
Full Control via Method Override
Override thetruncationStrategy() method for full control:
Built-in Strategies
SimpleTruncationStrategy
Keeps the last N messages, discarding older ones. Fast and simple.SummarizationStrategy
Summarizes removed messages using an AI agent, preserving context.SymbolizationStrategy
Creates brief “symbols” for each removed message, providing a timeline.Runtime Configuration
Understanding Thresholds
Effective threshold calculation:- Maximum: 80% of model’s context window (aggressive)
- Recommended: 30-50% (balanced)
- Conservative: 20-30% (for agents with large tool outputs)
Creating Custom Strategies
Use the artisan command to scaffold a custom strategy:app/TruncationStrategies/CustomTruncationStrategy.php:
Example: Priority-Based Truncation
Example: Priority-Based Truncation
Keep messages marked as important:
Events
Chat history operations emit events that you can listen to for logging, validation, or custom behavior. Available events include:MessageAdding/MessageAddedChatHistorySaving/ChatHistorySavedChatHistoryLoadedChatHistoryTruncated
Chat History Events
Learn how to listen to and handle chat history events for custom behavior.
Next Steps
Context Overview
Understand the Context and Identity system for storage isolation.
Usage Tracking
Track token usage and costs across agent interactions.

