# Lab — Managed inference, ZDR and PII redaction

**Time:** 120 minutes — 10 setup, 10 snapshot and predict, 10 redaction, 5 EXIF, 10 calls A+B, 10 call C, 15 call D and the ZDR-only toggle, 5 rule, 15 register, 20 evidence, 10 teardown. Core 80, evidence 20, setup 10, teardown 10.

**Prerequisite:** the OpenRouter account and API key created in S6, the S7 register rows R01–R05, Python 3.14 with Pillow 11.3.0, `curl`, and `jq`. The shared synthetic pack is at `../../assets/synthetic-media/` — 20 EXIF-tagged photos and 100 transcripts with fabricated contact details. Do not substitute your own photos or transcripts.

**You need to know:** a managed inference call crosses a trust boundary. OpenRouter is a router: one model id is served by many provider **endpoints**, and each endpoint has its own data policy. A model's brand does not tell you what the serving endpoint retains. The ZDR list (`GET /api/v1/endpoints/zdr`) names the endpoints that meet zero-data-retention terms, and each row carries `safety_retention`. Your governance rule must name the **policy** the endpoint must satisfy, not just the model you like.

**Safe-data acknowledgement:** I have read the lab safe-data policy. I will use only the course-supplied synthetic media and will not upload real personal media to any cloud service.

## Task

Predict which endpoints satisfy the governance rule, redact the 100 transcripts and strip EXIF from the 20 photos, then make four calls: a deliberate failure, the control that refuses it, a clean governed call, and a retention contrast that fails closed under ZDR-only. Finish by writing the six-part rule and the ten-row risk register.

## Before you start — the panel checklist

These steps are panel-only; there is no CLI for them. Do them in the OpenRouter dashboard first.

1. **Settings → Credits**: confirm the balance, turn **auto top-up off**, and top up to **$10** if S6 left less. The purchase fee is 5.5% with a **$0.80 minimum**, so about **$10.55** is charged. The top-up is reimbursed under the course account policy and stays yours for the capstone.
2. **Keys → Create key**: name it `course-s8-<your-id>`. The key is shown once. Copy it, then close the dialog.
3. **Settings → Privacy**: leave the training toggles at their course defaults for calls A–D. Two separate toggles control paid-model and free-model training. Do not change them yet; Step 6 changes ZDR deliberately so you can observe the effect.
4. **Settings → Privacy → ZDR**: confirm the per-model-group ZDR control is visible. You will set **ZDR only** in Step 6.

The account and key are the ones S6 created. Do not create a second account.

## Steps

### 1. Store the key and confirm the account

Write the key to a local `.env` file that is never committed. `umask 077` makes the file private at creation; `chmod 600` states the rule explicitly. Sourcing the file puts the key in the shell's environment without printing it.

```bash
umask 077
printf 'OPENROUTER_API_KEY=%s\n' '<paste-the-key-here>' > .env
chmod 600 .env
set -a; . ./.env; set +a
```

Create the results directory for this run's evidence.

```bash
export RESULTS=results
mkdir -p "$RESULTS"
```

Read the key's state into the evidence directory: usage, any limit, the remaining balance, whether the account is treated as free-tier, and the free-model daily allowance.

```bash
curl -sS https://openrouter.ai/api/v1/key -H "Authorization: Bearer $OPENROUTER_API_KEY" -o "$RESULTS/key-before.json"
jq '{usage, limit, limit_remaining, is_free_tier, free_model_daily_requests}' "$RESULTS/key-before.json"
```

**What to look for:** `usage` is what has been spent, `limit_remaining` is the balance, and `is_free_tier` should be false after the top-up. Record this JSON as evidence item 2. Never photograph or paste the key itself.

### 2. Snapshot the catalogue — the prediction input

The endpoint list for the DeepSeek model. It returns every provider endpoint, with `provider_name`, `tag`, prices and `status`.

```bash
export SNAPSHOT_DIR=data
mkdir -p "$SNAPSHOT_DIR"
curl -sS https://openrouter.ai/api/v1/models/deepseek/deepseek-v4.1-flash-1017/endpoints -o "$SNAPSHOT_DIR/endpoints-deepseek.json"
```

The same list for the Gemini model.

```bash
curl -sS https://openrouter.ai/api/v1/models/google/gemini-4-hydrogen/endpoints -o "$SNAPSHOT_DIR/endpoints-gemini.json"
```

