# Resources and billing

> What a sandbox reserves, what your bucket is billed for it and when, and what the node measures.

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

A sandbox reserves CPU and memory on its node for as long as it runs, and your bucket is billed for that
reservation. The node also measures what the sandbox uses, so you can size the next one to fit.

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

## What you request

| Resource | CLI flag | SDK argument | Default | Range |
| --- | --- | --- | --- | --- |
| CPU, in vCPU | `--cpu` | `cpu=` | 1 | more than 0, at most 4 (fractions such as 0.25 work) |
| Memory, in MiB | `--memory` | `memory=` | 1024 | 128 to 8192 |
| Processes | `--pids` | `pids=` | 2048 | 1 to 32768 |
| Timeout, in seconds | `--timeout` | `timeout=` | 600 | 1 to 86400 |
| Idle timeout, in seconds | `--idle-timeout` | `idle_timeout=` | off | up to the timeout |

The request is also the sandbox's limit: its CPU is capped at the vCPU it reserved, and a sandbox that needs more
memory than it reserved ends with `oom_killed`. The sandbox may write up to 2048 MiB of disk; disk is not billed.

## What is billed

Your bucket pays for what a sandbox reserves, from the moment its container starts until gVisor stops, however it
ends. Each [usage record](https://docs.compute.dev.ddcdragon.com/concepts/usage/) bills, for its stretch of time:

- **CPU:** vCPU reserved × seconds, or the CPU-seconds measured if those are more;
- **Memory:** GiB reserved × seconds, or the GiB-seconds measured if those are more.

Because the sandbox cannot use more than it reserved, the bill is the reservation almost always. The measured
amount can be slightly larger in a busy sandbox's first minute, which includes gVisor's start; that minute is then
billed as measured.

For example, a sandbox with 1 vCPU and 1024 MiB that runs for 90 seconds is billed 90 CPU-seconds and 90
GiB-seconds, however little it does.

| Not billed | |
| --- | --- |
| Pulling the image | Billing starts when the container starts. |
| A sandbox that never started | Status `failed`: nothing is billed. |
| After the stop | Logs and retained files are free. |
| Disk and network | Measured and reported, not billed. |

The bill goes to the bucket of the login the sandbox was created with. Model calls through the inference API are
billed to the bucket of the key they present, separately from sandboxes.

There is no price list for sandboxes yet: their usage is billed in CPU-seconds and GiB-seconds and not converted to
money.

## Keeping the bill down

- Ask for what the sandbox uses. Fractional CPU (`--cpu 0.25`) and small memory (`--memory 256`) bill less for the
  same time. The measured amounts in its usage show what it used.
- Set a short `--timeout`, and terminate a sandbox when you are done with it.
- Set `--idle-timeout` on a sandbox that waits for commands, so it ends when nobody uses it.

## When the bucket has no balance

Every API request checks that the bucket's owner has a balance; with none, the API refuses requests, creating a
sandbox included, with `403` ([what the refusal looks like](https://docs.compute.dev.ddcdragon.com/guides/resources-and-billing/#when-the-bucket-cannot-pay)).

## Related

- [Next: Egress and credentials](https://docs.compute.dev.ddcdragon.com/concepts/egress-and-credentials/)
- [Usage](https://docs.compute.dev.ddcdragon.com/concepts/usage/)
- [Guide: Resources and billing](https://docs.compute.dev.ddcdragon.com/guides/resources-and-billing/)
- [Sandboxes](https://docs.compute.dev.ddcdragon.com/concepts/sandboxes/)
