# Session Summaries (/sessions/session-summaries)



Session summaries add a model-generated description of earlier interactions to the next run's context. They can omit details or contain mistakes, so retain the original history when exact wording matters.

History is off by default. When enabled, it normally includes the last three runs. Summaries do not automatically replace that history: both are added when both context flags are enabled. Use `add_history_to_context=False` for summary-only context, or combine a summary with a small recent-history window.

Generating a summary makes an additional model request. By default that request reads all eligible session messages; use `SessionSummaryManager(last_n_runs=..., conversation_limit=...)` to bound its own input. A smaller main-model prompt does not guarantee lower total cost once summary generation is included.

## Setup [#setup]

Create and activate a [virtual environment](/sdk/setup), then install the dependencies and set your key:

```bash
uv pip install agno openai sqlalchemy "psycopg[binary]"
export OPENAI_API_KEY="your-api-key"
```

Start Docker and the local PostgreSQL server below, or use an existing server and update the connection URL. Check readiness with `docker exec pgvector pg_isready -U ai -d ai` before running Python.

<Steps>
    <Step title="Run PgVector">
      <CodeBlockTabs defaultValue="macOS / Linux">
        <CodeBlockTabsList>
          <CodeBlockTabsTrigger value="macOS / Linux">
            macOS / Linux
          </CodeBlockTabsTrigger>

          <CodeBlockTabsTrigger value="Windows">
            Windows
          </CodeBlockTabsTrigger>
        </CodeBlockTabsList>

        <CodeBlockTab value="macOS / Linux">
          ```bash
          docker run -d \
            -e POSTGRES_DB=ai \
            -e POSTGRES_USER=ai \
            -e POSTGRES_PASSWORD=ai \
            -e PGDATA=/var/lib/postgresql \
            -v pgvolume:/var/lib/postgresql \
            -p 5532:5432 \
            --name pgvector \
            agnohq/pgvector:18
          ```
        </CodeBlockTab>

        <CodeBlockTab value="Windows">
          ```powershell
          docker run -d `
            -e POSTGRES_DB=ai `
            -e POSTGRES_USER=ai `
            -e POSTGRES_PASSWORD=ai `
            -e PGDATA=/var/lib/postgresql `
            -v pgvolume:/var/lib/postgresql `
            -p 5532:5432 `
            --name pgvector `
            agnohq/pgvector:18
          ```
        </CodeBlockTab>
      </CodeBlockTabs>
    </Step>
</Steps>

## How It Works [#how-it-works]

Session summaries follow a simple three-step pattern:

<Steps>
  <Step title="Enable Summary Generation">
    Set `enable_session_summaries=True` on your agent or team. Summaries are automatically created and updated after runs when there are meaningful messages to summarize, then stored in your database.
  </Step>

  <Step title="Use Summaries in Context">
    Set `add_session_summary_to_context=True` to include the summary in your messages (this is enabled by default if you enable session summary generation). The summary is added alongside any enabled raw history. Set `add_history_to_context=False` to send the summary without those historical messages.
  </Step>

  <Step title="Customize (Optional)">
    Use [`SessionSummaryManager`](/reference/session/summary_manager) to control summary generation - use a cheaper model, customize prompts, or change the summary format. This lets you optimize costs by using a lightweight model for summaries while your main agent keeps a more capable one.
  </Step>
</Steps>

## Enable Session Summaries [#enable-session-summaries]

Turn on `enable_session_summaries=True` to have Agno maintain a rolling summary for each session. Summaries sit alongside the stored history and can be reused later to save tokens.

