> ## Documentation Index
> Fetch the complete documentation index at: https://khaos-ml.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Serving

> Deploy artifacts behind an HTTP API.

`khaos serve` loads one or more artifacts and exposes them over HTTP. It runs the
same pipelines you get from `khaos.load()`, so what you test locally is what you
deploy.

## Run a server

```bash theme={null}
pip install "khaos-ml[serve]"
khaos serve pde_darcy_flow_2d pde_heat_diffusion_2d --port 8000
```

You can also point it at a local artifact directory, for example straight out of a
training run:

```bash theme={null}
khaos serve ./runs/darcy-v3/artifact
```

All artifacts are loaded at start-up, so a missing or broken artifact fails the
deploy instead of the first request.

## Endpoints

| Method | Path | Purpose |
| - | - | - |
| `GET` | `/health` | Liveness check and the list of served artifacts |
| `GET` | `/v1/artifacts` | Name, domain and version of each served artifact |
| `POST` | `/v1/artifacts/{name}/predict` | Run an artifact on a JSON input |
| `POST` | `/v1/artifacts/{name}/predict/file` | Run an artifact on an uploaded file |

```bash curl theme={null}
curl -X POST localhost:8000/v1/artifacts/pde_darcy_flow_2d/predict \
  -H "Content-Type: application/json" \
  -d '{"input": [[0.0, 0.1], [0.2, 0.3]]}'
```

```python python theme={null}
import requests

r = requests.post(
    "http://localhost:8000/v1/artifacts/pde_darcy_flow_2d/predict",
    json={"input": [[0.0, 0.1], [0.2, 0.3]]},
)
print(r.json())
```

```json theme={null}
{
  "artifact": "pde_darcy_flow_2d",
  "result": { "prediction": [[0.0, 0.4], [0.9, 1.3]] }
}
```

The `result` is the artifact's own container, serialized field by field. Arrays
become nested lists.

File-based domains such as CAD parts and SEC filings use the upload route:

```bash theme={null}
curl -X POST localhost:8000/v1/artifacts/geo_part_classifier/predict/file \
  -F "file=@bracket.step"
```

## Authentication

Set `KHAOS_API_KEY` to require a bearer token on every `/v1` route. `/health` stays
open for load balancers.

```bash theme={null}
KHAOS_API_KEY=change-me khaos serve pde_darcy_flow_2d
curl localhost:8000/v1/artifacts -H "Authorization: Bearer change-me"
```

## Docker

The repository ships a `Dockerfile`. Mount a volume at `/models` to ship weights
with the deployment, or let the container download them from the Hub on start.

```bash theme={null}
docker build -t khaos .
docker run -p 8000:8000 \
  -v $PWD/models:/models \
  -e KHAOS_ARTIFACTS=pde_darcy_flow_2d,pde_heat_diffusion_2d \
  khaos
```

## Configuration

| Variable | Default | Purpose |
| - | - | - |
| `KHAOS_ARTIFACTS` | none | Comma-separated artifacts to serve when none are passed on the command line |
| `KHAOS_API_KEY` | none | Bearer token required on `/v1` routes |
| `KHAOS_HOME` | `~/.cache/khaos` | Where artifacts are stored and downloaded to |
| `KHAOS_HUB_ORG` | `fletxcher` | Hugging Face Hub namespace artifacts are pulled from |

> [!NOTE]
>
> Inference runs on CPU through ONNX Runtime. Requests run in a thread pool, so
> scale throughput by running more worker processes behind a load balancer.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.