# Vista Brain — Libraxis API Full Agent Handbook

> Complete, copy-pasteable engineering guide for building against `https://api.libraxis.com`.

## 1. Base URL, Liveness & Discovery

- Production base URL: `https://api.libraxis.com`
- Public liveness (no API key): `GET https://api.libraxis.com/health` → `{"ok": true}`
- Public speech liveness (no API key): `GET https://api.libraxis.com/v1/audio/health`, `GET https://api.libraxis.com/v1/audio/ready`
- Machine-readable contract: `GET https://api.libraxis.com/openapi.json` and `GET https://api.libraxis.com/openapi.yaml`
- Concise index: `GET https://api.libraxis.com/llms.txt`
- Content negotiation: send `Accept: text/markdown` on `/`, `/api-reference`, `/docs`, `/about`, `/contact`, `/privacy`, or any unknown path (404) to receive Markdown with `Vary: Accept`.

## 2. Authentication

Send one of the following headers on all protected `/v1/*` requests:

```http
Authorization: Bearer $LIBRAXIS_API_KEY
X-API-Key: $LIBRAXIS_API_KEY
```

Keys are issued during the reviewed private beta by exchanging a signed registration token at `https://api.libraxis.com/access` (`POST /access`) or requesting onboarding via `contact@libraxis.ai`.

## 3. Canonical Model Aliases

Always inspect `GET /v1/models` to verify live capabilities and context limits. Do not send raw Hugging Face repository IDs.

| Alias | Purpose |
|---|---|
| `chat` | General clinical and developer chat policy |
| `soap` | Veterinary SOAP note generation policy |
| `master` | Primary reasoning policy |
| `emergency` | Low-TTFT veterinary triage and critical-care policy |
| `ai-suggestions` | Fast suggestion policy |
| `programmer` | Code generation and technical formatting policy |
| `buddy` (`buddy-none`/`low`/`medium`/`high`/`xhigh`) | Resident local 27B model with native image and tool input |
| `big-buddy` (`big-buddy-none`/`low`/`medium`/`high`/`xhigh`) | Opt-in local Flash-Next high-capacity runtime |
| `buddy-cloud`, `mentor`, `mistral` | Cloud-routed policies (explicitly marked in `GET /v1/models`) |

## 4. OpenAI Responses API (`POST /v1/responses`)

`POST /v1/responses` is the canonical inference contract.

### Python (`openai` SDK)

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.libraxis.com/v1",
    api_key=os.environ["LIBRAXIS_API_KEY"],
)

resp = client.responses.create(
    model="buddy-none",
    input=[
        {
            "role": "user",
            "content": [{"type": "input_text", "text": "List 3 post-op monitoring checks for a dog."}],
        }
    ],
)
print(resp.id, resp.output_text)
```

### Multi-turn chaining (`previous_response_id`)

- Store is enabled by default (`store: true`); pass `previous_response_id: resp.id` on follow-up turns.
- Set `store: false` for ephemeral non-stored turns.
- Retrieve stored results with `GET /v1/responses/{response_id}`, replay events with `GET /v1/responses/{response_id}/events`, inspect input items with `GET /v1/responses/{response_id}/input_items`, cancel in-flight work with `POST /v1/responses/{response_id}/cancel`, or delete stored state with `DELETE /v1/responses/{response_id}`.
- Estimate input token count via `POST /v1/responses/input_tokens` and compact long context via `POST /v1/responses/compact`.

### Streaming Formats

- **SSE (default for `stream: true`):** `Accept: text/event-stream`. Emits `response.created`, `response.in_progress`, `response.output_item.added`, `response.output_text.delta`, `response.completed`, terminated by `data: [DONE]`.
- **NDJSON:** `Accept: application/x-ndjson` or `?stream_format=ndjson`.
- **WebSocket:** `WS wss://api.libraxis.com/v1/responses`.

## 5. Vista Brain Veterinary Evidence Retrieval

1. **Check index readiness:** `GET /v1/vista-brain/index/status` → `{"status": "...", "ready": true, "provider": "...", "indexed_documents": ...}`.
2. **Bounded provenance search:** `POST /v1/vista-brain/search` with `{"query": "...", "limit": 5}` (`limit` 1..20). Returns `hits` with stable source provenance.
3. **Safety-gated clinical query:** `POST /v1/vista-brain/query` (JSON) or `POST /v1/vista-brain/query/stream` (SSE events: `reasoning`, `tool_call`, `tool_result`, `answer`, `done`, `error`).
4. **Document OCR:** `POST /v1/vista-brain/ocr` (multipart `pdf` or JSON `pdf_url`/`pdf_base64`) and `GET /v1/vista-brain/ocr/status`.
5. **Tools & async jobs:** `GET /v1/vista-brain/tools`, `POST /v1/vista-brain/tools/{tool_name}/execute`, `POST /v1/vista-brain/tools/{tool_name}/execute-async`, `GET /v1/vista-brain/jobs/{job_id}`.

## 6. Speech-to-Text & Audio

- **File transcription:** `POST /v1/audio/transcriptions` (multipart `file` or JSON `audio_data` base64; supports `language`, `response_format=json|verbose_json|text`, `timestamp_granularities[]`, `store`).
- **Transcribe + format:** `POST /v1/audio/transcriptions:format` (multipart `file`, optional `instruction`, `model`, `stream`).
- **NDJSON live streaming:** `POST /v1/audio/transcribe:stream` (`application/x-ndjson` frames `set`/`chunk`/`flush`/`end` → `hello`/`ack`/`transcript.final`/`stream.closed`).
- **Format transcript text:** `POST /v1/audio/format` (form fields `text`, `assistive`).
- **Text-to-Speech:** `POST /v1/audio/speech` (`input`, `voice`, `response_format=mp3|wav|opus|aac|flac|pcm`).

## 7. Anthropic Messages, Embeddings & Reranking

- **Anthropic Messages:** `POST /v1/messages` and `POST /v1/messages/count_tokens` (send `anthropic-version: 2023-06-01` or `2026-10-21`; `GET /v1/models` with `anthropic-version` returns Anthropic model objects).
- **Embeddings:** `POST /v1/embeddings` (`{"input": "..." | ["..."], "model": "..."}`).
- **Rerank:** `POST /v1/rerank` (`{"query": "...", "documents": ["..."], "top_k": 10}`).

## 8. Rate Limits & Error Handling

- Every response includes standard IETF headers:
  - `RateLimit-Limit`: quota for the active window
  - `RateLimit-Remaining`: remaining requests in the active window
  - `RateLimit-Reset`: seconds until window reset
  - `RateLimit-Policy`: `<quota>;w=<window_seconds>`
- Legacy `X-RateLimit-Limit`, `X-RateLimit-Remaining`, and `X-RateLimit-Reset` (Unix timestamp) are also returned for compatibility.
- On `HTTP 429`, read `Retry-After` (seconds) before retrying.
- All `/v1/*` errors return structured JSON (`error.type`, `error.code`, `error.message`, `error.param`) with an `X-Request-Id` correlation header.
