> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tenetlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Placeholders and sessions

> What /deidentify writes into your text ([LABEL_N] placeholders), how sessions scope, expire and erase the placeholder map, and the rules a downstream consumer or prompt must follow.

**A placeholder** is `[LABEL_N]` — square brackets, an upper-case label, an underscore, a 1-based index — written where `/deidentify` removed a value. **A session** is the vault scope that maps those placeholders back to the encrypted originals, until its TTL lapses or someone erases it.

## When to use it

Read this before you send de-identified text to a model, route on it, or regex over it. `/deidentify` *rewrites* text; everything downstream depends on the format below whether it knows it or not.

## The flow

```text theme={null}
POST /deidentify {"text": "Email jane@example.com about the invoice."}            (detect service)
  → {"text": "Email [EMAIL_1] about the invoice.", "session_id": "AbC…", "expires_at": 1790000000.0, "findings": [...]}

your LLM sees only "Email [EMAIL_1] about the invoice." and replies "Sure, I have sent [EMAIL_1] a reminder."

POST /reidentify {"text": "Sure, I have sent [EMAIL_1] a reminder.", "session_id": "AbC…"}   (rehydrate service)
  → {"text": "Sure, I have sent jane@example.com a reminder.", "session_id": "AbC…", "restored": 1, "unresolved": []}
```

Every call is signed with the caller's own IAM role (SigV4, service `vpc-lattice-svcs`) and goes through VPC Lattice to the service that owns the route. [Rehydration and audit](https://docs.tenetlabs.com/pii-engine/concepts/rehydration-and-audit.md) covers who may call what.

## The placeholder format

```text theme={null}
[LABEL_N]        regex: \[([A-Z][A-Z0-9_]*)_(\d+)\]
```

The label set is closed. Every placeholder you'll ever see uses one of:

`ACCOUNT`, `ADDRESS`, `AGE`, `BUILDING_NUM`, `CITY`, `COORDINATES`, `CREDIT_CARD`, `DATE`, `DEA_NUMBER`, `DOB`, `DRIVERS_LICENSE`, `EMAIL`, `FIRST_NAME`, `ID_CARD`, `IP_ADDRESS`, `LAST_NAME`, `LICENSE_PLATE`, `LOCATION`, `MRN`, `NPI`, `PASSWORD`, `PHONE`, `PII`, `REFERENCE_NUM`, `SSN`, `STATE`, `TAX_ID`, `URL`, `USERNAME`, `VIN`, `ZIPCODE`

What you can rely on:

* **Indices are 1-based and per label**, so `[EMAIL_1]` and `[FIRST_NAME_1]` can coexist.
* **A value that recurs reuses its placeholder** — within one call and across calls in the same session. `jane@example.com` is `[EMAIL_1]` every time in that session.
* **Placeholder labels are display labels.** `findings[].label` in the same response is the detector's taxonomy label and can differ. Match placeholders with the regex, not by joining on `findings[].label`.
* **`[PII_N]` is a span the model flagged but couldn't name** (`findings[].label` `OTHER`). It's still removed and still restores. A clock time on its own — the time half of `2/5/1994 12:00:00 AM` — is one of these, not a date of birth.
* **`[REFERENCE_NUM_N]` is a structured account or reference id**, such as `AC-2EA6-KY4B-G6FX`.
* **A phone number keeps its extension.** `+1 202 555 0137 ext 11037` is one `[PHONE_N]`, and `+`-prefixed international numbers are detected.
* **The index is not an identity.** `[FIRST_NAME_1]` in two different sessions is two different people.

## Sessions

| | |
| - | - |
| Created | By `/deidentify` without `session_id`. The server issues a 128-bit id (22 URL-safe characters); you can never name one. |
| Extended | By `/deidentify` with that `session_id`. A client-supplied id that is unknown or expired → `404`; one that isn't shaped like a server-issued id → `400`. |
| TTL | Sliding, default 3600 s (range 1–604,800), extended on every `/deidentify` write, never on reads. Set it with `TENET_PII_SERVE_VAULT_TTL` (seconds) in the detect service's environment — Helm `detect.extraEnv`; the CloudFormation template and Terraform module don't expose it. |
| `expires_at` | Epoch seconds, returned on every `/deidentify`. |
| Erased | `DELETE /sessions/{session_id}` on the detect service, by a role in the erasure invoker list (`ErasureInvokerRoleArns`, `erasure_invoker_role_arns`, `lattice.erasureInvokerRoleArns`) → `204`, always, including for unknown ids. A `tenet.erasure-audit.v1` record is written first; if it can't be, the call returns `503` and the session survives. With an empty list nobody can erase, and sessions just expire. |
| Limit | 10,000 placeholders per session. Crossing it → `400`, and nothing is written. Start a new session per document or conversation. |
| Concurrency | Two writers to one session at once → `409 "concurrent deidentify writes to one session; retry the request"`. Retry. |

The `session_id` is a **capability**: together with a role the rehydrate service admits, it returns plaintext until the session expires. Store it next to the conversation it belongs to, treat it like a credential, and never log it. Audit records carry `session_ref`, a keyed digest, instead.

