# Run the trace worker

Run the Trace GPU worker on a dedicated or serverless GPU and connect a self-hosted Provenance API to it.

Use the trace worker to run Trace on your own GPU. The worker loads an open-weight model, hooks every MLP layer, and serves completions, canary readings and attribution graphs over HTTP to the Provenance API.

The same image runs Provenance's hosted inference and, on any plan, sits beside your own model as a [sidecar](https://provenanceauth.com/docs/self-host/sidecar.md).

**Before you begin:** a GPU with room for the model (Qwen3-8B in bf16 needs about 16 GB, so one 24–48 GB card), Docker, and a checkout of the Provenance repository. Managed inference APIs can't be used, because they don't expose activations.

## 1. Build and start the worker

Build the image with the CUDA wheel index for your driver, then start it with a model and a random bearer token:

```bash
docker build -f python/trace-worker/Dockerfile --build-arg TORCH_INDEX=cu124 -t provenance-trace-worker .
export TRACE_WORKER_TOKEN="$(openssl rand -hex 32)"
docker run --gpus all -p 8090:8090 \
  -e TRACE_WORKER_BACKEND=hf \
  -e TRACE_MODEL=Qwen/Qwen3-8B \
  -e TRACE_WORKER_TOKEN \
  provenance-trace-worker
```

Without a GPU, build with `TORCH_INDEX=cpu` and run with `TRACE_WORKER_BACKEND=fixture` to replay the sandbox recordings. Next to a local stack, `docker compose --profile gpu up trace-worker` starts it.

## 2. Check that the model is loaded

**curl**

```bash norun
curl localhost:8090/health
```

**Node**

```ts
const health = await (await fetch("http://localhost:8090/health")).json();
```

**Python**

```python
import httpx

health = httpx.get("http://localhost:8090/health").json()
```

```json
{ "status": "ready", "backend": "hf", "models": ["qwen3-8b@b968826d"] }
```

While the model loads, `status` is `loading` and every other route answers `503`, which the API reports as [`trace_backend_cold`](https://provenanceauth.com/docs/platform/errors.md#trace_backend_cold).

## 3. Connect the API

Set these on the API and restart it:

```bash
TRACE_BACKEND=remote
TRACE_WORKER_URL=https://trace-worker.internal.example:8090
TRACE_WORKER_TOKEN=<the same token>
TRACE_MASK_SALT=<at least 16 random characters>
```

In production the worker URL must be https and resolve to a public address, the same guard outbound webhooks use. `TRACE_BACKEND=fixture` is refused in production. Every setting is on [server configuration](https://provenanceauth.com/docs/self-host/configuration.md).

## 4. Confirm the API sees the worker

[`GET /v1/trace/health`](https://provenanceauth.com/docs/api/traced-inference.md#get-v1-trace-health) reports the configured backend and the worker's state:

**curl**

```bash
export PROVENANCE_API=https://sandbox-api.provenanceauth.com
export PROVENANCE_KEY=prv_test_acct_P9xN2vQ7bT4mW8sYd3cFjLh6gAe1kR5
curl $PROVENANCE_API/v1/trace/health -H "Authorization: Bearer $PROVENANCE_KEY"
```

**Node**

```ts
import { Provenance } from "@provenanceauth/sdk";

const prov = new Provenance({ apiKey: process.env.PROVENANCE_KEY!, baseUrl: process.env.PROVENANCE_API });
const status = await prov.trace.health();
```

**Python**

```python
import os
from provenanceauth import Provenance

prov = Provenance(os.environ["PROVENANCE_KEY"], base_url=os.environ["PROVENANCE_API"])
status = prov.trace.health()
```

```json
{
  "configured": true,
  "backend": "fixture",
  "kind": "fixture",
  "status": "ready",
  "models": ["qwen2.5-0.5b-instruct@7ae55760"],
  "registry_models": 2
}
```

The sandbox reports `fixture`; your API reports `remote` once it reaches the worker. When it can't, `status` says why: the worker is loading, down, or not configured.

## Choose where to run it

| Option | Trade-off |
| --- | --- |
| A dedicated GPU pod (recommended) | The model stays loaded, so there are no cold starts. |
| Serverless GPU | Cold starts take minutes while weights load. Set `TRACE_KEEP_WARM_INTERVAL_MS` and the API pings `/health` to keep the model loaded. A `trace_id` from an earlier inference never waits on the GPU. |

## Optional: regenerate the sandbox recordings

The sandbox fixtures are measured on Qwen2.5-0.5B-Instruct on CPU, in about ten minutes:

```bash
pip install -e 'python/trace-worker[hf]'
python python/trace-worker/scripts/gen_fixtures.py
```

Everything it writes is measured on the model except the labels, which are heuristic and name their labeler.

To add a listed model that the registry shows as `building`, measure it on its pinned revision into a scratch folder, then merge it. Qwen2.5-7B-Instruct needs about 15 GB in bf16, so a GPU, or 32 GB or more of memory:

```bash
python python/trace-worker/scripts/gen_fixtures.py --hf-id Qwen/Qwen2.5-7B-Instruct \
  --revision a09a35458c702b33eeacc393d103063234e8bc28 --device mps --out /tmp/7b
python python/trace-worker/scripts/add_registry_model.py --from /tmp/7b
```

The merge refuses a different revision, unhashed weights or a recording of another model, and writes nothing when it refuses.

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| `503` [`trace_backend_not_configured`](https://provenanceauth.com/docs/platform/errors.md#trace_backend_not_configured) | `TRACE_BACKEND` is `none` | Set it to `remote` with the worker URL and token. |
| `503` [`trace_backend_cold`](https://provenanceauth.com/docs/platform/errors.md#trace_backend_cold) | The model is still loading | Retry after `Retry-After`, or keep a serverless worker warm. |
| `503` [`trace_model_not_built`](https://provenanceauth.com/docs/platform/errors.md#trace_model_not_built) | The model is listed but its registry isn't built | Use a model whose status is `ready` or `fixture`, or build it as above. |
| `502` [`trace_backend_error`](https://provenanceauth.com/docs/platform/errors.md#trace_backend_error) | The worker failed or timed out | Check the worker's logs and its token. |
| The API refuses to boot | A missing `TRACE_MASK_SALT`, or `fixture` in production | Set the salt and a `remote` backend. |

## See also

- [Self-hosted sidecar](https://provenanceauth.com/docs/self-host/sidecar.md)
- [Server configuration](https://provenanceauth.com/docs/self-host/configuration.md)
- [Builds and canary sets](https://provenanceauth.com/docs/trace/guides/registry-builds.md)
