For all forms of session history, you need to have a database assigned to the agent. See Storage for more details.
Agent-Level History
- You can set
add_history_to_context=Trueandnum_history_runs=5to add the inputs and responses from the last 5 runs automatically to every request sent to the agent. - You can be more granular about how many messages to add to include in the list sent to the model, by setting
num_history_messages. - You can set
read_chat_history=Trueto provide aget_chat_history()tool to your agent allowing it to read any message in the entire chat history. - You can set
read_tool_call_history=Trueto provide aget_tool_call_history()tool to your agent allowing it to read tool calls in reverse chronological order. - You can enable
search_session_historyto allow searching through previous sessions.
History Reference
- Simple History
- Long Conversations
- Multi-Session Memory
Start with Agent History in Context for basic conversation continuity:
Database Requirement: All history features require a database configured on the agent. See Storage for setup.
Add history to the agent context
To add the history of the conversation to the context, you can setadd_history_to_context=True.
This will add the inputs and responses from the last 3 runs (that is the default) to the context of the agent.
You can change the number of runs by setting num_history_runs=n where n is the number of runs to include.
You can either set add_history_to_context=True on the Agent or on the run() method directly.
See the Persistent Session with History example for a complete implementation.
Learn more in the Context Engineering documentation.
Read the chat history
To read the chat history, you can setread_chat_history=True.
This will provide a get_chat_history() tool to your agent allowing it to read any message in the entire chat history.
See the Chat History Management page for a complete implementation.
Search the session history
In some scenarios, you might want to fetch messages from across multiple sessions to provide context or continuity in conversations. To enable fetching messages from the last N sessions, you need to use the following flags:search_session_history: Set this toTrueto allow searching through previous sessions.num_history_sessions: Specify the number of past sessions to include in the search. In the example below, it is set to2to include only the last 2 sessions.num_history_sessions: Specify the number of past sessions to include in the search.