# Resources and billing

> Size a sandbox, set its timeouts, and read what it is billed and what it measured, while it runs and afterwards.

Source: https://docs.compute.dev.ddcdragon.com/guides/resources-and-billing/

A sandbox reserves CPU and memory, and its bucket pays for that reservation for as long as the sandbox runs. Size it
to what your work uses, end it when the work is done, and read its usage to check. For how billing works, see
[Resources and billing](https://docs.compute.dev.ddcdragon.com/concepts/resources-and-billing/) in Concepts.

:::caution[Devnet is moving to requests, limits and burst billing]
Some of devnet's nodes already run it. A sandbox on one of them can use more CPU and memory than it reserved,
up to its limit (`cpu_limit` and `memory_limit_mib` in its `resources`), and is billed, minute by minute, the larger
of what it reserved and what it measured: its usage records say `billing_basis` `request_or_usage`, and the
CLI says "the larger of reserved and measured per minute". This page describes the `reserved`
basis, which the other nodes still bill.
:::

## Size a sandbox

`--cpu` (vCPUs), `--memory` (MiB) and `--pids` set what the sandbox reserves; in Python, `cpu=`, `memory=` and
`pids=`.

**CLI**

```sh
ID=$(cere sandbox create --image busybox:1.36 --cpu 0.25 --memory 256 --pids 256 --timeout 300 -q)
cere sandbox get $ID | grep -E '^(resources|timeout)'
```

```
resources  cpu 0.25, memory 256 MiB, pids 256
timeout    300s (idle 0s)
```

**Python**

```python
import cere

sb = cere.Sandbox.create(image="busybox:1.36", cpu=0.25, memory=256, pids=256, timeout=300)
print(sb.data["resources"])
```

```
{'cpu': 0.25, 'memory_mib': 256, 'pids': 256, 'disk_mib': 0, 'gpu': None}
```

The reservation is also the ceiling: the CPU quota equals `--cpu`, and a sandbox that uses more memory than
`--memory` ends with `oom_killed`.

| Resource | Default | Allowed |
|---|---|---|
| `--cpu` | 1 | more than 0, up to 4 |
| `--memory` | 1024 MiB | 128 to 8192 MiB |
| `--pids` | 2048 | 1 to 32768 |
| `--timeout` | 600 s | 1 s to 24 hours |
| Sandboxes per bucket | | 20 starting, running or unknown at a time; one more is refused with `409 limit_active_sandboxes` |

A request outside these is refused before anything starts:

```sh
cere sandbox create --image busybox:1.36 --cpu 8 --timeout 60
cere sandbox create --image busybox:1.36 --memory 64 --timeout 60
cere sandbox create --image busybox:1.36 --timeout 48h
```

```
error: 400 invalid_request: resources.cpu must be in (0, 4]
error: 400 invalid_request: resources.memory_mib must be in [128, 8192]
error: 400 invalid_request: timeout_s must be in [1, 86400]
```

A sandbox that needs more memory than it reserved ends with `oom_killed`; the command that was running gets exit
code 128:

**CLI**

```sh
M=$(cere sandbox create --image python:3.12-slim --memory 128 --timeout 120 -q)
cere sandbox exec $M -- python -c "b = bytearray(512 * 1024 * 1024)"; echo "exit code: $?"
cere sandbox get $M | grep -E '^(status|exit)'
```

```
waiting on pid 2: waiting on PID 2 in sandbox "3e3aedf2...": urpc method "containerManager.WaitPID" failed: EOF
exit code: 128
status     terminated
exit       oom_killed (exit code 137)
```

**Python**

```python
m = cere.Sandbox.create(image="python:3.12-slim", memory=128, timeout=120)
p = m.exec("python", "-c", "b = bytearray(512 * 1024 * 1024)")
print(p.wait())
print(m.refresh().status, m.exit_reason)
```

```
128
terminated oom_killed
```

## Set timeouts

Every sandbox ends at its `--timeout` (`timeout=`, seconds; the CLI also takes a duration such as `5m`).
`--idle-timeout` (`idle_timeout=`) ends it earlier when nothing has called exec or a file for that long. Idle
timeouts are off by default.

**CLI**

```sh
T=$(cere sandbox create --image busybox:1.36 --cpu 0.25 --memory 256 --timeout 30 -q)
I=$(cere sandbox create --image busybox:1.36 --cpu 0.25 --memory 256 --timeout 5m --idle-timeout 20s -q)
sleep 50
cere sandbox get $T | grep -E '^(status|exit)'
cere sandbox get $I | grep -E '^(status|exit)'
```

```
status     terminated
exit       timeout (30s) (exit code 137)
status     terminated
exit       idle_timeout (20s) (exit code 137)
```

**Python**

```python
t = cere.Sandbox.create(image="busybox:1.36", cpu=0.25, memory=256, timeout=30)
i = cere.Sandbox.create(image="busybox:1.36", cpu=0.25, memory=256, timeout=300, idle_timeout=20)
print(t.wait(), t.exit_reason)
print(i.wait(), i.exit_reason)
```

```
137 timeout (30s)
137 idle_timeout (20s)
```

A sandbox also ends 60 seconds before the login it was created with expires, with the exit reason `token_expiry`,
whichever comes first; a create with 60 seconds or less left on the login is refused. `cere whoami` shows when your
login expires:

```sh
cere whoami | grep expires
```

```
expires     2026-12-24T21:57:01Z (in 29d05h)
```

For a sandbox that must outlive your login, sign in again first with a longer lifetime
(`cere login --days 30`, see [Signing in](https://docs.compute.dev.ddcdragon.com/guides/signing-in/)).

## Read what a running sandbox is billed

`cere sandbox usage` shows what the sandbox is billed so far next to what it measured; in Python, `sb.usage()`.

**CLI**

```sh
cere sandbox exec $ID -- sh -c 'yes > /dev/null & sleep 10; kill $!'
cere sandbox usage $ID
```

```
billed cpu     0.25 vCPU × 11.6 s = 2.9 CPU-s (measured 2.6)
billed memory  0.25 GiB × 11.6 s = 2.9 GiB-s (measured 0.1)
wall           11.6 s
cpu            2.585 CPU-s
memory         peak 24 MiB, 0.119 GiB-s
network        out 0 B, in 806 B
disk           peak 0.0 MiB
gpu            0.0 s
```

**Python**

```python
sb.exec("sh", "-c", "yes > /dev/null & sleep 10; kill $!").wait()
u = sb.usage()
print(u.billing())
print(u)
```

```
0.25 vCPU × 11.5 s = 2.9 CPU-s (measured 2.6), 0.25 GiB × 11.5 s = 2.9 GiB-s (measured 0.1)
billed 0.25 vCPU × 11.5 s = 2.9 CPU-s (measured 2.6), 0.25 GiB × 11.5 s = 2.9 GiB-s (measured 0.1); measured wall 11.5s, cpu 2.58s, memory peak 25 MiB, 0.12 GiB-s, net out 0 B in 806 B, disk peak 0.0 MiB
```

`Usage` has the numbers as fields: `billed_cpu_s`, `billed_ram_gib_s`, `cpu_s`, `ram_gib_s`, `memory_peak_mib`,
`wall_s`, `net_egress_bytes`, `net_ingress_bytes`, `disk_peak_mib`, `billing_basis`.

- **Billed** is the reservation over the time the sandbox has run: vCPU × seconds and GiB × seconds, or the
  measured amount where that is more. It starts when the container starts (the image pull is free) and stops when
  gVisor stops.
- **Measured** is what the node measured: CPU-seconds, GiB-seconds of memory in use, the memory peak, network
  bytes and disk.

`cere sandbox get` shows the same `billed` and `usage` lines, and after `terminate` they are final.

## Read usage records

Each node writes a signed usage record for every sandbox every minute, plus a final one when it ends. `cere usage`
lists them, newest first; in Python, `cere.usage_records()`.

**CLI**

```sh
cere sandbox terminate $ID
cere usage --sandbox $ID
```

```
sb-01m89qrv95xamvxbnwnhgpewwj terminated: terminated by API
billed 0.25 vCPU × 16.8 s = 4.2 CPU-s (measured 2.6), 0.25 GiB × 16.8 s = 4.2 GiB-s (measured 0.2)
usage: wall 16.8s, cpu 2.59s, memory peak 24 MiB, 0.17 GiB-s, net out 0 B in 918 B, disk peak 0.0 MiB
PERIOD END            SANDBOX                        FINAL  BASIS     RESERVED             BILLED CPU-S  MEASURED CPU-S  BILLED GIB-S  MEASURED GIB-S  MEM PEAK  NET OUT  CPU UNITS     RAM UNITS     TCA       DAC     EXIT
2026-11-25T16:14:28Z  sb-01m89qrv95xamvxbnwnhgpewwj  final  reserved  0.25 vCPU, 0.25 GiB  4.203         2.586           4.203         0.172           24 MiB    0 B      420281200000  420281225000  29927054  pushed  terminated by API

1 records: billed 4.203 CPU-s, 4.203 GiB-s (measured 2.586 CPU-s, 0.172 GiB-s)
```

`--since` takes a duration back (`1h`) or an RFC 3339 time. A listing stops at `--limit` (default 100) and says
so; `--all` lists and sums every record:

```sh
cere usage --since 1h --limit 5
cere usage --since 1h --all | tail -1
```

```
PERIOD END            SANDBOX                        FINAL  BASIS     RESERVED             BILLED CPU-S  MEASURED CPU-S  BILLED GIB-S  MEASURED GIB-S  ...  DAC     EXIT
2026-11-25T16:15:00Z  sb-01m89qryxz9x2d2dcghj6nqgd4         reserved  1.0 vCPU, 1.0 GiB    44.805        5.043           44.805        10.109          ...  pushed  -
2026-11-25T16:15:00Z  sb-01m89qnh5erh2xb8t1mb2n8wmj         reserved  1.0 vCPU, 1.0 GiB    60.000        2.494           60.000        14.029          ...  pushed  -
2026-11-25T16:14:57Z  sb-01m89qsamtae70382y3ngmvj9j  final  reserved  0.25 vCPU, 0.25 GiB  7.525         0.117           7.525         0.421           ...  pushed  exited with code 0
2026-11-25T16:14:55Z  sb-01m89qs9ejmckx7fstyvgstrvs  final  reserved  1.0 vCPU, 1.0 GiB    30.036        0.147           30.036        0.421           ...  pushed  exited with code 0
2026-11-25T16:14:54Z  sb-01m89qs75t3x0apy23h1t6w9pj  final  reserved  0.25 vCPU, 0.25 GiB  7.732         0.095           7.732         0.288           ...  pushed  timeout (30s)

the newest 5 records: billed 150.098 CPU-s, 150.098 GiB-s (measured 7.896 CPU-s, 25.268 GiB-s); there are more (--all sums every record)
41 records: billed 596.290 CPU-s, 596.290 GiB-s (measured 58.284 CPU-s, 70.776 GiB-s)
```

**Python**

```python
sb.terminate()
print(sb.billing())

recs = cere.usage_records(sandbox_id=sb.object_id)
for r in recs:
    print(r["period_end"], r["final"], r["billing_basis"], r["billed_cpu_s"], r["cpu_s"],
          r["billed_ram_gib_s"], r["ram_gib_s"], r["dac_status"])
print("billed", round(sum(r["billed_cpu_s"] for r in recs), 3), "CPU-s; measured",
      round(sum(r["cpu_s"] for r in recs), 3), "CPU-s")
```

```
0.25 vCPU × 14.4 s = 3.6 CPU-s (measured 2.6), 0.25 GiB × 14.4 s = 3.6 GiB-s (measured 0.2)
2026-11-25T16:15:57.507043Z True reserved 3.606555 2.580469 3.606555 0.151229 pushed
billed 3.607 CPU-s; measured 2.58 CPU-s
```

`since=` takes a `datetime` (or an RFC 3339 string) and `limit=None` reads every record:

```python
from datetime import datetime, timedelta, timezone

hour = cere.usage_records(since=datetime.now(timezone.utc) - timedelta(hours=1), limit=None)
print(len(hour), "records,", round(sum(r["billed_cpu_s"] for r in hour), 1), "CPU-s billed")
```

```
45 records, 655.1 CPU-s billed
```

Each record has the period it covers, whether it is the final one, the billing basis (`reserved`), the
reservation, the billed and measured CPU-seconds and GiB-seconds, and whether the node has delivered it. Every
field is explained in [Usage](https://docs.compute.dev.ddcdragon.com/concepts/usage/#records).

There is no price list yet: records count CPU-seconds and GiB-seconds, and nothing is converted to money.

## Size to the measured column

The measured column says what the work needed. Run the same work at the default size and at a quarter of it, and
compare:

```sh
BIG=$(cere sandbox create --image python:3.12-slim --timeout 300 -q -- python -c "import time; time.sleep(30)")
SMALL=$(cere sandbox create --image python:3.12-slim --cpu 0.25 --memory 256 --timeout 300 -q -- python -c "import time; time.sleep(30)")
sleep 50
cere sandbox get $BIG | grep -E '^(resources|exit|billed|usage)'
cere sandbox get $SMALL | grep -E '^(resources|exit|billed|usage)'
```

```
resources  cpu 1, memory 1024 MiB, pids 2048
exit       exited with code 0 (exit code 0)
billed     1.0 vCPU × 30.0 s = 30.0 CPU-s (measured 0.1), 1.0 GiB × 30.0 s = 30.0 GiB-s (measured 0.4)
usage      wall 30.0s, cpu 0.15s, memory peak 24 MiB, 0.42 GiB-s, net out 0 B in 726 B, disk peak 0.0 MiB
resources  cpu 0.25, memory 256 MiB, pids 2048
exit       exited with code 0 (exit code 0)
billed     0.25 vCPU × 30.1 s = 7.5 CPU-s (measured 0.1), 0.25 GiB × 30.1 s = 7.5 GiB-s (measured 0.4)
usage      wall 30.1s, cpu 0.12s, memory peak 15 MiB, 0.42 GiB-s, net out 210 B in 656 B, disk peak 0.0 MiB
```

The work measured the same in both; the smaller sandbox is billed a quarter as much. Size `--cpu` and `--memory`
a little above the measured CPU and memory peak, end sandboxes when the work is done, and set `--idle-timeout` on
sandboxes that wait for calls.

## When the bucket cannot pay

Every call to the API checks that the bucket's owner has a positive balance. Without one, the API refuses the call,
create included, with `403`:

```
error: 403 FORBIDDEN: bucket owner has insufficient balance
```

In Python the same refusal is a `cere.CereError` with `status` 403. Top up the bucket owner's balance and call again.

## Related

- [Next: Egress and secrets](https://docs.compute.dev.ddcdragon.com/guides/egress-and-secrets/)
- [Resources and billing](https://docs.compute.dev.ddcdragon.com/concepts/resources-and-billing/): what is reserved, billed and measured
- [Usage](https://docs.compute.dev.ddcdragon.com/concepts/usage/): records per minute, billed vs measured
- [CLI](https://docs.compute.dev.ddcdragon.com/reference/cli/): `cere sandbox usage` and `cere usage`
