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

# Basic Memory Example

> Complete example of using memory in a customer support agent

## Scenario

A customer support agent that:

* Remembers conversation context within a session
* Learns about customers across sessions
* Provides personalized support based on user profile

## Complete Implementation

```python theme={null}
from upsonic import Agent, Task
from upsonic.storage.memory import Memory
from upsonic.storage.sqlite import SqliteStorage

# 1. Create persistent storage
storage = SqliteStorage(db_file="./support.db")

# 2. Configure memory with all features
memory = Memory(
    storage=storage,
    session_id="support_001",
    user_id="customer_123",
    full_session_memory=True,      # Remember conversation
    summary_memory=True,           # Generate summaries
    user_analysis_memory=True,     # Learn about customer
    num_last_messages=10,          # Keep last 10 turns
    model="anthropic/claude-sonnet-4-5"
)

# 3. Create support agent
agent = Agent(
    model="anthropic/claude-sonnet-4-5",
    name="Support Agent",
    memory=memory
)

# 4. First interaction
result1 = agent.do(Task("I'm having trouble logging in to my account"))
print("Response 1:", result1)

# 5. Follow-up - agent remembers context
result2 = agent.do(Task("I tried resetting my password but didn't get the email"))
print("Response 2:", result2)

# 6. Agent references previous context
result3 = agent.do(Task("What have I told you about my issue?"))
print("Response 3:", result3)
```

## Cross-Session Memory

Same customer returns in a new session:

```python theme={null}
from upsonic import Agent, Task
from upsonic.storage.memory import Memory
from upsonic.storage.sqlite import SqliteStorage

storage = SqliteStorage(db_file="./support.db")

# New session, same customer
memory = Memory(
    storage=storage,
    session_id="support_002",       # NEW session
    user_id="customer_123",          # SAME customer
    full_session_memory=True,
    user_analysis_memory=True,
    model="anthropic/claude-sonnet-4-5"
)

agent = Agent("anthropic/claude-sonnet-4-5", memory=memory)

# Agent knows customer from previous sessions via user profile
result = agent.do(Task("Hello, I'm back. What did we discuss before?"))
print(result)
```

## Token-Efficient Mode

Save everything but only inject summaries and user profiles to reduce token usage:

```python theme={null}
from upsonic import Agent, Task
from upsonic.storage.memory import Memory
from upsonic.storage.sqlite import SqliteStorage

storage = SqliteStorage(db_file="./efficient_support.db")

memory = Memory(
    storage=storage,
    session_id="support_003",
    user_id="customer_123",
    full_session_memory=True,          # Save raw history
    summary_memory=True,               # Save summaries
    user_analysis_memory=True,         # Save user profiles
    load_full_session_memory=False,    # Don't inject raw history
    load_summary_memory=True,          # Inject summary only
    load_user_analysis_memory=True,    # Inject user profile
    model="anthropic/claude-sonnet-4-5"
)

agent = Agent("anthropic/claude-sonnet-4-5", memory=memory)

result1 = agent.do(Task("I need help with billing"))
result2 = agent.do(Task("The charge was for $49.99 on March 15"))
result3 = agent.do(Task("What was the amount I mentioned?"))
print(result3)  # Recalls via summary, full history saved in storage
```

## Async Usage

```python theme={null}
import asyncio
from upsonic import Agent, Task
from upsonic.storage.memory import Memory
from upsonic.storage.sqlite import SqliteStorage

async def main():
    storage = SqliteStorage(db_file="./async_support.db")

    memory = Memory(
        storage=storage,
        session_id="async_001",
        user_id="user_456",
        full_session_memory=True,
        summary_memory=True,
        model="anthropic/claude-sonnet-4-5"
    )

    agent = Agent("anthropic/claude-sonnet-4-5", memory=memory)

    result1 = await agent.do_async(Task("My order hasn't arrived"))
    result2 = await agent.do_async(Task("It's been 5 days"))

    print("Final response:", result2)

asyncio.run(main())
```

## What Gets Persisted

### Session Table

| Field        | Content                   |
| ------------ | ------------------------- |
| `session_id` | `"support_001"`           |
| `user_id`    | `"customer_123"`          |
| `messages`   | Full conversation history |
| `summary`    | Generated session summary |
| `runs`       | Individual run outputs    |

### User Memory Table

| Field         | Content                                              |
| ------------- | ---------------------------------------------------- |
| `user_id`     | `"customer_123"`                                     |
| `user_memory` | Extracted traits (login issues, communication style) |

## Key Takeaways

1. **Same storage, different sessions** - User profile persists across sessions
2. **Memory is automatic** - Just attach to agent, no manual saving needed
3. **Summary + History** - Use both for best context/cost balance
4. **Save/Load separation** - Save everything, inject only what's needed
5. **Sync and async** - Both `do()` and `do_async()` work with memory
