Learn about Agent sessions.
Agent.run()
, it creates a stateless, singular Agent run.
But what if we want to continue this conversation i.e. have a multi-turn conversation? That’s where “Sessions” come in. A session is collection of consecutive runs.
In practice, a session is a multi-turn conversation between a user and an Agent. Using a session_id
, we can connect the conversation history and state across multiple runs.
Here are the core concepts:
session_id
and house all runs, metrics, state and other data that belong to the session.run_id
and Agent.run()
creates a new run_id
when called.run_id
is automatically generated, as well as a session_id
(because we didn’t provide one tot yet associated with a user.
user_id
to connect a user to their sessions with the Agent.
In the example below, we set a session_id
to demo how to have multi-turn conversations with multiple users at the same time.
Multi-user, multi-session example
Run the example
enable_session_summaries=True
on the Agent
.
Session summary example
Run the example
session_summary_prompt
to the Agent
.
The SessionSummaryManager
class is responsible for handling the model used to create and update session summaries.
You can adjust it to personalize how summaries are created and updated:
agent.get_messages_for_session()
-> Gets access to all the messages for the session, for the current agent.agent.get_chat_history()
-> Gets access to all the unique messages for the session.add_history_to_context=True
and num_history_runs=5
to add the messages from the last 5 runs automatically to every message sent to the agent.read_chat_history=True
to provide a get_chat_history()
tool to your agent allowing it to read any message in the entire chat history.add_history_to_context=True
, num_history_runs=3
and read_chat_history=True
for the best experience.read_tool_call_history=True
to provide a get_tool_call_history()
tool to your agent allowing it to read tool calls in reverse chronological order.Session history example
Run the example
search_session_history
: Set this to True
to 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 to 2
to include only the last 2 sessions.