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

# Sessions and Memory

> Manage AgentOS sessions and user memories with the Python client.

```python sessions_and_memory.py theme={null}
"""Manage AgentOS sessions and user memories with the Python client.

The example creates a named agent session, runs in it, inspects its history,
and exercises create/read/update/delete memory calls.

Prerequisites: start ``_server.py`` and set OPENAI_API_KEY.
Run: .venvs/demo/bin/python cookbook/05_agent_os/03_python_client/03_sessions_and_memory.py
Try: inspect the named session before the example cleans it up.
"""

import asyncio

from agno.client import AgentOSClient
from agno.db.base import SessionType

BASE_URL = "http://localhost:7778"
AGENT_ID = "assistant"
USER_ID = "python-client-user"


# ---------------------------------------------------------------------------
# Create the Client
# ---------------------------------------------------------------------------


async def manage_session_and_memory() -> None:
    """Exercise the session lifecycle and memory CRUD APIs."""
    client = AgentOSClient(base_url=BASE_URL)
    session_id: str | None = None
    memory_id: str | None = None

    try:
        session = await client.create_session(
            session_type=SessionType.AGENT,
            user_id=USER_ID,
            session_name="Python client session",
            agent_id=AGENT_ID,
        )
        session_id = session.session_id
        print(f"Created session: {session_id}")

        run = await client.run_agent(
            agent_id=AGENT_ID,
            message="Remember that this request belongs to our SDK tutorial.",
            session_id=session_id,
            user_id=USER_ID,
        )
        print(f"Created run: {run.run_id}")

        sessions = await client.get_sessions(
            session_type=SessionType.AGENT,
            component_id=AGENT_ID,
            user_id=USER_ID,
        )
        print(f"Sessions for user: {len(sessions.data)}")

        details = await client.get_session(
            session_id=session_id,
            session_type=SessionType.AGENT,
            user_id=USER_ID,
        )
        print(f"Session name: {details.session_name}")

        runs = await client.get_session_runs(
            session_id=session_id,
            session_type=SessionType.AGENT,
            user_id=USER_ID,
        )
        print(f"Runs in session: {len(runs)}")

        renamed = await client.rename_session(
            session_id=session_id,
            session_name="Renamed Python client session",
            session_type=SessionType.AGENT,
            user_id=USER_ID,
        )
        print(f"Renamed session: {renamed.session_name}")

        memory = await client.create_memory(
            memory="The user prefers concise SDK examples.",
            user_id=USER_ID,
            topics=["preferences", "sdk"],
        )
        memory_id = memory.memory_id
        print(f"Created memory: {memory_id}")

        memories = await client.list_memories(user_id=USER_ID)
        print(f"Memories for user: {len(memories.data)}")

        retrieved = await client.get_memory(memory_id, user_id=USER_ID)
        print(f"Retrieved memory: {retrieved.memory}")

        updated = await client.update_memory(
            memory_id=memory_id,
            memory="The user prefers concise, typed SDK examples.",
            user_id=USER_ID,
            topics=["preferences", "sdk", "typing"],
        )
        print(f"Updated memory: {updated.memory}")
    finally:
        if memory_id is not None:
            await client.delete_memory(memory_id, user_id=USER_ID)
            print(f"Deleted memory: {memory_id}")
        if session_id is not None:
            await client.delete_session(session_id, user_id=USER_ID)
            print(f"Deleted session: {session_id}")


# ---------------------------------------------------------------------------
# Run the Example
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    asyncio.run(manage_session_and_memory())
```

## Run the Example

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

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U "agno[os]" chromadb openai
    ```
  </Step>

  <Step title="Export your API keys">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Clone Agno">
    Clone the pinned Agno source and run the remaining commands from its root:

    ```bash theme={null}
    git clone https://github.com/agno-agi/agno.git
    cd agno
    git checkout v3.0.4
    ```
  </Step>

  <Step title="Start the AgentOS server">
    Open another terminal in the parent directory where `.venv` and the cloned `agno` directory are siblings, then start the shared server on port 7778:

    <CodeGroup>
      ```bash Mac/Linux theme={null}
      source .venv/bin/activate
      export OPENAI_API_KEY="your_openai_api_key_here"
      cd agno
      python cookbook/05_agent_os/03_python_client/_server.py
      ```

      ```powershell Windows theme={null}
      .venv\Scripts\activate
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      Set-Location agno
      python cookbook/05_agent_os/03_python_client/_server.py
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the example">
    Run the example from the repository root:

    ```bash theme={null}
    python cookbook/05_agent_os/03_python_client/03_sessions_and_memory.py
    ```
  </Step>
</Steps>

Full source: [cookbook/05\_agent\_os/03\_python\_client/03\_sessions\_and\_memory.py](https://github.com/agno-agi/agno/blob/v3.0.4/cookbook/05_agent_os/03_python_client/03_sessions_and_memory.py)
