Attach a GPU to a sandbox
Lease a GPU for a sandbox only while a command needs it, with a spend cap and a time cap, then release it. The sandbox keeps its files, environment and agent context; the lease runs your GPU commands on a GPU cloud provider and returns their output and cost.
This works the same for Docker and Firecracker sandboxes, because the GPU is a separate worker, not a device added to the sandbox’s host.
Run one command on a GPU
tangle sandbox gpu run my-project \
--accelerator-kind nvidia-3090 \
--accelerator-memory 24 \
--max-spend-usd 1 \
--max-lifetime 900 \
--idle-timeout 120 \
-- python eval.pygpu run attaches a lease, runs the command, and destroys the lease even when the command fails. It exits with the command’s exit code. Put -- before the command so its flags are not read as tangle flags. --accelerator-memory is the minimum memory per GPU in decimal GB: 24 for a 24 GB card.
Detach in a finally block. Deleting the sandbox is a separate step.
Find a GPU you can lease
const { usable, statuses } = await box.gpu.providers({ accelerator: { kind: "nvidia-h100" } });
console.log(usable);
for (const s of statuses) console.log(s);tangle sandbox gpu providers my-project --accelerator-kind nvidia-h100usable lists the providers a lease can be placed on now; an empty list means no lease can attach. statuses covers every supported provider, with whether it is configured and, when you pass an accelerator, a current quote. Pick accelerator kinds from this answer rather than from a fixed list.
accelerator is { kind, count?, vramGb? }. Omit provider and the platform picks the cheapest eligible offer; the lease’s providerSelection shows the candidates it compared and why others were rejected. Name a provider when you need that vendor:
tangle sandbox gpu run my-project --provider runpod --accelerator-kind nvidia-4090 \
--max-spend-usd 1 --max-lifetime 900 -- python eval.pyWhen no provider can take the lease, the attach fails with code GPU_PROVIDER_NOT_CONFIGURED:
import { SandboxError } from "@tangle-network/sandbox";
try {
await box.gpu.attach({ accelerator: { kind: "nvidia-h100" }, maxSpendUsd: 2, maxLifetimeSeconds: 600 });
} catch (error) {
if (error instanceof SandboxError && error.code === "GPU_PROVIDER_NOT_CONFIGURED") {
console.log("No GPU provider can take this lease right now");
} else {
throw error;
}
}Keep a lease across several commands
tangle sandbox gpu attach my-project \
--accelerator-kind nvidia-3090 --accelerator-memory 24 \
--max-spend-usd 1 --max-lifetime 900 --idle-timeout 120
tangle sandbox gpu list my-project
tangle sandbox gpu exec my-project <lease-id> --cwd /home/agent -- python train.py
tangle sandbox gpu exec my-project <lease-id> -- python eval.py
tangle sandbox gpu detach my-project <lease-id>const lease = await box.gpu.attach({ accelerator: { kind: "nvidia-3090", vramGb: 24 }, maxSpendUsd: 2, maxLifetimeSeconds: 1800 });
await box.gpu.exec(lease.id, { command: "python train.py", cwd: "/home/agent", env: { EPOCHS: "3" } });
await box.gpu.renew(lease.id, { maxSpendUsd: 4, maxLifetimeSeconds: 3600 });
await box.gpu.exec(lease.id, { command: "python eval.py" });
console.log(await box.gpu.list());
await box.gpu.detach(lease.id);renew raises the spend cap or the lifetime of a live lease. exec returns { lease, result: { exitCode, stdout, stderr, durationMs } }.
Request a GPU at create
A sandbox can start with a lease. Set resources.accelerator and the lease caps:
const box = await client.create({
resources: { accelerator: { kind: "nvidia-3090", vramGb: 24 } },
gpuLease: { maxSpendUsd: 1, maxLifetimeSeconds: 900, idleTimeoutSeconds: 120 },
});tangle sandbox create --accelerator-kind nvidia-3090 --accelerator-memory 24 \
--gpu-max-spend-usd 1 --gpu-max-lifetime 900 --gpu-idle-timeout 120What you pay
Every lease needs maxSpendUsd and maxLifetimeSeconds; the lease ends when it reaches either. idleTimeoutSeconds ends a lease that has not run a command for that long, so an agent that forgets to detach does not keep paying.
A lease is billed by the second. A closed lease reports:
| Field | Meaning |
|---|---|
billing.seconds | Seconds you are charged for. |
billing.customerPricePerHourUsd | The hourly price of this lease. |
billing.customerCostUsd | What this lease cost you. |
billing.startedAt, billing.stoppedAt | The billed interval. |
client.usage() adds GPU seconds and GPU cost to your account totals. tangle sandbox gpu detach prints the billed seconds and cost once billing is final. Prices are on Pricing.
Fleets with GPUs
Fleet workers can each request the same accelerator and caps, for parallel evals where each worker needs its own short GPU burst:
tangle fleet create --count 4 \
--accelerator-kind nvidia-3090 --accelerator-memory 24 \
--gpu-max-spend-usd 1 --gpu-max-lifetime 900 --gpu-idle-timeout 120Caps apply per worker: four workers with --gpu-max-spend-usd 1 can spend up to four dollars together. See Run fleets of sandboxes.
Python and REST
Python has no GPU lease methods. Call the routes with client.fetch, which returns the raw response and does not raise on an error status:
lease = client.fetch(
"POST",
f"/v1/sandboxes/{box.id}/gpu/leases",
json={"accelerator": {"kind": "nvidia-3090", "vramGb": 24}, "maxSpendUsd": 1, "maxLifetimeSeconds": 900},
).json()
lease_id = lease["lease"]["id"]
try:
out = client.fetch("POST", f"/v1/sandboxes/{box.id}/gpu/leases/{lease_id}/exec", json={"command": "nvidia-smi"})
print(out.json())
finally:
client.fetch("DELETE", f"/v1/sandboxes/{box.id}/gpu/leases/{lease_id}")GET /v1/sandboxes/{id}/gpu/providers lists providers, and PATCH /v1/sandboxes/{id}/gpu/leases/{leaseId} renews.