Chat History in Teams
Manage team session history and conversation context.
Before running the examples, create and activate a virtual environment:
uv pip install agno openai sqlalchemy
export OPENAI_API_KEY="your-api-key"Teams with storage enabled automatically have access to the run history of the session (also called the "conversation history" or "chat history").
We can give the Team access to the chat history in the following ways:
Team-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 team leader. - 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 team allowing it to read any message in the entire chat history. - You can enable
search_past_sessionsto allow searching through previous sessions. - You can set
add_team_history_to_members=Trueandnum_team_history_runs=5to add the inputs and responses from the last 5 runs (that is the team-level inputs and responses) automatically to every message sent to the team members.
Member-Level History:
- You can also enable
add_history_to_contextfor individual team members. This will only add the inputs and outputs for that member to all requests sent to that member, giving it access to its own history.
Working with team history can be tricky. Experiment with the above settings to find the best fit for your use case. See the History Reference for help on how to use the different history features.
The fragments below reuse this setup:
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.team import Team
german_agent = Agent(name="German", instructions="Answer German questions.")
spanish_agent = Agent(name="Spanish", instructions="Answer Spanish questions.")
profile_agent = Agent(name="Profile", instructions="Explain profile settings.")
billing_agent = Agent(name="Billing", instructions="Explain billing concepts.")History Reference
Start with Team History in Context for basic conversation continuity:
team = Team(
members=[german_agent, spanish_agent],
db=SqliteDb(db_file="tmp/team.db"),
add_history_to_context=True,
num_history_runs=5,
)Use Team History to Members for shared context:
team = Team(
members=[german_agent, spanish_agent],
db=SqliteDb(db_file="tmp/team.db"),
add_team_history_to_members=True,
num_team_history_runs=3,
)Share Member Interactions during a run:
team = Team(
members=[profile_agent, billing_agent],
db=SqliteDb(db_file="tmp/team.db"),
share_member_interactions=True,
)Add Chat History Tool when agents need to search history:
team = Team(
members=[german_agent, spanish_agent],
db=SqliteDb(db_file="tmp/team.db"),
read_chat_history=True, # Team decides when to look up
)Enable Multi-Session Search for cross-session continuity:
team = Team(
members=[german_agent, spanish_agent],
db=SqliteDb(db_file="tmp/team.db"),
search_past_sessions=True,
num_past_sessions_to_search=10,
)Durable history: Configure a database to persist and reload team history. See Database for setup.
Performance Tip: More history = larger context = slower and costlier requests. Start with num_history_runs=3 and increase only if needed.
Add history to the team context
To add the history of the conversation to the context, you can set add_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 team leader.
You can change the number of runs by setting num_history_runs=n where n is the number of runs to include.
See the Direct Response with Team History example for a complete implementation.
Send team history to members
To send the team history to the members, you can set add_team_history_to_members=True.
This will send the inputs and responses from the last 3 team-level runs (that is the default) to the members when tasks are delegated to them.
You can change the number of runs by setting num_team_history_runs=n where n is the number of runs to include.
When enabled, team history is appended to the task sent to a team member in this format:
<team_history_context>
[run-1]
input: Hallo, wie heißt du? Mein Name ist John.
response: Ich heiße ChatGPT.
</team_history_context>This allows members to access information from previous interactions with other team members.
See the Team History for Members example for a complete implementation.
Share member interactions with other members
All interactions with team members are automatically recorded, including the member name, the task given to the member, and the response from the member.
This feature is only available during a single run - it shares interactions that happen within the current execution.
If you want members to have access to all interactions that has happened during the current run, you can set share_member_interactions=True.
When enabled, interaction details are appended to the task sent to a team member in this format:
<member_interaction_context>
See below interactions with other team members.
Member: Web Researcher
Task: Find information about the web
Response: I found information about the web
Member: HackerNews Researcher
Task: Find information about the web
Response: I found information about the web
</member_interaction_context>See the Share Member Interactions example for a complete implementation.
Read the chat history
To read the chat history, you can set read_chat_history=True.
This will provide a get_chat_history() tool to your team allowing it to read any message in the entire chat history.
Search the session history
In some scenarios, you might want to fetch messages from across multiple sessions to provide context or continuity in conversations.
Set search_past_sessions=True to give the team two tools: search_past_sessions() returns previews of recent sessions, and read_past_session(session_id) returns the full conversation for a specific session.
num_past_sessions_to_search: Maximum number of past sessions to search. Defaults to 20.num_past_session_runs_in_search: Number of runs per session shown in the preview. Defaults to 3.
Keep num_past_sessions_to_search low to avoid filling up the context length of the model, which can lead to performance issues.
Developer Resources
Direct Response with History
Team leader routes requests with access to conversation history.
Team History for Members
Members access shared team history from previous interactions.
Member-Level History
Each member maintains its own isolated conversation history.
Share Member Interactions
Share member interactions during the current run to avoid duplicate work.