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

# State store

> Keep a small key-value store per account that lives beside your sandboxes, not inside them.

It is readable while a sandbox is paused, after it is killed, and by the next sandbox that
takes the same name. Use it for an agent's memory, a task queue cursor, checkpoints of a long job or anything a sandbox
should find again after it was recreated.

## Namespaces and keys

State lives in namespaces of your account. A namespace is 1 to 128 letters, digits, `.`, `_` or `-`; a key is 1 to 512
bytes of UTF-8 (slashes allowed); a value is up to 1 MiB of bytes. Every key has a version that starts at 1 and grows by
one with every write.

| Call | Does |
| - | - |
| `PUT /state/{ns}/{key}` | Writes the body as the value. Answers `{"version", "durability"}` and `ETag: "<version>"`. |
| `GET /state/{ns}/{key}` | The value as raw bytes, with `ETag` and `styne-updated-ms`. `404` when there is none. |
| `DELETE /state/{ns}/{key}` | Removes the key: `204`, or `404` when there was none. |
| `GET /state/{ns}` | Keys of the namespace: `prefix`, `cursor`, `limit` (1 to 1000, default 100), `values=true` adds each value (`value` as text, or `valueBase64`). |
| `DELETE /state/{ns}` | Removes the whole namespace. |
| `GET /state` | Your usage (`keys`, `bytes`, per namespace) and your ceilings. |
| `/sandboxes/{id}/state[/{key}]` | The same calls on the sandbox's own namespace: its [name](https://docs.styne.ai/calls.md#name-a-sandbox) when it has one, else its id. |

```bash theme={"theme":"css-variables"}
curl -sS -X PUT https://api.styne.ai/state/agent-7/memory/plan \
  -H "Authorization: Bearer $STYNE_API_KEY" --data-binary '{"step": 3}'

curl -sS https://api.styne.ai/state/agent-7/memory/plan -H "Authorization: Bearer $STYNE_API_KEY"
```

## Conditional writes

Send `If-Match: "<version>"` on a `PUT` or `DELETE` to change the key only while it is still at that version, or
`If-None-Match: *` on a `PUT` that must create it. A mismatch answers `412 version_mismatch` with `currentVersion`, and
nothing is written. This is how two sandboxes update one key without losing a write: read, change, write with the version
you read, and repeat on `412` (the TypeScript SDK's `state.update()` does exactly this).

## From inside a sandbox

Every sandbox finds `STYNE_SANDBOX_TOKEN`, `STYNE_SANDBOX_ID`, `STYNE_API_URL` and, when it has a name,
`STYNE_SANDBOX_NAME` in its environment. The token opens the sandbox's own namespace (its name, else its id) and nothing
else of your account, so no API key has to enter the sandbox:

```text theme={"theme":"css-variables"}
curl -sS -X PUT "$STYNE_API_URL/sandboxes/$STYNE_SANDBOX_ID/state/progress" \
  -H "Authorization: Bearer $STYNE_SANDBOX_TOKEN" --data-binary 'done: 41 of 100'
```

A token presented for another namespace answers `403 forbidden`.

## Surviving a kill

The namespace of a named sandbox is its name. Kill it, create a new sandbox with the same `metadata["styne.name"]`, and the
new one reads the same state through `/sandboxes/{new id}/state` and its own token. An unnamed sandbox's namespace is its
id: after a kill, read it with an API key at `/state/{old id}`.

## Durability

A write answers once it is applied, and its `durability` field (also the `styne-durability` header) says how safe it is:

| `durability` | Means |
| - | - |
| `disk` | The write is in the store's append-only file on the store's disk (fsync), before the answer. A store that confirms every write with an fsync answers this. |
| `memory` | The store applied it and writes it to its append-only file on disk within about a second; the write is acknowledged before that sync. This is the answer of a store that syncs once a second (the production store) and of `?durable=false`. |
| `unconfirmed` | Applied, but the disk did not confirm in time. Read it back if it matters. |

A write that answered `disk` survives a restart of the store, of the API and of every sandbox. `disk` covers the store's
primary machine: whether a second machine also holds it depends on the region's replication, so treat the state store as
durable storage, not as a backup.

## Limits and billing

| Limit | Default |
| - | - |
| Value size | 1 MiB (`413 value_too_large`) |
| Keys per account | 100,000 |
| Bytes per account (keys plus values) | 1 GiB |

A write past a ceiling answers `507 state_quota_exceeded` with what you hold now; overwriting with a smaller value always
fits. Stored bytes are billed per GiB-month, metered every 15 minutes, to the API key of the account's latest write (for a
sandbox, the key that created it); see [billing](https://docs.styne.ai/billing.md).

## Operations

| Method | Path | What it does |
| - | - | - |
| `GET` | `/state` | The account's state usage and ceilings |
| `GET` | `/state/{ns}` | List the keys of a state namespace |
| `DELETE` | `/state/{ns}` | Delete a state namespace |
| `GET` | `/state/{ns}/{key}` | Read a state value |
| `PUT` | `/state/{ns}/{key}` | Write a state value, optionally at a version |
| `DELETE` | `/state/{ns}/{key}` | Delete a state value |
| `GET` | `/sandboxes/{id}/state` | List the keys of a sandbox's own state |
| `GET` | `/sandboxes/{id}/state/{key}` | Read a value of a sandbox's own state |
| `PUT` | `/sandboxes/{id}/state/{key}` | Write a value of a sandbox's own state |
| `DELETE` | `/sandboxes/{id}/state/{key}` | Delete a value of a sandbox's own state |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.