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

# Memory

Memory enables agents to remember information from previous interactions, allowing them to maintain context across conversations. Without memory, each agent interaction would be isolated, requiring users to repeat information in every message. With memory, agents can reference past conversations, learn from feedback, and adapt to user preferences over time.

Memory stores conversation history associated with `session_id` and `user_id`. When you make a request with the same identifiers, the agent automatically includes relevant conversation history in the context, enabling it to reference previous messages and maintain continuity.

**Session vs User IDs:**

* **`session_id`** - Isolates conversation threads. Each session maintains separate context.
* **`user_id`** - Groups conversations by user. Useful for maintaining user-specific context across sessions.

<Note>
  You cannot define `session_id` alone. Use `user_id` for long-term memory (persists across sessions) or both `session_id` and `user_id` together for short-term memory (isolated to a specific session).
</Note>

## Supported Memory Backends

Hypertic supports multiple memory backends. Click on a backend to see setup and usage examples:

<Columns cols={3}>
  <Card title="MongoDB" icon="https://www.mongodb.com/favicon.ico" href="/memory/mongodb">
    View guide >
  </Card>

  <Card title="PostgreSQL" icon="https://www.postgresql.org/favicon.ico" href="/memory/postgresql">
    View guide >
  </Card>

  <Card title="Redis Cache" icon="https://redis.io/favicon.ico" href="/memory/redis">
    View guide >
  </Card>
</Columns>

## InMemory

Use `InMemory` for testing and development. Data is stored in RAM and lost when the process ends:

```python theme={null} theme={null}
import os
from dotenv import load_dotenv
from hypertic.memory import InMemory
from hypertic.agents import Agent
from hypertic.models import XAI

load_dotenv()

def main():
    memory = InMemory()
agent = Agent(
        model=XAI(
            model="grok-3",
            max_tokens=4096,
        ),
        memory=memory
    )
    
    user_id = "user_123" 
    session_id = "session_001"
    
    print("Chat with the agent (type 'quit' to exit):\n")
    
    while True:
        user_input = input("You: ").strip()
        if user_input.lower() in ['quit', 'exit', 'q']:
            break
        if not user_input:
            continue
        try:
            response = agent.run(user_input, session_id=session_id, user_id=user_id)
            print(f"Agent: {response.content}\n")
        except Exception as e:
            print(f"Error: {e}\n")


if __name__ == "__main__":
    main()
```

<Note>
  For production applications, use persistent storage like PostgreSQL or MongoDB. See the individual backend pages for setup instructions.
</Note>
