# How do I run a long task in the background?

Submit work as a **job** and disconnect. It runs in its own sandbox, stops at the time and price limit you set, and keeps its result files until you collect them – from anywhere.

## Steps

1. **Submit the job**: the command, a time limit, a price limit (`max_cost`) and `save_artifacts` to keep the files the command writes to `output/`.
2. **Note the job ID.** The call returns at once; you can close your laptop.
3. **Wait or come back later.** Waiting happens on the server – no polling loop.
4. **Download the result files** and look at the log if something went wrong.

**Ask your assistant**

No code: copy this into Claude or ChatGPT with [WBX Sandbox connected](https://sandbox.wbx.cz/docs/connect.md) and change it to fit.

```
Run this as a WBX background job with a price limit of US$0.10 (2.50 Kč) and a time limit of 30 minutes:

mkdir -p output && sleep 30 && echo 42 > output/result.txt

Save the files from output/. Tell me the job ID, and when it has finished give me output/result.txt.
```

**Python**

`pip install wbxsandbox`, your key in `WBXSANDBOX_API_KEY`.

```
from wbxsandbox import run_job, get_job

job = run_job(command="mkdir -p output && sleep 30 && echo 42 > output/result.txt",
              timeout_seconds=1800, save_artifacts=True, max_cost="0.10")   # for CZK use "2.50"
print(job.id)                        # returns at once – you can close your laptop

job = get_job(job.id).wait()         # later, from anywhere
print(job.status, job.summary()["cost"])
job.download("output/result.txt", "result.txt")
```

**TypeScript**

`npm install wbxsandbox` (Node.js 20+); save as `.mts` for top-level `await`.

```
import { writeFile } from "node:fs/promises";
import { WBX } from "wbxsandbox";

const wbx = new WBX();
let job = await wbx.jobs.create({
  command: "mkdir -p output && sleep 30 && echo 42 > output/result.txt",
  timeoutSeconds: 1800, saveArtifacts: true, maxCost: "0.10",   // for CZK use "2.50"
});
console.log(job.id);                 // returns at once

job = await (await wbx.jobs.get(job.id)).wait();   // later, from anywhere
console.log(job.data.status, (await job.summary()).cost);
await writeFile("result.txt", await job.download("output/result.txt"));
```

**curl**

Just `curl` and `jq`; your key in `WBXSANDBOX_API_KEY`.

```
H="Authorization: Bearer $WBXSANDBOX_API_KEY"
JOB=$(curl -s -X POST https://sandbox.wbx.cz/v1/jobs -H "$H" -d '{"command": "mkdir -p output && echo 42 > output/result.txt",
  "timeout_s": 1800, "save_artifacts": true, "max_cost": "0.10"}' | jq -r .id)   # for CZK use "2.50"
curl -s -X POST https://sandbox.wbx.cz/v1/jobs/wait -H "$H" -d "{\"job_ids\": [\"$JOB\"], \"timeout_s\": 60}"
curl -s https://sandbox.wbx.cz/v1/jobs/$JOB/artifacts/output/result.txt -H "$H" -o result.txt
```

## How to tell it worked

- The job ends as `completed` and its cost is below your limit.
- `result.txt` contains `42`.
- In the portal, *Jobs* shows the job with its log, result files and cost.

## If it doesn't work

| What you see | What to do |
| --- | --- |
| `422 budget_too_low` | The price limit is below the worst case: the time limit plus about 7 minutes reserved for start and upload. Lower the time limit or raise `max_cost`. |
| `402 budget_exceeded` | The job would go over your monthly or daily [budget](https://sandbox.wbx.cz/docs/howto/budget.md). |
| `failed` with `nonzero_exit` | The command failed. Read the log: in the portal, with `job.digest("errors")`, or ask your assistant for the job log. |
| No result files | Only files in `output/` are kept, and only with `save_artifacts`. |

## What it costs

You pay for the time the job really runs (the `base` template about US$0.07, €0.06 or 1.6 Kč an hour). `max_cost` is a ceiling: a job whose worst case could cost more is refused before it starts.

## Next steps

- [Background jobs](https://sandbox.wbx.cz/docs/jobs.md): All options: templates, environment, retries, receipts.
- [Logs and waiting](https://sandbox.wbx.cz/docs/jobs/logs.md): A short digest of the log instead of thousands of lines.
- [Run many tasks at once](https://sandbox.wbx.cz/docs/howto/batch.md): The same job 20 or 1000 times.

Source: https://sandbox.wbx.cz/docs/howto/background-job
