> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Limit Past Session Search

> Limit the number of prior session previews returned by search_past_sessions.

Register the `search_past_sessions` tool and limit how many prior session previews it returns. The previews are tool results that the agent can inspect when it chooses to search past sessions.

**1. Create a Python file**

```python last_n_session_messages.py theme={null}
import os

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

# Remove the tmp db file before running the script
if os.path.exists("tmp/data.db"):
    os.remove("tmp/data.db")

agent = Agent(
    model=OpenAIResponses(id="gpt-5.2"),
    user_id="user_1",
    db=SqliteDb(db_file="tmp/data.db"),
    search_past_sessions=True,  # register the prior-session search tool
    num_past_sessions_to_search=2,  # return at most two prior session previews
)

session_1_id = "session_1_id"
session_2_id = "session_2_id"
session_3_id = "session_3_id"
session_4_id = "session_4_id"
session_5_id = "session_5_id"

agent.print_response("What is the capital of South Africa?", session_id=session_1_id)
agent.print_response("What is the capital of China?", session_id=session_2_id)
agent.print_response("What is the capital of France?", session_id=session_3_id)
agent.print_response("What is the capital of Japan?", session_id=session_4_id)
agent.print_response(
    "What did I discuss in my previous conversations?", session_id=session_5_id
)  # The search tool can return at most two prior session previews.
```

<Snippet file="create-venv-step.mdx" />

**2. Install dependencies**

```bash theme={null}
uv pip install -U agno openai sqlalchemy
```

**3. Export your OpenAI API key**

<Snippet file="set-openai-key.mdx" />

**4. Run Agent**

```bash theme={null}
python last_n_session_messages.py
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.