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

# Storage Backends

> Configure filesystem storage with different backends

## Usage

DeepAgent supports three types of storage backends for the virtual filesystem: ephemeral (StateBackend), persistent (MemoryBackend), and hybrid routing (CompositeBackend).

## Backend Types

### StateBackend (Ephemeral)

In-memory storage that persists only during the agent session.

**Use when:**

* Temporary working files
* No persistence needed
* Fast operations required

```python theme={null}
import asyncio
from upsonic.agent.deepagent import DeepAgent
from upsonic.agent.deepagent.backends import StateBackend
from upsonic import Task

async def main():
    backend = StateBackend()
    agent = DeepAgent(
        model="anthropic/claude-sonnet-4-5",
        filesystem_backend=backend
    )
    
    task = Task(description="Create /tmp/scratch.txt with temporary data")
    result = await agent.do_async(task)
    
    # Files exist only during this session
    exists = await backend.exists("/tmp/scratch.txt")
    print(f"File exists: {exists}")

asyncio.run(main())
```

### MemoryBackend (Persistent)

Persistent storage that survives process restarts.

**Use when:**

* Long-term memory needed
* Files must persist across sessions
* Important data storage

```python theme={null}
import asyncio
import os
import tempfile
from upsonic.agent.deepagent import DeepAgent
from upsonic.agent.deepagent.backends import MemoryBackend
from upsonic.storage import SqliteStorage
from upsonic import Task

async def main():
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as tmp:
        db_path = tmp.name
    
    try:
        storage = SqliteStorage(db_file=db_path)
        backend = MemoryBackend(storage)
        
        agent = DeepAgent(
            model="anthropic/claude-sonnet-4-5",
            filesystem_backend=backend
        )
        
        task = Task(description="Research latest AI trends. Save important research findings to /memory/research.txt")
        await agent.do_async(task)
        
        # Files persist across sessions
        content = await backend.read("/memory/research.txt")
        print(f"Persistent content: {content[:50]}...")
        
        storage.close()
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)

asyncio.run(main())
```

### CompositeBackend (Hybrid)

Route different paths to different backends.

**Use when:**

* Mix ephemeral and persistent storage
* Different storage strategies for different paths
* Optimize storage based on file importance

```python theme={null}
import asyncio
from upsonic.agent.deepagent import DeepAgent
from upsonic.agent.deepagent.backends import StateBackend, MemoryBackend, CompositeBackend
from upsonic.storage.providers.sqlite import SqliteStorage
from upsonic import Task
import tempfile

async def main():
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as tmp:
        db_path = tmp.name
    
    try:
        storage = SqliteStorage(db_file=db_path)
        backend = CompositeBackend(
            default=StateBackend(),  # Ephemeral for default paths
            routes={
                "/research/": MemoryBackend(storage),  # Persistent
                "/reports/": MemoryBackend(storage)   # Persistent
            }
        )
        
        agent = DeepAgent(
            model="anthropic/claude-sonnet-4-5",
            filesystem_backend=backend
        )
        
        task = Task(description="""
        Create /tmp/temp.txt (ephemeral)
        Create /research/findings.txt (persistent)
        Create /reports/summary.txt (persistent)
        """)
        
        result = await agent.do_async(task)
        print(result)
        
        await storage.disconnect_async()
    finally:
        import os
        os.unlink(db_path)

asyncio.run(main())
```

## Key Points

* StateBackend: Fast, ephemeral, no persistence
* MemoryBackend: Persistent, survives restarts, requires Storage
* CompositeBackend: Route by path, mix strategies
* Default backend is StateBackend if not specified
