Skip to main content
The Chat History system in LarAgent stores and manages conversation messages between users and AI agents. It provides automatic persistence, lazy loading, and flexible storage driver configuration.

Overview

Chat history is managed through the ChatHistoryStorage 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 in config/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:

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:
How it works:
  • 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.
Use the fallback pattern for high availability. For example, cache for speed with file storage as a durable backup.

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

All messages support metadata. Use $message->addMeta([...]) to attach custom data or pass it during creation as second argument Message::user('text', ['key' => 'value']).

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.
You can also use artisan commands during development to clear chat histories.

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

Full Control via Method Override

Override the truncationStrategy() 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

The truncationThreshold is NOT the model’s context window. Set it to 30-50% of the model’s actual context window to leave room for system prompts, tools, and responses.
Effective threshold calculation:
Recommendations:
  • 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:
This creates app/TruncationStrategies/CustomTruncationStrategy.php:
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 / MessageAdded
  • ChatHistorySaving / ChatHistorySaved
  • ChatHistoryLoaded
  • ChatHistoryTruncated

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.