> ## 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.

# Sessions and memory

> Persistent multi-turn conversations and per-user memory.

An agent needs two kinds of state: what was said in this thread and what the agent knows about this user. Agno stores both in its configured database.

```python theme={null}
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.5"),
    db=db,
    add_history_to_context=True,
    num_history_runs=5,
    update_memory_on_run=True,
)

agent.run(
    "My name is Sarah and I prefer email over phone.",
    user_id="sarah@acme.com",
    session_id="thread-42",
)
reply = agent.run(
    "What's the best way to reach me?",
    user_id="sarah@acme.com",
    session_id="thread-99",
).content
# The sessions have separate histories. Stored user memory can carry the preference.
```

## Sessions vs memory

Session history and memory solve different problems and can be used together.

| | Session history | Memory |
| - | - | - |
| Stores | The messages in this conversation thread | Learned facts about the user |
| Scope | One `session_id` | One `user_id`, across all their sessions |
| Enable with | `add_history_to_context=True` | `enable_agentic_memory=True` or `update_memory_on_run=True` |
| Answers | "What did we just discuss?" | "What do I know about this person?" |

## Identifiers

| Identifier | Distinguishes | Maps to in your product |
| - | - | - |
| `user_id` | The person | Your auth subject (user ID, email) |
| `session_id` | A conversation thread for that person | A chat tab, a Slack thread, a support case |

Pass both on every run. `session_id` scopes conversation threads, while `user_id` scopes memory.

## Memory: automatic or agentic

| Mode | Set | Use when |
| - | - | - |
| Automatic | `update_memory_on_run=True` | You want memory computed after every run. Without a `user_id`, memories land under the `default` user. |
| Agentic | `enable_agentic_memory=True` | The agent decides using tool calls. |

## Reading memory back

For a profile screen or a debug view, pull a user's memories directly.

```python theme={null}
memories = agent.get_user_memories(user_id="sarah@acme.com")
```

## Long conversations

`num_history_runs` bounds the history that flows into context. It defaults to `3`. Session summaries give the agent recall of the earlier thread on top of that bounded history.

| Technique | Effect |
| - | - |
| `num_history_runs=N` | Only the last N stored runs flow into context |
| `enable_session_summaries=True` | A running summary of the session is added to the system prompt, in addition to the history already in context |

## Next steps

| Task | Guide |
| - | - |
| Put this behind an HTTP API | [Serve as an API](https://agno-v2.mintlify.site/use-cases/product-agents/serve-as-an-api.md) |
| Carry memory across Slack and web | [Interfaces](https://agno-v2.mintlify.site/use-cases/product-agents/interfaces.md) |
| Give the agent external data | [Connecting your data](https://agno-v2.mintlify.site/use-cases/product-agents/connecting-your-data.md) |

## Developer Resources

* [Sessions](https://agno-v2.mintlify.site/sessions/overview.md)
* [Session management](https://agno-v2.mintlify.site/sessions/session-management.md)
* [Memory](https://agno-v2.mintlify.site/memory/overview.md)
* [Memory cookbook](https://github.com/agno-agi/agno/tree/main/cookbook/11_memory)


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