<CodeBlockTabs defaultValue="Agent">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Agent">
      Agent
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="Team">
      Team
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Agent">
    ```python
    from agno.agent import Agent
    from agno.db.postgres import PostgresDb
    from agno.models.openai import OpenAIResponses

    agent = Agent(
        model=OpenAIResponses(id="gpt-5.2"),
        db=PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),
        enable_session_summaries=True,
        add_history_to_context=False,
    )

    agent.print_response("Hi my name is John and I live in New York", session_id="conversation_123")

    # Retrieve the summary
    summary = agent.get_session_summary(session_id="conversation_123")
    if summary:
        print(summary.summary, summary.topics)
    ```
  </CodeBlockTab>

  <CodeBlockTab value="Team">
    ```python
    from agno.team import Team
    from agno.db.postgres import PostgresDb
    from agno.models.openai import OpenAIResponses

    team = Team(
        members=[],  # Minimal leader-only team
        model=OpenAIResponses(id="gpt-5.2"),
        db=PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),
        enable_session_summaries=True,
        add_history_to_context=False,
    )

    team.print_response("Hi my name is John and I live in New York", session_id="conversation_123")

    # Retrieve the summary
    summary = team.get_session_summary(session_id="conversation_123")
    if summary:
        print(summary.summary, summary.topics)
    ```
  </CodeBlockTab>
</CodeBlockTabs>

### Customizing Generation [#customizing-generation]

* Provide a [`SessionSummaryManager`](/reference/session/summary_manager) to specify a cheaper model or custom prompt
* Limit what the summary model sees with `last_n_runs` or `conversation_limit` on the manager

## Use Summary in Context [#use-summary-in-context]

`add_session_summary_to_context=True` is enabled by default if you enable session summary generation. To reuse an existing stored summary without updating it, set only `add_session_summary_to_context=True`. The snippets below reuse `conversation_123` created in the matching Agent or Team example above; a new session has no summary to load. Alternatively, if you don't want to use summaries in context, you can set `add_session_summary_to_context=False`.

<CodeBlockTabs defaultValue="Agent">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Agent">
      Agent
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="Team">
      Team
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Agent">
    ```python
    from agno.agent import Agent
    from agno.db.postgres import PostgresDb
    from agno.models.openai import OpenAIResponses

    db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")

    agent = Agent(
        model=OpenAIResponses(id="gpt-5.2"),
        db=db,
        add_session_summary_to_context=True,
    )

    agent.print_response("Hi my name is John and I live in New York", session_id="conversation_123")
    ```
  </CodeBlockTab>

  <CodeBlockTab value="Team">
    ```python
    from agno.team import Team
    from agno.db.postgres import PostgresDb
    from agno.models.openai import OpenAIResponses

    db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")

    team = Team(
        members=[],  # Minimal leader-only team
        model=OpenAIResponses(id="gpt-5.2"),
        db=db,
        add_session_summary_to_context=True,
    )

    team.print_response("Hi my name is John and I live in New York", session_id="conversation_123")
    ```
  </CodeBlockTab>
</CodeBlockTabs>

Agno loads the stored summary when it loads the session. You can still mix in recent history:

<CodeBlockTabs defaultValue="Agent">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Agent">
      Agent
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="Team">
      Team
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Agent">
    ```python
    agent = Agent(
        model=OpenAIResponses(id="gpt-5.2"),
        db=db,
        add_session_summary_to_context=True,
        add_history_to_context=True,
        num_history_runs=2,  # Summary for long-term memory, last 2 runs for detail
    )
    ```
  </CodeBlockTab>

  <CodeBlockTab value="Team">
    ```python
    team = Team(
        members=[],  # Minimal leader-only team
        model=OpenAIResponses(id="gpt-5.2"),
        db=db,
        add_session_summary_to_context=True,
        add_history_to_context=True,
        num_history_runs=2,  # Summary for long-term memory, last 2 runs for detail
    )
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## When to Use Session Summaries [#when-to-use-session-summaries]

**✅ Best for:**

* Long-running customer support conversations
* Multi-day or multi-week interactions
* Conversations with 10+ turns
* Production systems where cost matters

**⚠️ Consider alternatives for:**

* Short conversations (fewer than 5 turns)
* When full detail is critical
* Real-time chat with recent context only

## Developer Resources [#developer-resources]

* [SessionSummaryManager reference](/reference/session/summary_manager)
* [Session management](/sessions/session-management)
* [Agent metrics](/sessions/metrics/agent)