The ZDR preview: every endpoint in the catalogue that meets zero-retention terms, each with `safety_retention`.

```bash
curl -sS https://openrouter.ai/api/v1/endpoints/zdr -o "$SNAPSHOT_DIR/endpoints-zdr.json"
```

Your account's current model settings, before the ZDR-only toggle. It is account evidence, not a model list.

```bash
curl -sS https://openrouter.ai/api/v1/models/user -H "Authorization: Bearer $OPENROUTER_API_KEY" -o "$SNAPSHOT_DIR/models-user-before.json"
```

Tell the notebook where the fresh snapshots are and what date to stamp on them. The shipped copies in `data/` are a dated fallback for offline work.

```bash
export S8_SNAPSHOT_DIR="$SNAPSHOT_DIR"
export S8_SNAPSHOT_DATE="$(date -u +%F)"
```

**What to look for on 2026-11-13:** 48 DeepSeek endpoints and 7 Gemini endpoints; the ZDR catalogue holds 1,099 endpoints across all models. Joining the model lists to the ZDR list by `tag` leaves 42 of 48 DeepSeek endpoints and 4 of 7 Gemini endpoints. DeepSeek's **own first-party endpoint** (`tag: deepseek`) is not in the ZDR list. Every Gemini ZDR endpoint is a Google Vertex endpoint with `safety_retention: true`, and all three Google AI Studio endpoints are excluded. These counts drift; the date travels with the evidence. The shipped `data/endpoints-zdr.json` keeps only the rows for these two models and records the full catalogue total, so the file stays small and does not publish other providers' endpoint names.

### 3. Redact and strip — make the controls executable

Run the supplied helper over the whole pack. It redacts every transcript, strips EXIF from every photo, and writes a report whose before/after counts are the evidence.

```bash
export RESULTS=results
python3 redact.py --pack ../../assets/synthetic-media --out "$RESULTS/redaction"
```

The project environment pins the same Pillow version in `requirements-local.txt`; the runner uses this equivalent so the version is reproducible.

```bash
uv run --with-requirements requirements-local.txt python redact.py --pack ../../assets/synthetic-media --out "$RESULTS/redaction"
```

**What to look for:** `transcripts 100`, `pii_rows_before 100`, `pii_rows_after 0`, `photos 20`, `gps_before 20`, `gps_after 0`, `exif_tags_before 100`, `exif_tags_after 0`. The report also carries one before/after transcript line. The redacted transcripts and stripped photos are working files; the report and the diff are the evidence. If a count is different, stop and fix the control before calling.

### 4. Predict, then apply the rule

Open `notebook.ipynb` in JupyterLab and run the cells in order. Step 1 joins the snapshots and prints the prediction table: provider, tag, prompt price per million tokens, whether the endpoint is ZDR, and its `safety_retention` flag. Step 4 turns the six-part rule into a function and checks the cases, including the maker's own endpoint and the Gemini carve-out.

**What to look for:** 48/42 for DeepSeek, 7/4 for Gemini, `deepseek` absent from the ZDR set, and every Gemini ZDR row `safety_retention: true`. The notebook asserts these, so a drifted snapshot fails loudly instead of quietly changing the lesson.

### 5. Make the four calls

The calls are made from the notebook, not the shell. The runner command below executes the same notebook headlessly; a learner using Jupyter runs the cells instead. Set the live flag only after the snapshots and the redaction exist.

```bash
test -n "${OPENROUTER_API_KEY:-}"
export OPENROUTER_LIVE=1
python3 run_notebook.py notebook.ipynb
```

The runner uses the pinned project environment and sends the artifact to `results/`.

```bash
S8_OUT="$RESULTS" uv run --with-requirements requirements-local.txt python run_notebook.py notebook.ipynb
```

The four calls the notebook makes are described here so you can read the request before you run it. **A — the deliberate failure.** The raw synthetic line is sent to DeepSeek's own endpoint, with no policy filter. The identifier is fabricated, but the provider sees it.

```json
{
  "model": "deepseek/deepseek-v4.1-flash-1017",
  "messages": [{"role": "user", "content": "Summarise this synthetic caption request: Anan S. (anan.s@example.test, +66 000 000 001, student S0000001) approved the campus library clip."}],
  "max_tokens": 1200,
  "temperature": 0.2,
  "provider": {"only": ["deepseek"], "allow_fallbacks": false}
}
```

