# Batches and jobs

Long-running work runs as a job. The jobs are [research](https://websearch.fast/docs/research), [crawl](https://websearch.fast/docs/crawl) and the asynchronous mode of [batch](https://websearch.fast/docs/batch). Many short calls in one request go through the synchronous mode of batch. Jobs are planned. Their paths return `501 api_not_implemented`.

## Lifecycle

A create call answers `202` with the job object at once. `status` moves from `queued` to `running` and ends as `succeeded`, `failed`, `cancelled` or `expired`. Read the job with its `GET` path. `poll_after_ms` in the job says when to read it again. A job that ran out of its budget ends `succeeded` with `stop_reason: "budget"` and keeps what it found.

`POST .../cancel` stops a job. What was done before the cancel took effect stays readable and is charged.

## Idempotency

Send an `Idempotency-Key` (1 to 128 bytes, kept 24 hours) with every create. Repeating a create with the same key and body returns the same job and the header `Idempotent-Replayed: true`, so a retry after a network error never starts a second job. The same key with a different body gets `409 idempotency_conflict`. While the first create is still running, the answer is `409 request_in_progress` with `Retry-After`.

## Costs of a job

A job is charged per unit used while it runs. `max_cost_usd` caps what one job can charge. The job stops with `stop_reason: "budget"` when the next step would pass the cap.

## Retention

| Object | Kept after it ends |
| --- | --- |
| Research job and report | 30 days |
| Crawl job and pages | 7 days |
| Asynchronous batch and results | 7 days |
| Events of a job | as long as its job |

`expires_at` on each job says when it expires. After that its paths return `404 not_found`.
