# Thalamus HTTP API

Source: https://usecns.dev/contract-thalamus/

## 1. `Thalamus` HTTP Relay Contract (`node-d:18000`)

**Illustrative deployment:** node names, ports, model ids and seed-service counts in this section describe an example topology, not a live inventory or required hardware. Configure actual endpoints and discover available models through the documented interfaces. Wire formats and endpoint-resolution rules are the contract; the example fleet is not a default configuration.

### 1.1 Endpoint Resolution Order (`THALAMUS_URL`)

Remote use requires an explicitly configured endpoint; there is no built-in relay address. Endpoint precedence is organ-specific, not one shared environment-over-CLI rule:

| Consumer | Resolution, highest priority first |
| --- | --- |
| Engram embeddings | `ENGRAM_EMBEDDING_ENDPOINT`, OTE `retrieval.embedding_endpoint`, then `${THALAMUS_URL}/v1/embeddings`. |
| Engram reranking | `ENGRAM_RERANKER_ENDPOINT`, OTE `retrieval.reranker.endpoint`, then `${THALAMUS_URL}/v1/rerank`. |
| Axon remote embeddings | `--embed-url`, `AXON_EMBED_URL`, then `THALAMUS_URL`. Explicit URL flags require a remote provider. |
| Axon remote reranking | `--rerank-url`, explicit embedding URL, `AXON_EMBED_URL`, then `THALAMUS_URL`. `AXON_RERANK_URL` is not consumed by the reviewed implementation. |
| Soma rubric | Explicit `THALAMUS_URL`; verification model discovery and the shared timeout budget are described in §5.2. |
| Cortex | Configured catalog routes and Thalamus discovery; direct routing requires explicit `CORTEX_FALLBACK_*` configuration. See the [route reference](https://usecns.dev/commands-cortex/). |

Missing remote configuration is an error naming the setting (Engram, Axon), an `unrouted` route (Cortex), or **not evaluated** for the requested Soma rubric. Explicit local providers remain local. Axon's `auto` provider can select remote embeddings when endpoint environment configuration is present; use `local` to require isolation. Endpoint selection alone does not enable an Engram remote provider or reranker. See the [Engram](https://usecns.dev/commands-engram/) and [Axon](https://usecns.dev/commands-axon/) references before authorizing source/query material to leave the machine; [what leaves your machine](https://usecns.dev/providers/) summarises, per consumer above, what a configured endpoint receives and the documented transport of each client.

**No implicit fallback.** Soma (§5.2), `cns` and the Pi extension require the operator's explicit relay configuration. With `THALAMUS_URL` unset:

* `cns status` prints `[1] Thalamus : not configured (THALAMUS_URL not set) WARN`; if nothing else is red, the result is `STATUS: WARN`, exit 0 (neither red nor green).
* `cns init` makes no relay request and writes Git hooks carrying no address.
* `cns gate` with rubric opt-in prints `rubric: not evaluated (THALAMUS_URL not set)` and exits 3.
* The Pi extension sets no `THALAMUS_URL` for itself or its spawned gate and registers no `thalamus` provider.

**`cns init` and `cns curate`:** `axon anchors embed` runs only when `THALAMUS_URL` or `AXON_EMBED_URL` is set; otherwise they print `embed: not configured`.

`cns init` renders `.engram/ote.toml` from `harnesses/pi/ote.example.toml`, never copying template endpoints:

* With `THALAMUS_URL`, it sets `retrieval.embedding_endpoint` to `${THALAMUS_URL}/v1/embeddings` and `retrieval.reranker.endpoint` to `${THALAMUS_URL}/v1/rerank`.
* Without it, every `retrieval*` table is replaced by local-only configuration (`embedding_provider = "local-hash"`, reranker `enabled = false`, `provider = "none"`), and init reports this.
* Any other URL remaining in the rendered manifest makes init refuse to write it and name the URL. An existing `.engram/ote.toml` is never rewritten.

### 1.2 Data-Plane Endpoints

| Endpoint | Plane | Target Models & Nodes | Request / Response Contract |
| --- | --- | --- | --- |
| `POST /v1/chat/completions` | `Plane::Intelligence` (`system2`) | `Qwen/Qwen3.8-Flash-Next` on `node-a`, `node-b`, `node-c` (`:8000`) | OpenAI-compatible JSON or SSE stream. Supports `x-session-id` / `x-agent-session-id` header or deterministic prompt-prefix SHA-256 session pinning. Emits `x-thalamus-node`, `x-thalamus-sticky-hit`, `x-thalamus-work-stolen`, and `x-thalamus-queue-wait-ms` response headers. |
| `POST /v1/systemone` (alias `/v1/system1/decide`) | `Plane::Verification` (`system1`) | `clef-flash` on `node-d:8000` | Stateless rubric evaluation. Request: `{"model": "clef-flash", "state": "<diff or context>", "questions": {"q1": {"type": "noul" \| "choice" \| "score", "question": "<rubric>"}}}`. Response: `{"model": "clef-flash", "answers": {"q1": {"type": "noul", "noul": 0.0001}}, "usage": {...}}`. |
| `POST /v1/embeddings` | `Plane::Knowledge` (`retrieval`) | `qwen3-embedding-8b` on `node-d:8001` | OpenAI-compatible batch embedding request: `{"model": "qwen3-embedding-8b", "input": ["..."]}`. Returns 4096-dim (or caller-sliced/L2-normalized) float vectors in `data[].embedding`. |
| `POST /v1/rerank` (alias `/rerank`) | `Plane::Knowledge` (`retrieval`) | `qwen3-reranker-8b` on `node-d:8002` | Cross-encoder scoring: `{"model": "qwen3-reranker-8b", "query": "...", "documents": ["..."]}`. Returns `{"results": [{"index": 0, "relevance_score": 0.9805, ...}]}`. |

### 1.3 Control-Plane Endpoints

* `GET /admin/v1/status` (alias `GET /lb/status`): Returns the configured and dynamically registered member inventory, cluster health, active session pins, work-steal counters, admission metrics and per-node telemetry. The member count depends on configuration, not a built-in fleet.
* `POST /admin/v1/members`: Registers a member. Optional persistent registrations use the explicitly configured state file; there is no separate `GET /admin/v1/members` route.
* `POST /admin/v1/members/{name}/drain`: Drains the named member. `DELETE /admin/v1/members/{name}` removes it.
* `POST /admin/v1/policy`: Updates routing policy. The [Thalamus operator manual](https://usecns.dev/manual-thalamus/) documents additional compatibility endpoints, configuration and admission behavior against a pinned source revision.
* `GET /health`: `status`, `healthy_members`, `total_members`, and `telemetry`, which is `enabled (<path>)`, `degraded (<n> write error(s), ...)` or `disabled (<reason>)`. A disabled or degraded store never changes routing and never changes `status`; readers that care about history must read `telemetry` (see 1.4).
  `name_conflicts`: an array of strings, one per model id or alias declared on more than one plane (including names a probe learned from an upstream), each sentence naming both members. Reader: `cns status` [1]: a non-empty list renders `[1] Thalamus : WARN name-conflicts (<n>)` followed by one `name-conflict: <entry>` line per entry (control characters shown as spaces), and `STATUS: WARN (thalamus: name-conflicts (<n>) ...)` when nothing is red; an empty list is `ONLINE`; on an unhealthy Thalamus the list is still shown and the line stays `UNHEALTHY` (red); a field that is not an array of strings is `UNREADABLE name_conflicts` (red); a Thalamus without the field is not reported.

### 1.4 Durable Request Telemetry (`$THALAMUS_STATE_DIR/telemetry.db`)

Writer: `thalamus serve` (one row per completed request, written off the routing path). Reader: `thalamus replay --since <ts> [--node <name>] [--json]`, which reads the file directly and works while Thalamus is down. Routing never reads this data.

| Column | Type | Meaning |
| --- | --- | --- |
| `ts` | `INTEGER` | Completion time, unix milliseconds. |
| `node` / `plane` | `TEXT` | Member that served the request; `intelligence`, `verification` or `knowledge`. |
| `session_fp` | `TEXT` | First 16 hex chars of SHA-256 of the session id; `NULL` on stateless planes. The id itself is never stored. |
| `ttft_ms` / `latency_ms` | `REAL` | Time to first token (`NULL` when unknown) and total latency. |
| `prompt_tokens` / `completion_tokens` | `INTEGER` | From the upstream `usage` block; `NULL` for streamed responses. |
| `kv_hit` | `INTEGER` | `1` if `prompt_tokens_details.cached_tokens > 0`, `0` if it was zero, `NULL` when the upstream reported nothing. |
| `error` | `TEXT` | `http_<status>` for status >= 400, else `NULL`. |
| `stolen` | `INTEGER` | `1` if contention-aware work stealing moved the session. |

Retention: `--telemetry-retention-days` (default 30), applied at startup and hourly against the wall clock. Rows stamped in the future are kept and counted in `/health` (`telemetry_detail.future_dated_rows`). `replay` exit codes: `0` ok, `1` unreadable, `2` bad arguments, `3` `absent` (no `telemetry.db`).

---