---
title: "Session Memory"
description: "Persist and restore agent session state across compactions and restarts"
canonical: "https://docs.ainative.studio/docs/zeromemory/session-memory"
last-updated: "2026-10-13T21:42:35.442Z"
---

# Session Memory

Source: https://docs.ainative.studio/docs/zeromemory/session-memory

> Persist and restore agent session state across compactions and restarts

## Session Memory

Session Memory stores structured snapshots of an agent's working state — files modified, commands run, decisions made, and open tasks. Agents can restore full context after a session ends or a compaction event fires.

## Store a Session Snapshot

```bash
curl -X POST https://api.ainative.studio/api/v1/public/memory/v2/session \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "session_id": "session-abc123",
    "project_path": "/Users/dev/myapp",
    "files_modified": ["app/main.py", "app/api/router.py"],
    "key_decisions": [
      "Switched to async endpoints for better throughput",
      "Added Redis caching layer"
    ],
    "open_tasks": ["Write tests for /api/v1/models/available"],
    "event": "pre_compact"
  }'
```

**event values:**
- `pre_compact` — fired before context window compaction
- `session_end` — agent session ending cleanly
- `checkpoint` — manual mid-session save

## Get the Latest Session

```bash
curl "https://api.ainative.studio/api/v1/public/memory/v2/session/latest" \
  -H "x-api-key: $API_KEY"
```

## Get a Specific Session

```bash
curl "https://api.ainative.studio/api/v1/public/memory/v2/session/{session_id}" \
  -H "x-api-key: $API_KEY"
```

## Python SDK

```python
import requests

API_KEY = "your-api-key"
BASE = "https://api.ainative.studio/api/v1/public/memory/v2/session"
headers = {"x-api-key": API_KEY, "Content-Type": "application/json"}

## Save session state before compaction
requests.post(BASE, headers=headers, json={
    "session_id": "my-session-001",
    "project_path": "/Users/dev/project",
    "files_modified": ["src/main.py"],
    "key_decisions": ["Refactored auth middleware to use JWT"],
    "open_tasks": ["Add rate limiting to /api/v1/messages"],
    "event": "pre_compact"
})

## Restore on next session start
latest = requests.get(f"{BASE}/latest", headers=headers).json()
print(f"Restoring session: {latest['session_id']}")
print(f"Open tasks: {latest['session_data']['open_tasks']}")
```

## Request Fields

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `session_id` | `string` | Yes | Unique session identifier |
| `project_path` | `string` | No | Working directory path |
| `files_modified` | `string[]` | No | Files changed this session |
| `key_decisions` | `string[]` | No | Important decisions made |
| `open_tasks` | `string[]` | No | Unfinished tasks at save time |
| `event` | `string` | No | `pre_compact`, `session_end`, `checkpoint` |

## Endpoints

| Method | Path | Description |
|--------|------|-------------|
| POST | `/memory/v2/session` | Store session snapshot |
| GET | `/memory/v2/session/latest` | Get most recent session for user |
| GET | `/memory/v2/session/{session_id}` | Get session by ID |