**B — the control.** The same request and the same pinned endpoint, with the policy enforced. DeepSeek's first-party endpoint is not in the ZDR list, so OpenRouter refuses before anything reaches a provider. The expected result is a 4xx with a "no endpoints match your data policy" message; on 2026-11-13 it was `404 No endpoints found matching your data policy (Zero data retention). Configure: https://openrouter.ai/settings/privacy`. Record the exact status and body as evidence, and do not treat the refusal as an outage.

```json
"provider": {"only": ["deepseek"], "zdr": true, "data_collection": "deny", "allow_fallbacks": false}
```

**C — the clean governed call.** The redacted text and the EXIF-stripped image, no provider pinned, policy enforced. The serving provider must be one of the 42 ZDR rows. Record `provider`, `usage` and the output.

```json
{
  "model": "deepseek/deepseek-v4.1-flash-1017",
  "messages": [{"role": "user", "content": [
    {"type": "text", "text": "Describe this redacted caption request in one sentence. Do not include names, places or identifiers."},
    {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,<photo-01.stripped.jpg>"}}
  ]}],
  "max_tokens": 1200,
  "temperature": 0.2,
  "provider": {"zdr": true, "data_collection": "deny", "allow_fallbacks": false}
}
```

**D — the retention contrast.** The same clean payload on Gemini. Under the default settings, `zdr: true` routes to Vertex, whose ZDR still carries `safety_retention: true`, and the call succeeds. Record the serving provider and the flag.

```json
"model": "google/gemini-4-hydrogen"
```

**What to look for:** A returns 200 and the provider sees the raw line; record whether the identifier appears in the output — a reasoning model may paraphrase or omit it, and the point is that it was sent. B returns 4xx with no provider; C returns 200 from a provider in the ZDR set; D returns 200 from Google with `safety_retention: true`. A reasoning model spends part of the token budget on hidden reasoning before its visible answer, so `max_tokens` is 1,200 rather than 100; a small budget can return an empty `content` with a `finish_reason` of `length`. The notebook writes every call, status, serving provider, usage and computed cost to `inference_results.json`. On this first live run the ZDR-only pair is still canned; Step 6 replaces it.

### 6. Flip the account to ZDR only and retry

Panel: **Settings → Privacy → ZDR only** for the Google model group and for "All other models". Refetch the account settings so the change is recorded.

```bash
curl -sS https://openrouter.ai/api/v1/models/user -H "Authorization: Bearer $OPENROUTER_API_KEY" -o "$SNAPSHOT_DIR/models-user-zdr-only.json"
```

Run the notebook again with the toggle marked. The ZDR-only pair is made live in this run; the four default calls are repeated too.

```bash
export OPENROUTER_ZDR_ONLY=1
python3 run_notebook.py notebook.ipynb
```

The runner's pinned-environment equivalent.

```bash
S8_OUT="$RESULTS" uv run --with-requirements requirements-local.txt python run_notebook.py notebook.ipynb
```

**What to look for:** D now fails closed with a 4xx, because every remaining Gemini ZDR endpoint carries safety retention and the account no longer allows it. On 2026-11-13 the refusal was the same `404 No endpoints found matching your data policy (Zero data retention)` body as call B. C still succeeds, because all 42 DeepSeek ZDR endpoints are true ZDR (`safety_retention: false`). That pair is the retention part of the rule, observed rather than asserted. The retry reads the previous live artifact and keeps calls A–D, so the before/after pair survives in one file; it records `zdr_only_source: live`. If there is no previous live artifact, the notebook stops and tells you to run the default pass first.

### 7. Write the rule and complete the register

Revise `data/governance-rule.json` for your deployment. It has six parts — what may be sent, what must be removed, **which endpoint and model**, where it may go, how long it may stay, and who reviews — plus the incident path. The notebook evaluates the rule; it does not replace your judgement.

`data/register.json` carries the ten rows: R01–R05 from S7, R06–R10 for inference and compliance. Revise R06–R10 for what you observed, rerun the notebook to score the rows, and write 200 words justifying the two highest residual risks. Name what would change each judgement.

### 8. Verify the evidence

Run the verifier over the results directory. It checks the artifact, the snapshot counts, the refusal statuses, the serving provider, the cost ceiling, the register vocabulary and the secret scan.

```bash
bash verify.sh "$RESULTS"
```

The runner states which mode it expects; a canned artifact cannot pass a live check.

```bash
EXPECTED_MODE=live bash verify.sh "$RESULTS"
```

