Chat history is a crucial component that allows your agents to maintain
context across multiple interactions, providing a more natural and coherent
conversation experience.
Built-in Chat Histories
LarAgent provides several built-in chat history implementations to suit different needs:- In Laravel
- Outside Laravel
Set in your Agent class by the name:Or use it by class:Or use it by override method to add custom logic or configuration:
yourAgent.php
yourAgent.php
yourAgent.php
Chat History Configuration
You can configure how chat history behaves using these properties in your Agent class:Reinjecting Instructions
$reinjectInstructionsPer property defines when to reinject these instructions to ensure the agent stays on track. By default, it’s set to 0 (disabled).
Managing Context Window Size
Managing chatHistory SessionId
false (disabled). If you want to include model name in chat session id, set it to true.
Agent automatically appends the Agent class basename and model name used on each message’s metadata to keep track of the agent and model used.
You can access metadata of message using getMetadata() method or store it in chat history automatically by setting storeMeta property to true in your agent class.
Check Storing usage data section for more information.
How chat history works
The flow of information in the chat history process works as follows:- User to Agent: The user sends a prompt to the agent.
- Agent to Chat History: The agent processes and adds the user’s message to the chat history.
- Chat History to Agent: The agent retrieves all relevant messages from the chat history.
- Agent to LLM: The agent sends these messages as context to the language model.
- LLM to Agent: The language model generates a response based on the provided context.
- Agent to Chat History: The agent processes and adds the LLM’s response to the chat history.
- Agent to User: The agent displays the last message from the chat history to the user.
- Tracks all messages in the conversation
- Counts tokens to ensure they stay within model limits
- Prunes older messages when necessary to maintain the context window size
Extensibility
You can implement custom logic for context window management using events and the chat history instance inside your agent. Or create custom chat history implementations by implementing theChatHistoryInterface.
Using Chat History
Per-User Chat History
One of the most common use cases is maintaining separate chat histories for different users:Named Chat Histories
You can also create named chat histories for specific contexts or topics:Random Chat Histories
If you don’t specify chat history name withfor or forUser methods, there will be used a random chat key:
Without history name
Str facade, but you can customize the automatic key generation by overriding the generateRandomKey static method in your Agent class:
You can use
generateRandomKey method to set non-random, but dynamic or
semi-random keys. The point is that, this method is used to automatically set
chat name when there is no for or forUser methods specifing the custom
history name.Accessing and Managing Chat History
LarAgent provides several methods to access and manage chat history:The
addMessage(MessageInterface $message) method adds a new message to the
chat history instance, which will be saved automatically by the agent class if
you are using it in agent context. In other case, you can save it manually
using writeToMemory() method.Creating Custom Chat Histories
You can create your own chat history by implementing theChatHistoryInterface and extending the LarAgent\Core\Abstractions\ChatHistory abstract class.
Check example implementations in src/History
There are two ways to register your custom chat history into an agent. If you use standard constructor only with $name parameter, you can define it by class in $history property or provider configuration:
Agent Class
$name, you can override createChatHistory() method:
Storing usage data
You can store usage data (if available) automatically in the chat history by settingstore_meta config or $storeMeta property to true:
toArrayWithMeta() method to get array with metadata:
afterResponse hooks in your agent class:
Best Practices
Do choose the appropriate chat history implementation based on your needs
(persistence, performance, etc.)
Do set a reasonable context window size to balance coherence and token
usage
Do use unique identifiers for chat histories to prevent
cross-contamination

