Nested Team History

Demonstrates how nested teams (teams as members of other teams) maintain their own conversation history across multiple delegations.

nested_team_history.py
"""
Nested Team History
===================

Demonstrates how nested teams (teams as members of other teams) maintain
their own conversation history across multiple delegations.

Key concept: When a parent team delegates to a nested team, the nested team
receives its previous conversation history via `add_history_to_context=True`.
"""

from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIResponses
from agno.team import Team

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
db = SqliteDb(db_file="tmp/nested_team_history.db")

# ---------------------------------------------------------------------------
# Create Nested Team
# ---------------------------------------------------------------------------
analyst = Agent(
    name="Data Analyst",
    model=OpenAIResponses(id="gpt-5.6-sol"),
    role="Analyze data and provide insights",
)

research_team = Team(
    name="Research Team",
    model=OpenAIResponses(id="gpt-5.6-sol"),
    members=[analyst],
    add_history_to_context=True,
    role="Conduct research and analysis",
)

# ---------------------------------------------------------------------------
# Create Parent Team
# ---------------------------------------------------------------------------
writer = Agent(
    name="Writer",
    model=OpenAIResponses(id="gpt-5.6-sol"),
    role="Write and format outputs",
)

main_team = Team(
    name="Main Team",
    model=OpenAIResponses(id="gpt-5.6-sol"),
    members=[writer, research_team],
    db=db,
    add_history_to_context=True,
    mode="route",
    show_members_responses=True,
)

# ---------------------------------------------------------------------------
# Run Multi-Turn Conversation
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    session_id = "nested-team-demo"

    # Turn 1: Research request
    main_team.print_response(
        "Research the top 3 benefits of AI coding assistants",
        session_id=session_id,
        stream=True,
    )

    # Turn 2: Follow-up - research team should remember Turn 1
    main_team.print_response(
        "Based on your research, which benefit is best for startups?",
        session_id=session_id,
        stream=True,
    )

    # Turn 3: Another follow-up
    main_team.print_response(
        "What challenges might startups face adopting that?",
        session_id=session_id,
        stream=True,
    )

Follow the delegated history

Each nested team with add_history_to_context=True receives its own prior messages when it is delegated to again. The parent chooses the member for each request, so a turn routed elsewhere does not demonstrate that nested team's recall. Inspect the member responses to follow the actual route. The agents here reason from the conversation; they have no live research or diagnostic tools.

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno openai sqlalchemy

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"

Run the example

Save the code above as nested_team_history.py, then run:

python nested_team_history.py

Full source: cookbook/03_teams/07_session/nested_team_history.py