**What to look for:** `verify OK` and the printed totals. A non-zero exit is a failed run, not a warning.

### 9. Teardown

Record the balance after the calls. Keep it with the evidence; it proves the lab stayed inside the ceiling.

```bash
curl -sS https://openrouter.ai/api/v1/key -H "Authorization: Bearer $OPENROUTER_API_KEY" -o "$RESULTS/key-after.json"
jq '{usage, limit_remaining}' "$RESULTS/key-after.json"
```

Delete the working copies of the redacted data and the stripped images. The reports and the evidence stay; only the regenerable files go.

```bash
rm -f "$RESULTS/redacted-transcripts.jsonl"
rm -rf "$RESULTS/photos"
```

Remove the local secret if you are not keeping the key for the capstone. If you are keeping it, leave `.env` in place and say so in the teardown log.

```bash
unset OPENROUTER_API_KEY
rm -f .env
```

Panel: confirm **auto top-up is still off**, record the remaining balance, and either keep `course-s8-<id>` for the capstone or disable the key. There is no cloud resource to terminate; the $10 top-up remains as credit.

## Evidence bundle — ten items

1. Account, key custody and the $10 top-up with the 5.5% fee — redacted, never the secret.
2. `GET /api/v1/key` before and after: balance, limit, remaining balance and free-tier status.
3. The redaction before/after pair and the EXIF metadata diff.
4. The endpoint prediction table: all versus ZDR versus excluded, with `safety_retention`.
5. Call A: the raw prompt, the serving endpoint and whether the identifier was echoed.
6. Call B: the refusal status and body.
7. Call C: clean output, serving endpoint, usage and computed cost.
8. Call D: the Vertex route and `safety_retention`; the ZDR-only fail-closed result.
9. The six-part rule and the ten-row register with the 200-word residual-risk justification.
10. `inference_results.json`, the marked trust-boundary map, the 150–250-word note and the teardown log.

The bundle is scored 3/2/1/0 by the standard lab rubric. A fallback that changes the submitted shape is a defect; a fallback that substitutes a path is labelled item by item.

## Failure and recovery

Call A is the deliberate failure: a synthetic identifier reaches a provider because the pipeline did not redact. Call B is the control refusing before anything leaves. D under ZDR-only is the control failing closed. The recovery is never a better prompt alone: redact, strip, enforce the policy, then choose an allowed endpoint. A refusal is recorded as evidence, not treated as an outage.

## Cost ceiling

The four calls and the ZDR-only retry are **under $0.01**. DeepSeek starts at $0.03 per million input tokens on its cheapest ZDR endpoint and Gemini at $1.50–$2.70 per million input tokens, with a small image and a short prompt. The lab ceiling is **$0.10**; the notebook computes the cost from `usage` multiplied by the serving endpoint's prices and asserts it. The $10 top-up plus the fee is account funding, reimbursed separately, and remains yours for the capstone.

## Fallback

If the card top-up is blocked, the instructor demo key runs A–D and you keep the same evidence shape. If the API is blocked entirely, the dated snapshots and `canned-inference.json` carry the prediction, redaction, rule and register cells, and the four calls are marked **not run** rather than fabricated. The notebook's `mode` field says `canned` or `live`; never present a canned run as live.

## Reference run

The agent-run reference sequence is `run.sh`; the learner still types the commands above. The runner is the verified record of the same steps, not a substitute for them. It provisions no cloud resource, so there is nothing to terminate.

The runner checks the pinned environment first.

```bash
uv run --with-requirements requirements-local.txt python --version
uv run --with-requirements requirements-local.txt python -c 'import PIL; print("Pillow", PIL.__version__)'
```

Run the offline path, then the live path with the key in the environment, then the ZDR-only retry after the panel toggle.

```bash
MODE=canned bash run.sh
MODE=live bash run.sh
REFRESH=1 MODE=live bash run.sh
MODE=live ZDR_ONLY=1 bash run.sh
EXPECTED_MODE=canned bash verify.sh "$RESULTS"
```

## Notebook

Open [the lab notebook](notebook.ipynb). Step 1 predicts the endpoints, Steps 2–3 redact the pack and strip EXIF, Step 4 applies the six-part rule, Step 5 makes calls A–D, Step 6 prices them, Step 7 scores the register and Step 8 writes `inference_results.json`. The runner's verified command sequence is in [COMMANDS.md](COMMANDS.md); the learner types the commands in this brief, not the runner.