## Re-identification rules

* **Single pass, allowlist only.** Bracketed text is replaced only if it is an exact key in this session's map. The output is never rescanned, so a restored value that happens to look like a placeholder can't be substituted again.
* **Hallucinated placeholders are not errors.** A model that invents `[EMAIL_9]` gets it back unchanged in `text` and listed in `unresolved` — `200`, not `4xx`. Check `unresolved` before you show the text to a person.
* **Pre-existing placeholder-shaped text in user input** (`[EMAIL_1]` typed by a user) can be substituted on re-identify if that session issued the same placeholder.

## What your consumer must do

* **Tell your model what the placeholders mean.** A prompt that has never seen `[FIRST_NAME_1]` treats it as noise or reasons through it badly. One line is enough: "Text in `[LABEL_N]` form is a placeholder for a removed value of that type. Keep placeholders exactly as written."
* **Widen your token classes.** A router or regex built for plain words won't match `[FIRST_NAME_1]` — brackets are outside `[\w…]`. Admit the placeholder regex explicitly.
* **Keep placeholders verbatim.** A model that rewrites `[EMAIL_1]` as `EMAIL_1` or `[Email 1]` produces text that won't restore.

Wire de-identify → LLM → re-identify into an existing app, signed with the app's own IAM role.

Add Tenet PII Engine (Enterprise) de-identification around this codebase's LLM calls, following [https://docs.tenetlabs.com/pii-engine/concepts/placeholders-and-sessions](https://docs.tenetlabs.com/pii-engine/concepts/placeholders-and-sessions.md).

Ask me for: the env var names holding the detect and rehydrate endpoints (`https://<name>.<hash>.vpc-lattice-svcs.<region>.on.aws` — never hardcode them); which IAM role this code runs as, and confirm it is in the detect invoker list (and the rehydrate and erasure lists if it restores or erases) with `vpc-lattice-svcs:Invoke` in its own policy; whether the `tenet-pii-lattice` client is already a dependency (otherwise add `tenet-pii-lattice @ https://platform.tenetlabs.com/enterprise/current/tenet_pii_lattice.tar.gz` to the project's dependencies; it needs Python 3.11+ and boto3); which code paths call the LLM; which users or services may see restored text; the session scope I want (per conversation, or per document).

1. Find every place that sends user or document text to an LLM. Before each call, use `tenet_pii_lattice.LatticeClient(detect_endpoint).deidentify(text, session_id)` (omit session\_id to start a session) and send only the returned `text` to the model. Store the returned `session_id` with the conversation, treated as a secret (never log it). Reuse one client per thread: it holds one HTTPS connection and isn't thread-safe. Credentials come from the default AWS chain — no tokens or keys in code.
2. Add one line to the system prompt: text in \[LABEL\_N] form is a placeholder for a removed value of that type; keep placeholders exactly as written.
3. Where the model's reply is shown to an authorized user, call `LatticeClient(rehydrate_endpoint).reidentify(reply, session_id)`. If `unresolved` is non-empty, surface that rather than silently showing bracketed text. Never call rehydrate from a browser or an unauthenticated path.
4. If the app deletes conversations, call `LatticeClient(detect_endpoint).erase(session_id)` as a role in the erasure invoker list.
5. Handle `TenetLatticeError(status, detail)`: 403 = refused by VPC Lattice or the service (wrong role, missing identity policy, source VPC or approval tag) — log the status, don't retry, don't fall back; 404 = session expired or erased (start a new one; don't retry the restore), or a path the service doesn't serve; 409 on /deidentify = retry once; 503 from /reidentify = the audit record couldn't be written and nothing was restored, retry later; 503 from erase = the erasure audit failed and nothing was deleted, retry later. The client never retries /deidentify itself; don't add a blind retry there either, or a resend can open a second session.
6. Write a test with a synthetic sample containing a name and an email that asserts the text sent to the model contains no original value and the restored reply equals the expected text (mock the client in unit tests).
   Do not send raw text to the LLM on any error path. Show me the diff before writing files.

## Gotchas

* **`/redact` is not reversible.** It rewrites with `mask`, `last_four`, `hash_prefix` or `drop` and stores nothing. Use `/deidentify` when you need the values back.
* **Detection recall bounds everything.** A value the detector missed is not replaced and reaches the model as-is. The services run at a confidence threshold of 0.55; a per-request `confidence_threshold` overrides it. Measure recall on your own data before you rely on de-identification. [Attestation](https://docs.tenetlabs.com/pii-engine/security/attestation.md).
* **Sessions are TTL-bounded, not a system of record.** After expiry or erasure, `/reidentify` returns `404` for that session and the values are gone.
* **Erasure answers `204` whatever happened.** Only the erasure audit record says whether the session existed (`outcome: "erased"`) or not (`unknown_session`).

## Links

* [Rehydration and audit](https://docs.tenetlabs.com/pii-engine/concepts/rehydration-and-audit.md)
* [API Reference](https://docs.tenetlabs.com/pii-engine/api-reference/introduction.md)
* [What the attestation does and does not certify](https://docs.tenetlabs.com/pii-engine/security/attestation.md)


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