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

# Access Memories in Memory Completed Event

> Stream an agent run with stream_events=True and read user memories off the MemoryUpdateCompleted event, backed by PostgresDb.

```python access_memories_in_memory_completed_event.py theme={null}
"""
Test script to verify memory events are working correctly.
Steps:
1. Run: `./cookbook/scripts/run_pgvector.sh` to start a postgres container with pgvector
2. Run: `pip install openai sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/11_models/openai/chat/access_memories_in_memory_completed_event.py`
"""

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.openai import OpenAIChat
from agno.run.agent import RunEvent

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

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

agent = Agent(
    model=OpenAIChat(id="gpt-5-mini"),
    user_id="test_user",
    session_id="test_session",
    db=db,
    enable_user_memories=True,
    enable_session_summaries=True,
)


def run_with_events(message: str):
    print(f"--- Query: {message} ---")

    stream = agent.run(message, stream=True, stream_events=True)

    for chunk in stream:
        if chunk.event == RunEvent.run_started.value:
            print(f"[RunStarted] model={chunk.model}")

        elif chunk.event == RunEvent.run_completed.value:
            print("[RunCompleted]")

        elif chunk.event == RunEvent.model_request_started.value:
            print(f"[ModelRequestStarted] model={chunk.model}")

        elif chunk.event == RunEvent.model_request_completed.value:
            print(
                f"[ModelRequestCompleted] tokens: in={chunk.input_tokens}, out={chunk.output_tokens}"
            )

        elif chunk.event == RunEvent.memory_update_started.value:
            print("[MemoryUpdateStarted]")

        elif chunk.event == RunEvent.memory_update_completed.value:
            print("[MemoryUpdateCompleted]")
            if chunk.memories:
                print(f"  Memories ({len(chunk.memories)}):")
                for mem in chunk.memories:
                    print(f"    - {mem.memory}")
            else:
                print("  No memories returned")

        elif chunk.event == RunEvent.session_summary_started.value:
            print("[SessionSummaryStarted]")

        elif chunk.event == RunEvent.session_summary_completed.value:
            print("[SessionSummaryCompleted]")
            if hasattr(chunk, "session_summary") and chunk.session_summary:
                print(f"  Summary: {chunk.session_summary.summary}")

        elif chunk.event == RunEvent.run_content_completed.value:
            print("[RunContentCompleted]")


# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    run_with_events("My name is John Billings")
    run_with_events("I live in NYC")
    run_with_events("What is my name?")

    print("--- Final Memories ---")
    memories = agent.get_user_memories(user_id="test_user")
    if memories:
        for mem in memories:
            print(f"  - {mem.memory}")
    else:
        print("  No memories found")
```

## Run the Example

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

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

  <Step title="Export your OpenAI API key">
    <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>

  <Snippet file="run-pgvector-step.mdx" />

  <Step title="Run the example">
    Save the code above as `access_memories_in_memory_completed_event.py`, then run:

    ```bash theme={null}
    python access_memories_in_memory_completed_event.py
    ```
  </Step>
</Steps>

Full source: [cookbook/90\_models/openai/chat/access\_memories\_in\_memory\_completed\_event.py](https://github.com/agno-agi/agno/blob/main/cookbook/90_models/openai/chat/access_memories_in_memory_completed_event.py)
