# Cruxy Learn: full corpus
> OpenAI-compatible LLM gateway for the Cruxy model tiers (canto, saga, and epos), plus embeddings, rerank, and content moderation behind one base URL (https://api.cruxy.ai) and one Bearer key.
Every documentation page, concatenated in navigation order. Each block below is fenced by a rule stating the page title, its canonical URL, and the markdown source it was rendered from. The per-page link index is at https://learn.cruxyai.com/llms.txt.
================================================================================
Page: Start here
URL: https://learn.cruxyai.com/learn/en/start-here
Markdown: https://learn.cruxyai.com/learn/en/start-here.md
================================================================================
# Start here
Two reading paths through the Cruxy API: the whole API in nine pages, and building an agent with tools, structured output, memory, and rules.
There are two paths here. Each step names one page and what you take from
it. The pages build on each other, so read a path in order the first time;
after that, each page stands on its own as a reference.
- [The API in nine pages](#the-api-in-nine-pages) goes from your first call
to running in production.
- [Build an agent](#build-an-agent) is for when the model has to act: call
your functions, return data your code can parse, and remember.
## The API in nine pages
1. **Make your first call**
[Quickstart](/docs/en/get-started/quickstart) takes you from an empty
terminal to your first model response: you get an API key with device login,
then send a chat request to the `canto` tier.
2. **Choose a tier**
[Choosing a tier](/docs/en/build/choosing-a-tier) compares the tiers you pass
as `model` (`canto`, `saga`, and `epos`) by profile, output cap, and how each
one reasons by default, and shows how to read the live catalog from
`GET /api/v1/models` instead of hardcoding it.
3. **Hold a conversation**
[Chat](/docs/en/build/chat) documents `POST /api/v1/chat`, the
OpenAI-compatible chat completion: the request options, a single JSON
completion or a Server-Sent Events stream, the `cruxy` block, and usage.
4. **Let the model call your functions**
[Tutorial: build a tool-using agent](/docs/en/build/tool-agent) builds on the
same endpoint. You describe two functions, run the calls the model returns,
send the results back, and loop on `finish_reason` until the model answers.
5. **Answer from your own documents**
[Retrieval-augmented generation](/docs/en/resources/cookbook/rag) is a
cookbook recipe that chains three endpoints: embeddings for a first-pass
search, rerank to sharpen the shortlist, and chat to write the answer.
6. **Screen content**
[Moderations](/docs/en/build/moderations) checks text against the
content-safety categories with the `cruxy-guard` model and returns an
overall `flagged` result.
7. **Handle failures**
[Errors and retries](/docs/en/build/errors-and-retries) defines the error
envelope, lists every error code, and says which failures to retry and how
long to wait, so your client branches on the stable `code`, never on the
wording of the message.
8. **Run it reliably**
[Reliability](/docs/en/build/reliability) puts the production rules in one
place: the per-tier deadlines, which failures to retry, `Idempotency-Key`, how
to tell a finished stream from a failed one, and where to check platform
status.
9. **Know the limits**
[Known limitations](/docs/en/build/known-limitations) lists what the API does
not do today, such as reporting a reasoning-token count or running a
non-streaming call past 90 seconds, and the workaround for each.
## Build an agent
An agent is a loop: the model asks for a function call, your code runs it and
sends the result back, and the model continues until it has an answer. This
path assumes you have made a chat call; if not, start with the first three
steps above.
1. **Describe your functions**
[Define tools](/docs/en/build/tools) shows how to describe each function in
the `tools` array with a JSON Schema for its arguments, how `tool_choice`
controls when the model calls one, and why tools are billed as input.
2. **Run the calls the model returns**
[Handle tool calls](/docs/en/build/tool-calls) covers the other half: read
the tool calls on the response, run each function, send every result back by
`tool_call_id`, and repeat until `finish_reason` is `stop`.
3. **Put the loop together**
[Tutorial: build a tool-using agent](/docs/en/build/tool-agent) builds a
working TypeScript agent from those two pages: two tools, their handlers, and
the loop on `finish_reason`, with the reasons the loop is shaped the way it
is.
4. **Get answers your code can parse**
[Ask for structured output](/docs/en/best-practices/prompting#ask-for-structured-output)
explains `response_format`: send a JSON schema to constrain the output, read
which tiers support it from `GET /api/v1/models`, and handle output that
does not match.
5. **Let the agent remember**
[Memory](/docs/en/build/memory) lets chat recall stored facts and capture new
ones. It is off for a developer API key until a request opts in, so read
[Opt in from a developer key](/docs/en/build/memory#opt-in-from-a-developer-key)
before you rely on it.
6. **Give it standing instructions**
[Standing rules](/docs/en/build/rules) injects your organization's rules into
chat as defaults. Like memory, rules are off for a developer key unless you
opt in, per request or once on the key.
7. **Make the loop survive failures**
[Reliability](/docs/en/build/reliability) matters more in a loop, where one
failed request stalls the whole task: the per-tier deadlines, which failures
to retry, `Idempotency-Key`, and how to tell a finished stream from a failed
one.
## Next steps
[Cookbook](/docs/en/resources/cookbook) has more recipes that combine
endpoints to finish a real job, and [API Reference](/docs/en/reference/api)
lists every endpoint with its exact request and response shape.
Working from a terminal instead?
[Get started with the CLI](/docs/en/cruxy-code) installs Cruxy Code, the
`cruxy` coding agent, and signs it in with the same device login.
Moving code that already calls OpenAI?
[Migrate from OpenAI](/docs/en/migrate/from-openai) changes the base URL, the
key, and `model`, and
[OpenAI SDK compatibility](/docs/en/migrate/openai-sdk-compatibility) says what
carries over.
================================================================================
Page: One gateway for every model tier
URL: https://learn.cruxyai.com/docs/en
Markdown: https://learn.cruxyai.com/docs/en.md
================================================================================
# One gateway for every model tier
An OpenAI-compatible LLM gateway for the Cruxy model tiers: chat, embeddings, and moderation behind one API key.
## Choose how you build
Two ways into the same gateway: call the endpoints directly, or start from a
pattern and copy what fits.
- [Chat](/docs/en/build/chat)
- [Embeddings](/docs/en/build/embeddings)
- [Rerank](/docs/en/build/rerank)
- [Moderations](/docs/en/build/moderations)
- [Feedback](/docs/en/build/feedback)
- [API Reference](/docs/en/reference/api)
- [Cookbook](/docs/en/resources/cookbook)
- [API guide for agents](/docs/en/resources/api-guide)
- [Glossary](/docs/en/resources/glossary)
- [Changelog](/docs/en/reference/changelog)
## From key to production
Every integration walks the same path; each stage links into the guide that
covers it. To read it as one ordered path, follow
[Start here](/learn/en/start-here).
- [Features overview](/docs/en/get-started/features)
- [Quickstart](/docs/en/get-started/quickstart)
- [Get your API key](/docs/en/get-started/api-keys)
- [Authentication](/docs/en/get-started/authentication)
- [Chat](/docs/en/build/chat)
- [Streaming](/docs/en/build/streaming)
- [Define tools](/docs/en/build/tools)
- [Embeddings](/docs/en/build/embeddings)
- [Rerank](/docs/en/build/rerank)
- [Moderations](/docs/en/build/moderations)
- [Models](/docs/en/reference/models)
- [Errors and retries](/docs/en/build/errors-and-retries)
- [Rate limits and budgets](/docs/en/build/rate-limits)
- [Production checklist](/docs/en/best-practices/production-checklist)
- [Feedback](/docs/en/build/feedback)
- [API Reference](/docs/en/reference/api)
## Pick a model
You pass a tier id as `model` (or `auto` to let the router choose), and the
gateway routes the call. The tier is the contract; the upstream model behind it
is never exposed. [Choosing a tier](/docs/en/build/choosing-a-tier) compares
them, and [Models](/docs/en/reference/models) documents every id and alias.
- `canto` (Deep): Deepest reasoning, text only; reach for it on the hardest tasks.
- `saga` (Balanced): Strong quality at lower latency and cost, a sensible default for everyday calls.
- `epos` (Fast): Lowest latency and cost for short, high-volume calls; the budget-downgrade target.
## Start from a recipe
Four working patterns that combine the endpoints to do a real job; copy the
one shaped like yours. [Cookbook](/docs/en/resources/cookbook) indexes them
all.
## Start building
One key, one endpoint, three tiers behind it.
================================================================================
Page: Features overview
URL: https://learn.cruxyai.com/docs/en/get-started/features
Markdown: https://learn.cruxyai.com/docs/en/get-started/features.md
================================================================================
# Features overview
What the Cruxy gateway gives you: OpenAI-compatible chat on three tiers, specialized model endpoints, and memory and rules you control.
Cruxy is an OpenAI-compatible LLM gateway. You call one API at
`https://api.cruxy.ai` with one key and name a model tier instead of a provider
model. Cruxy-native fields are additive and namespaced under a `cruxy` key, so
an OpenAI client keeps parsing the response and ignores what it does not know.
## Three tiers and auto
You pass a tier id as the chat `model`. The gateway resolves the tier to an
upstream model on the server; an upstream provider model is never accepted or
returned, so the tier is the stable contract your code depends on.
| Tier | Profile | Default reasoning | Reasoning control |
| --- | --- | --- | --- |
| `canto` | Deep | `high` | Graded: `low`, `medium`, and `high` are different depths. |
| `saga` | Balanced | `low` | Graded: `low`, `medium`, and `high` are different depths. |
| `epos` | Fast | `none` | Graded: `low`, `medium`, and `high` are different depths. |
The reasoning columns are the shipped defaults. `GET /api/v1/models` reports
each tier's live `reasoning.default_effort` and `reasoning.graded`, along with
its context window, output cap, and pricing; [Models](/docs/en/reference/models)
shows how to read the catalog at runtime, and
[Choosing a tier](/docs/en/build/choosing-a-tier) compares the tiers.
A reasoning tier thinks inside the same `max_tokens` budget as its answer, and
the thinking is not returned as content. A small `max_tokens` on `canto` can be
spent entirely on thinking, ending with `finish_reason: "length"` and empty
content. Send `reasoning_effort: "none"` for a short, direct answer, or leave
`max_tokens` unset to keep the tier's cap; see
[Reasoning effort](/docs/en/build/reasoning-effort).
Send `model: "auto"` to let the server-side router pick the tier from the
request. It resolves to a concrete tier before billing and before the upstream
call, and the tier that served the request comes back as `cruxy.routed_tier`.
An `auto` request may be budget-downgraded to `epos`, so read `routed_tier`
rather than assuming it; [Auto routing](/docs/en/build/auto-routing) covers the
router, and [Rate limits and budgets](/docs/en/build/rate-limits) explains the
budgets behind the downgrade.
## What you can call
| Area | Endpoints | What it does |
| --- | --- | --- |
| [Chat](/docs/en/build/chat) | `POST /api/v1/chat` | OpenAI-compatible chat with Cruxy features: [Streaming](/docs/en/build/streaming), tool calling (see [Define tools](/docs/en/build/tools)), [Memory](/docs/en/build/memory), [Standing rules](/docs/en/build/rules), and attached files. |
| Native chat | `POST /api/v2/chat` | The Cruxy-native wire: named SSE events that always end in a `finish` or `error` frame. |
| OpenAI wire | `POST /v1/chat/completions`, `GET /v1/models` | The plain OpenAI protocol for OpenAI clients and evaluation harnesses, with none of the Cruxy-native features. |
| [Models](/docs/en/reference/models) | `GET /api/v1/models` | The tier catalog. |
| Token counting | `POST /api/v1/tokens/count` | The input tokens a chat request would bill, counted without inference and not billed. |
| [Embeddings](/docs/en/build/embeddings) | `POST /api/v1/embeddings` | 1024-dimension vectors from `cruxy-embed`. |
| [Rerank](/docs/en/build/rerank) | `POST /api/v1/rerank` | Documents ranked against a query by `cruxy-rerank`. |
| [Moderations](/docs/en/build/moderations) | `POST /api/v1/moderations` | Content classification by `cruxy-guard`. |
| [Feedback](/docs/en/build/feedback) | `POST /api/v1/feedback` | A signal about an earlier call, joined to it by `request_id`. |
| [Files](/docs/en/build/files) | `/api/v1/files` | Uploads that a chat message attaches with `file_ids`; chat reads text, PDF, and Word (`.docx`) files, and images on a tier that reads them. |
| [Conversations](/docs/en/build/conversations) | `/api/v1/conversations` | Conversation history stored server-side, private to its author, with search and branches. |
| [Memory](/docs/en/build/memory) | `/api/v1/memories` | Stored facts, personal, organization-wide, or shared in a vault, that chat can recall. |
| [Vaults](/docs/en/build/vaults) | `/api/v1/vaults` | Shared spaces in an organization, with members, memory, rules, and files. |
| Rules | `/api/v1/rules`, `/api/v1/rules/resolved` | Standing instructions for chat, set for your organization or a vault. |
| [Prompts](/docs/en/build/prompts) | `/api/v1/prompts` | Named prompts with immutable versions. |
| [Evals](/docs/en/build/evals) | `/api/v1/evals/suites` | Test suites run against a tier; every case is a billed completion. |
| Account | `/api/v1/me`, `/api/v1/profile`, `/api/v1/limits`, `/api/v1/limits/month`, `/api/v1/limits/plans` | Your identity, profile, live limits, the month so far by day, and every plan's entitlements; see [Plan limits](/docs/en/build/plan-limits). |
| [Organization](/docs/en/build/organization) | `/api/v1/org/members`, `/api/v1/workspaces`, `/api/v1/logs` | Members, invitations, workspaces with spend caps, and request-log metadata. |
| Sign-in | `/api/v1/device/code`, `/api/v1/device/token` | Device login, which issues API keys; see [Get your API key](/docs/en/get-started/api-keys). |
| Status | `GET /api/status`, `GET /api/health`, `GET /api/ready` | Public platform status and health probes. |
Every `/api/v1` endpoint is also served under `/api/v2` with identical
behavior, and this site documents the `/api/v1` path. Chat is the one
exception: `POST /api/v2/chat` is a different wire from `POST /api/v1/chat`.
[API Reference](/docs/en/reference/api) has every operation with its exact
request and response shape.
## Memory and standing rules
Two chat features add context beyond the messages you send. You control both
per request, and their defaults depend on how the key was issued:
- **On by default** for a signed-in credential: what a person gets by signing
in to the Cruxy web app, desktop, the CLI, or mobile. A key minted by device
login or OAuth login is a signed-in credential whichever client ran the flow,
a third-party tool included.
- **Off by default** for a developer API key or a headless key, which opts in
per request.
`GET /api/v1/limits` reports which kind a key is as `bucket`: `subscription` is
a signed-in credential, and `apikey` and `headless` are not. A server, or
anything that serves people other than the key's owner, should use a developer
API key; see [Get your API key](/docs/en/get-started/api-keys#two-kinds-of-key).
To get the same behavior from any key, set both on every request.
### Memory
Memory has two independent sides, each set in the request's `memory` object:
- **Recall** (`recall`) adds the stored memories most relevant to the turn as
background context and reports them in `cruxy.memory_recall`: the scope and
key of each, never the stored value.
- **Capture** (`capture`) lets the gateway store a durable fact from the turn,
such as a stated preference, and reports it in `cruxy.memory_capture`. No
field means nothing was stored. Captures go to your personal memory, or to a
vault you can write when the request names one, never to organization-wide
memory. Content carrying an email address, phone number, or card number is
dropped before any write.
`true` needs a key whose allowed scopes include memory; otherwise the request
fails with `403` `key_scope_forbidden`. `false` needs no scope. Pin
`Cruxy-Version: 2026-09-26` on every request that relies on `false`, so a
gateway that predates the field refuses the request instead of ignoring it.
`GET /api/v1/memories` lists what is stored, and
`DELETE /api/v1/memories/{id}` removes an entry. [Memory](/docs/en/build/memory)
covers recall, capture, and the scope a key needs.
### Standing rules
Standing rules are instructions your organization sets for chat, and a vault
can have its own. They are injected into the turn as defaults that the user's
own message overrides. A developer API key gets them only when the request sends
`rules: true`, or when the key's allowed scopes name the rules family. Any key
may send `rules: false` to decline them for a turn.
When rules apply, the response carries `cruxy.rules_applied` with counts and
sources, never the rule text. `GET /api/v1/rules/resolved` returns the exact
rules a turn would get. Injected rule text counts as input tokens.
[Standing rules](/docs/en/build/rules) covers opting in and what gets
injected.
This request recalls memory, stores nothing new, and declines standing rules:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-H "Cruxy-Version: 2026-09-26" \
-d '{
"model": "saga",
"stream": false,
"memory": { "recall": true, "capture": false },
"rules": false,
"messages": [
{ "role": "user", "content": "Which time zone should my meeting use?" }
]
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
'Cruxy-Version': '2026-09-26',
},
body: JSON.stringify({
model: 'saga',
stream: false,
memory: { recall: true, capture: false },
rules: false,
messages: [{ role: 'user', content: 'Which time zone should my meeting use?' }],
}),
})
const completion = await res.json()
console.log(completion.message.content)
console.log(completion.cruxy.memory_recall) // undefined when nothing was recalled
```
## Versioning
The API is versioned by date. Send `Cruxy-Version: YYYY-MM-DD` to pin a
version, or omit it for the server's current default; an unknown or malformed
value is rejected with `400`. Every response, errors included, echoes the
resolved version in its `Cruxy-Version` header.
A breaking change, such as a change to streaming frame order, ships under a new
dated version. Additive fields, including new `cruxy.*` fields, ship without
one, so ignore JSON fields you do not recognize.
[Changelog](/docs/en/reference/changelog) records what changed.
## Next steps
- [Get your API key](/docs/en/get-started/api-keys): mint the key every call needs.
- [Quickstart](/docs/en/get-started/quickstart): make your first chat call.
- [Streaming](/docs/en/build/streaming): read a chat completion as it is written.
- [Errors and retries](/docs/en/build/errors-and-retries): handle failures.
================================================================================
Page: Quickstart
URL: https://learn.cruxyai.com/docs/en/get-started/quickstart
Markdown: https://learn.cruxyai.com/docs/en/get-started/quickstart.md
================================================================================
# Quickstart
Get an API key with device login, then make your first chat call.
You'll go from an empty terminal to your first model response against the
`canto` tier. Every request in this guide goes to `https://api.cruxy.ai`.
## 1. Start device login
Ask the gateway for a device code. This endpoint needs no authentication.
```bash
curl -X POST https://api.cruxy.ai/api/v1/device/code
```
You get back a `user_code` to type, a URL to approve it, and the
`device_code` your client polls with.
```json
{
"device_code": "cxy_dev_QK3f...redacted",
"user_code": "WDJB-MJHT",
"verification_uri": "https://cruxy.ai/device",
"verification_uri_complete": "https://cruxy.ai/device?user_code=WDJB-MJHT",
"expires_in": 600,
"interval": 5
}
```
## 2. Approve in the browser
Open `verification_uri_complete` and approve the request. The
`user_code` (`WDJB-MJHT`) is pre-filled; if you open the plain
`verification_uri` instead, type it in by hand. You have `expires_in`
seconds (600 = ten minutes) to finish.
## 3. Poll for the key
Poll the token endpoint with your `device_code`, waiting `interval`
seconds (5) between attempts.
```bash
curl -X POST https://api.cruxy.ai/api/v1/device/token \
-H "Content-Type: application/json" \
-d '{ "device_code": "cxy_dev_QK3f...redacted" }'
```
> **Note:** Until someone approves, you get `400` with an `authorization_pending`
> code. That is expected; keep polling.
```json
{
"code": "authorization_pending",
"error": "authorization_pending",
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
}
```
Once approved, the same request returns `200` with your key.
> **Warning:** The key is shown exactly once; there is no way to retrieve it again.
> Copy it now and store it before you move on.
```json
{
"access_token": "cxy_live_...redacted",
"token_type": "bearer",
"key_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"user_id": "6f9c1e2a-1b2c-4d5e-8f90-1a2b3c4d5e6f",
"expires_at": "2026-10-20T12:00:00.000Z",
"org": {
"id": "8c1a5b3e-2d4f-4a6b-9c8d-0e1f2a3b4c5d",
"name": "Acme Corp",
"role": "member"
}
}
```
Export it into your shell. Never commit a key to source control.
```bash
export CRUXY_API_KEY="cxy_live_..."
```
For the full flow (every polling error code, verifying the key, and key
hygiene), see [Get your API key](/docs/en/get-started/api-keys).
## 4. Make your first call
Send a prompt to the `canto` tier. `stream: false`, which is also the default,
asks for a single JSON response instead of a stream, so there is one block to
read on a first call.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "canto",
"stream": false,
"messages": [
{ "role": "user", "content": "Hello" }
]
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'canto',
stream: false,
messages: [{ role: 'user', content: 'Hello' }],
}),
})
const completion = await res.json()
console.log(completion)
```
The response is one JSON object: the reply is the top-level `message`, with
`finish_reason` and `usage` beside it, and the call's cost is `usage.cost`.
Cruxy adds a namespaced `cruxy` block carrying the `request_id`, the
`routed_tier`, and the `reasoning_effort`. See
[Chat](/docs/en/build/chat) for the streaming form and the full request options.
## Where to go next
- [Chat](/docs/en/build/chat): Streaming SSE, tiers, temperature, and the cruxy block.
- [Get your API key](/docs/en/get-started/api-keys): The device-login flow end to end, the key format, and key hygiene.
- [API Reference](/docs/en/reference/api): Every endpoint, typed and interactive.
- [TypeScript SDK](/docs/en/sdk/typescript): The typed client for these endpoints: install @cruxy/api and call them as methods.
================================================================================
Page: Get your API key
URL: https://learn.cruxyai.com/docs/en/get-started/api-keys
Markdown: https://learn.cruxyai.com/docs/en/get-started/api-keys.md
================================================================================
# Get your API key
Pick the right kind of key, mint a signed-in key with device login, verify it with GET /api/v1/me, and know when it expires.
Every authenticated request sends a `cxy_live_` user key, and a user key is one
of two kinds. A signed-in credential comes from device login and belongs in a
person's own tool, such as a CLI or a desktop app. A developer API key is
created in the Cruxy Console and belongs on a server.
> **Warning:** Device login gives you a signed-in credential, with memory and standing rules on
> by default. For a server, or anything that serves people other than you, use a
> developer API key from the Cruxy Console. A signed-in key expires 90 days after
> it is issued; revoke it yourself when you sign out.
## Two kinds of key
| | Signed-in credential | Developer API key |
| --- | --- | --- |
| How you get it | Device login, or native OAuth login, whichever client runs the flow. | Created in the Cruxy Console. |
| Use it for | A person's own tool, such as a CLI or a desktop app. | A server, and anything that serves people other than the key's owner. |
| Memory and rules | On by default. | Off unless a request opts in. |
| Expires | At `expires_at`, 90 days after issue. Use does not renew it. | Never. |
| Rotate or revoke | Revoke it yourself with `DELETE /api/v1/me/key`. It cannot be rotated. | In the Console. |
| Spend limits | A monthly spend cap. | The Console can limit it to chosen models and a monthly spend cap. |
| Limits bucket | `subscription` | `apikey` |
A signed-in credential on a server would recall its owner's memory into other
people's turns, store what they say in that memory, and apply the owner's
organization rules, unless every request sends
`memory: {"recall": false, "capture": false}` and `rules: false`. A headless
key, billed from prepaid credit, has memory and rules off by default, like a
developer API key. To see which kind a key is, read `bucket` on
`GET /api/v1/limits`; see [Rate limits and budgets](/docs/en/build/rate-limits#read-your-limits).
A developer API key needs a paid plan, and the API can use up to 25% of the
plan's usage; see [Plan limits](/docs/en/build/plan-limits).
## Device login
Cruxy issues signed-in credentials through an [RFC 8628][rfc]
device-authorization flow: your client asks for a code, a person approves it in
a browser, and your client polls until the key is minted. The browser and the
client can be different machines.
[rfc]: https://datatracker.ietf.org/doc/html/rfc8628
The two device-login endpoints take no key and no `Authorization` header: their
job is to issue one. Both are rate-limited and return `429` with
`rate_limited` if you call them too often.
1. **Request a device code**
`POST /api/v1/device/code` with no body.
```bash title="curl"
curl -X POST https://api.cruxy.ai/api/v1/device/code
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/device/code', {
method: 'POST',
})
if (!res.ok) throw new Error(`device code request failed: ${res.status}`)
const device = await res.json()
console.log(`Open ${device.verification_uri_complete} to approve.`)
console.log(`Confirm this code on that page: ${device.user_code}`)
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
// Device login takes no key.
const cruxy = new Cruxy()
const { result: device } = await cruxy.deviceLogin.startDeviceLogin()
console.log(`Open ${device.verificationUriComplete} to approve.`)
console.log(`Confirm this code on that page: ${device.userCode}`)
```
```json
{
"device_code": "cxy_dev_QK3f...redacted",
"user_code": "WDJB-MJHT",
"verification_uri": "https://cruxy.ai/device",
"verification_uri_complete": "https://cruxy.ai/device?user_code=WDJB-MJHT",
"expires_in": 600,
"interval": 5
}
```
| Field | Meaning |
| --- | --- |
| `device_code` | The code your client polls with. The person approving never needs it. |
| `user_code` | The short code the person confirms in the browser. |
| `verification_uri` | The approval page. The person types `user_code` there. |
| `verification_uri_complete` | The approval page with `user_code` already filled in. |
| `expires_in` | Seconds until `device_code` expires. |
| `interval` | Seconds to wait between polls. |
Read `verification_uri`, `expires_in`, and `interval` from each response
rather than hardcoding the example values.
2. **Approve in the browser**
Send the person to `verification_uri_complete`, or to `verification_uri` with
the `user_code` to type. They approve the request before
`expires_in` seconds pass. If they decline, polling returns `access_denied`.
3. **Poll for the key**
`POST /api/v1/device/token` with the `device_code`, waiting `interval` seconds
between attempts. Polling faster than the limiter allows returns `429` with
`rate_limited`; wait the `Retry-After` seconds, then resume at `interval`.
```bash title="curl"
# DEVICE_CODE holds the device_code from step 1.
curl -X POST https://api.cruxy.ai/api/v1/device/token \
-H "Content-Type: application/json" \
-d "{\"device_code\": \"$DEVICE_CODE\"}"
```
```ts title="TypeScript"
// `device` is the response from step 1.
const sleep = (seconds: number) => new Promise((r) => setTimeout(r, seconds * 1000))
async function pollForKey(device: { device_code: string; interval: number; expires_in: number }) {
const deadline = Date.now() + device.expires_in * 1000
while (Date.now() < deadline) {
await sleep(device.interval)
const res = await fetch('https://api.cruxy.ai/api/v1/device/token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ device_code: device.device_code }),
})
if (res.ok) return res.json()
if (res.status === 429) {
// Polling faster than the limiter allows: honour Retry-After, then resume.
await sleep(Number(res.headers.get('Retry-After')) || device.interval)
continue
}
const { code, reason } = await res.json()
if (code === 'authorization_pending') continue
throw new Error(`device login stopped: ${code}${reason ? ` (${reason})` : ''}`)
}
throw new Error('device_code expired before approval; start a new device login')
}
const key = await pollForKey(device)
// Store key.access_token in your secrets manager now. Log the id, never the key.
console.log(`Minted key ${key.key_id} for ${key.org.name}, expires ${key.expires_at}`)
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
import type { DeviceCodeResponse } from '@cruxy/api/models'
import { CruxyError, DeviceTokenPendingResponseError } from '@cruxy/api/models/errors'
// `device` is the result of step 1. Device login takes no key.
const cruxy = new Cruxy()
const sleep = (seconds: number) => new Promise((r) => setTimeout(r, seconds * 1000))
async function pollForKey(device: DeviceCodeResponse) {
const interval = device.interval
const deadline = Date.now() + device.expiresIn * 1000
while (Date.now() < deadline) {
await sleep(interval)
try {
const { result } = await cruxy.deviceLogin.pollDeviceToken({
body: { deviceCode: device.deviceCode },
})
return result
} catch (err) {
// Polling faster than the limiter allows: honour Retry-After, then resume.
if (err instanceof CruxyError && err.statusCode === 429) {
await sleep(err.retryAfter ?? interval)
continue
}
if (err instanceof DeviceTokenPendingResponseError && err.code === 'authorization_pending') {
continue
}
// Refused for good: access_denied, expired_token, or invalid_grant.
// err.reason refines it, such as org_membership_revoked.
throw err
}
}
throw new Error('device_code expired before approval; start a new device login')
}
const key = await pollForKey(device)
// Store key.accessToken in your secrets manager now. Log the id, never the key.
console.log(`Minted key ${key.keyId} for ${key.org.name}, expires ${key.expiresAt.toISOString()}`)
```
Until the flow resolves, the endpoint returns `400` with an OAuth-style body
carrying `code`, `error`, and `request_id`:
```json
{
"code": "authorization_pending",
"error": "authorization_pending",
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
}
```
Only `authorization_pending` means keep polling:
| `code` | Meaning | What to do |
| --- | --- | --- |
| `authorization_pending` | Not approved yet. | Wait `interval` seconds and poll again. |
| `access_denied` | The person declined. | Stop. |
| `expired_token` | The `device_code` expired. | Start a new device login. |
| `invalid_grant` | The `device_code` is not valid. | Start a new device login. |
A refusal can also carry `reason` and `error_description`. The one `reason`
today is `org_membership_revoked`: the login was approved for an organization
the account no longer belongs to. A new login succeeds, but it may bind to a
different organization, so tell the person instead of retrying silently.
Branch on `reason`; `error_description` is for people and is not meant to be
parsed.
On approval the endpoint returns `200` with the key:
```json
{
"access_token": "cxy_live_...redacted",
"token_type": "bearer",
"key_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"user_id": "6f9c1e2a-1b2c-4d5e-8f90-1a2b3c4d5e6f",
"expires_at": "2026-10-20T12:00:00.000Z",
"org": {
"id": "8c1a5b3e-2d4f-4a6b-9c8d-0e1f2a3b4c5d",
"name": "Acme Corp",
"role": "member"
}
}
```
> **Warning:** The key is returned exactly once. Store it before you do anything else.
## What the token response tells you
| Field | Meaning |
| --- | --- |
| `access_token` | Your API key. Send it as `Authorization: Bearer `. |
| `token_type` | `bearer`. |
| `key_id` | The key's id. Safe to log. |
| `user_id` | The account the key belongs to. |
| `expires_at` | When the key stops working, 90 days after issue. Record it with the key; see [Expiry and revocation](#expiry-and-revocation). |
| `org` | The one organization this key is bound to: its `id`, `name`, and your `role` in it (`owner`, `admin`, or `member`). |
A key is bound to exactly one organization, fixed when it is issued. That
organization decides which shared memory, rules, vaults, and conversations the
key can reach. A login is not always bound to the same organization as your
previous one, so show `org.name` to the person after every login, and read
`org.role` rather than inferring authority from anything else.
## Key format
| Prefix | What it is |
| --- | --- |
| `cxy_live_` | A user key, either kind: what you send as a Bearer token. |
| `cxy_svc_` | A service token, minted by an operator. It is not a user key, and an endpoint that expects one rejects the other with `401`. |
Treat everything after the prefix as opaque.
## Verify the key
`GET /api/v1/me` echoes the identity behind a key. Call it once after you store
a new key to confirm it works end to end.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/me \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/me', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
if (!res.ok) throw new Error(`key check failed: ${res.status}`)
const me = await res.json()
console.log(`key ${me.api_key_id} belongs to user ${me.user_id}`)
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
// Throws a CruxyError with status 401 when the key does not work.
const { result: me } = await cruxy.auth.getCurrentIdentity()
console.log(`key ${me.apiKeyId} belongs to user ${me.userId}`)
```
```json
{
"api_key_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"user_id": "6f9c1e2a-1b2c-4d5e-8f90-1a2b3c4d5e6f"
}
```
A `200` means the key works. A `401` `unauthorized` means it is missing,
malformed, unknown, expired, or revoked: the same answer for each. Check the
`Authorization` header, then the key itself, then its `expires_at`.
## Expiry and revocation
- **A signed-in credential stops working at `expires_at`,** 90 days after
issue. The time is fixed when the key is issued: use does not extend it, and
there is no refresh grant. Prompt for a new device login before it passes.
- **A developer API key does not expire.** Rotate or revoke it in the Console.
- **Any key can revoke itself** with `DELETE /api/v1/me/key`, a signed-in
credential included. Call it when a person signs out of your tool; see
[Revoke the current key](#revoke-the-current-key). A signed-in credential
cannot be rotated, so otherwise it ends at `expires_at`, when its
organization membership ends, or when Cruxy support revokes it.
- **An expired, revoked, or unknown key is `401` `unauthorized`,** with
`WWW-Authenticate: Bearer`. A key whose owner's account is suspended is `403`
`account_suspended`.
- **Revocation applies from the next request,** since nothing is cached. A
stream already running is not cut.
## Revoke the current key
`DELETE /api/v1/me/key` revokes the key that authenticates the request, and
only that key. It takes no key id, so it cannot reach any other credential:
other keys of the same account stay live, and usage already recorded against
this key is kept. Call it when a person signs out, so the key stops working
now rather than at `expires_at`.
```bash title="curl"
curl -X DELETE https://api.cruxy.ai/api/v1/me/key \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/me/key', {
method: 'DELETE',
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
// 204: revoked. 401: the key was already unusable. Either way it is signed out.
if (res.status !== 204 && res.status !== 401) {
throw new Error(`revoke failed: ${res.status}`)
}
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
// Resolves with the response headers (there is no body) once the key is revoked.
await cruxy.auth.revokeCurrentKey()
```
| Status | Meaning |
| --- | --- |
| `204` | Revoked. The key stops authenticating as soon as this returns. |
| `401` `unauthorized` | The key is missing, malformed, unknown, expired, or already revoked, so it is already unusable. A repeat of a successful revoke gets this. |
| `403` `key_scope_forbidden` | The key's allowed scopes exclude the `account` family. |
| `403` `key_read_only` | The key is read-only, and revoking is a change. |
Treat `204` and `401` as signed out, then delete the stored key. Neither `403`
clears by waiting: revoke a developer API key in the Console instead. The
revoke is recorded in your organization's audit trail.
## What a key is allowed to do
A key can carry restrictions. A request outside them is refused:
| Restriction | Refusal |
| --- | --- |
| Allowed scopes: the endpoint families the key may call, and whether it may use memory, rules, or files | `403` `key_scope_forbidden`, naming `required_scope` when a request field asked for a family the key lacks |
| Model allowlist: the tiers the key may call | `403` `model_not_allowed` |
| Read-only: the key may not perform billable operations | `403` `key_read_only` |
| Monthly spend cap | `429` `key_spend_cap_exceeded` |
A `403` never clears by waiting; re-scope the key or issue a new one. For a
developer API key, the Console sets the model allowlist and the monthly spend
cap.
Memory recall, memory capture, and your organization's standing rules are on by
default for a signed-in credential, which includes every key device login
mints, and off by default for a developer API key. To get the same behavior from
any key, set `memory` and `rules` on each chat request; see
[Memory](/docs/en/build/memory) and [Standing rules](/docs/en/build/rules).
## Keep the key safe
- **Treat it like a password.** Anyone who holds it can call the API as you.
- **Keep it on the server.** Never ship a key to a browser. Put a server route
between the browser and the gateway that adds the key; see
[Streaming chat in the browser](/docs/en/resources/cookbook/streaming-ui).
- **Read it from the environment.** Keep it in an environment variable or a
secrets manager, never in source control. Every sample on this site reads
`CRUXY_API_KEY`.
- **Use a developer API key on a server.** A signed-in credential carries its
owner's memory and rules into every request, and expires.
- **Revoke a signed-in credential on sign-out.** Call `DELETE /api/v1/me/key`
before you delete the stored key, so a copy left behind stops working too.
- **Store it the moment it is issued.** The token endpoint returns it once; if
you lose it, run device login again for a new key.
- **Log the `key_id`, never the key.** `key_id` and `expires_at` identify a key
and tell you when to replace it.
- **Use one key per environment.** Separate keys for local, staging, and
production keep each `key_id` meaningful in your logs.
- **Issue the narrowest key a service needs.** A key whose allowed scopes omit
memory can never read or write anyone's memory through chat.
- **Send it only to `https://api.cruxy.ai`.**
## Next steps
- [Authentication](/docs/en/get-started/authentication): send the key on every request.
- [Quickstart](/docs/en/get-started/quickstart): make your first chat call.
- [Errors and retries](/docs/en/build/errors-and-retries): handle `401`, `403`, and every other error code.
================================================================================
Page: Authentication
URL: https://learn.cruxyai.com/docs/en/get-started/authentication
Markdown: https://learn.cruxyai.com/docs/en/get-started/authentication.md
================================================================================
# Authentication
Send your Cruxy API key as a Bearer token in the Authorization header on every authenticated request.
Every authenticated endpoint reads one header: `Authorization: Bearer`
followed by your `cxy_live_` key. To mint a key, confirm it works, and store it
safely, see [Get your API key](/docs/en/get-started/api-keys).
## The Authorization header
Send the key on every authenticated request:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/me \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/me', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
console.log(res.status, await res.json())
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
const { result } = await cruxy.auth.getCurrentIdentity()
console.log(result)
```
The same key and header work on the OpenAI-compatible routes,
`POST /v1/chat/completions` and `GET /v1/models`: point an OpenAI client at the
base URL `https://api.cruxy.ai/v1` and give it your Cruxy key as its API key.
[Migrate from OpenAI](/docs/en/migrate/from-openai) covers the rest of the
switch.
## Endpoints that take no key
These endpoints are public:
| Endpoint | Why it is public |
| --- | --- |
| `POST /api/v1/device/code`, `POST /api/v1/device/token` | Device login issues keys. |
| `POST /api/v1/auth/oauth/start`, `POST /api/v1/auth/oauth/token` | Native sign-in issues keys. |
| `GET /api/status`, `GET /api/health`, `GET /api/ready` | Platform status and health probes. |
## When the key is rejected
A `401` `unauthorized` means the key is missing, malformed, unknown, expired,
or revoked: the same answer for each. A signed-in key stops working at its
`expires_at`; see [Expiry and revocation](/docs/en/get-started/api-keys#expiry-and-revocation).
Check the `Authorization` header, then
[verify the key](/docs/en/get-started/api-keys#verify-the-key).
A `403` means the key is valid but its authority does not cover the call:
| `code` | Meaning |
| --- | --- |
| `key_scope_forbidden` | The key is not scoped for this endpoint family, or for the memory, rules, or files feature a request field asked for. |
| `model_not_allowed` | The key's model allowlist excludes the requested tier. |
| `key_read_only` | The key may not perform billable operations. |
A `403` never clears by waiting: re-scope the key or issue a new one.
[Errors and retries](/docs/en/build/errors-and-retries) lists every error code.
## Next steps
- [Get your API key](/docs/en/get-started/api-keys): device login, key format, and key hygiene.
- [Chat](/docs/en/build/chat): make an authenticated chat call.
- [API Reference](/docs/en/reference/api): which endpoints require a key.
================================================================================
Page: Migrate from OpenAI
URL: https://learn.cruxyai.com/docs/en/migrate/from-openai
Markdown: https://learn.cruxyai.com/docs/en/migrate/from-openai.md
================================================================================
# Migrate from OpenAI
Move an OpenAI client to Cruxy: change the base URL and the key, pass a tier as the model, and know what carries over and what differs.
Cruxy serves OpenAI-compatible chat completions at `POST /v1/chat/completions`
and an OpenAI-compatible model list at `GET /v1/models`. Both take OpenAI's
request and return OpenAI's response, so code written for the official `openai` npm package moves
over with three changes: the base URL, the API key, and `model`.
## Change three settings
| Setting | Set it to |
| --- | --- |
| Base URL | `https://api.cruxy.ai/v1` |
| API key | Your Cruxy API key, read from `CRUXY_API_KEY`. It travels as `Authorization: Bearer `, as on OpenAI. |
| `model` | A Cruxy tier, `canto`, `saga`, or `epos`, or `auto` to let the router pick one. |
The key needs the `chat` endpoint family, the same as for
`POST /api/v1/chat`; a key without it is `403`. On a server, use a developer
API key from the Cruxy Console:
[Get your API key](/docs/en/get-started/api-keys) covers the kinds of key and
how to create one. Authentication, rate limits, and billing work as they do on
`POST /api/v1/chat`.
## Before and after
The same request, first to OpenAI, then to Cruxy. Only the base URL, the key,
and `model` change; the request body and the response you read stay the same.
```bash title="Before (curl)"
# OPENAI_MODEL is the OpenAI model id you call today.
curl https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "'"$OPENAI_MODEL"'",
"messages": [
{ "role": "user", "content": "Hello" }
]
}'
```
```bash title="After (curl)"
curl https://api.cruxy.ai/v1/chat/completions \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "canto",
"messages": [
{ "role": "user", "content": "Hello" }
]
}'
```
```ts title="Before (TypeScript)"
import OpenAI from 'openai'
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
const completion = await client.chat.completions.create({
// The OpenAI model id you call today.
model: process.env.OPENAI_MODEL ?? '',
messages: [{ role: 'user', content: 'Hello' }],
})
console.log(completion.choices[0].message.content)
```
```ts title="After (TypeScript)"
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.CRUXY_API_KEY,
baseURL: 'https://api.cruxy.ai/v1',
})
const completion = await client.chat.completions.create({
model: 'canto',
messages: [{ role: 'user', content: 'Hello' }],
})
console.log(completion.choices[0].message.content)
```
## Pass a tier as model
`model` takes a tier or `auto`. Any other value, an OpenAI model id included,
is `400` `invalid_request`. These docs map no OpenAI model to a tier: the model
behind a tier is never exposed, and it can change while the tier stays the
same. Choose a tier by its profile, then test it on your own prompts.
| `model` | Profile | Default `reasoning_effort` | `max_tokens` cap |
| --- | --- | --- | --- |
| `canto` | Deep | `high` | 65,536 |
| `saga` | Balanced | `low` | 131,072 |
| `epos` | Fast | `none` | 131,072 |
| `auto` | The router picks a tier for each request. | Chosen per request | The cap of the tier that serves |
The response's `model` is the tier that served the request. With `auto`, it is
the tier the router chose, which a budget can also have downgraded, so read it
rather than assume it. [Choosing a tier](/docs/en/build/choosing-a-tier)
compares the tiers, and [Auto routing](/docs/en/build/auto-routing) covers what
the router does.
`GET /v1/models` returns OpenAI's list shape,
`{"object": "list", "data": [...]}`, and its ids are the tiers. For each
tier's context window, output cap, tool support, and reasoning defaults, read
`GET /api/v1/models`; see [Models](/docs/en/reference/models).
## What carries over unchanged
- **The request.** `messages` with the `system`, `user`, `assistant`, and
`tool` roles, plus `temperature`, `top_p`, `max_tokens`, `stream`,
`stream_options`, `tools`, `tool_choice`, `response_format`, and
`reasoning_effort`.
- **The response.** A `chat.completion` object with `id`, `object`, `created`,
`model`, `choices[].message`, `choices[].finish_reason` (`stop`,
`tool_calls`, `length`, or `content_filter`), and `usage` with
`prompt_tokens`, `completion_tokens`, and `total_tokens`.
- **Streaming.** `chat.completion.chunk` frames that end with `data: [DONE]`,
and a usage frame only when you set `stream_options.include_usage`.
- **Tool calling.** `tools` and `tool_choice` on the request, `tool_calls` on
the assistant message, and a `tool` message with `tool_call_id` for each
result.
- **Errors.** OpenAI's envelope, `{"error": {"message", "type", "param", "code"}}`,
which the SDK raises as an `APIError`.
## What differs
- **`model` is a tier,** and the response's `model` names the tier that
served.
- **Fields Cruxy does not implement are ignored, not rejected:** `n`, `stop`,
`seed`, `presence_penalty`, `frequency_penalty`, `logit_bias`, `logprobs`,
`user`, and the rest. A request that sends them still runs, and they have no
effect.
- **`max_tokens` is clamped to the tier's cap, and thinking spends it.**
`canto` thinks by default, so a small `max_tokens` on `canto` can end with
`finish_reason: "length"` and empty content. `max_completion_tokens` is
accepted as an alias of `max_tokens`, and wins when you send both.
- **`reasoning_effort` takes `none`, `low`, `medium`, or `high`.** Any other
value, OpenAI's `minimal` included, is `400` `invalid_request`.
- **A non-streaming call has a deadline:** 90 seconds on `canto` and `saga`, 45
on `epos`. Past it, the call is `504` `upstream_timeout`.
- **An error's `code` is a Cruxy code,** and `param` is always `null`.
- **The route carries none of the Cruxy-native chat features:** no memory, no
standing rules, no vaults, no `file_ids`, and no `cruxy` block. For those,
call `POST /api/v1/chat`; see [Chat](/docs/en/build/chat).
- **The request ID is only in a header,** `X-Request-Id`, and the response's
`usage` carries no cost.
- **A tool call's `id` may not start with `call_`.** Treat it as opaque.
[OpenAI SDK compatibility](/docs/en/migrate/openai-sdk-compatibility) covers
each of these parameter by parameter, with streaming, errors, and how the SDK
retries.
## Next steps
- [OpenAI SDK compatibility](/docs/en/migrate/openai-sdk-compatibility): what the route supports, ignores, and rejects.
- [Choosing a tier](/docs/en/build/choosing-a-tier): pick the tier to pass as `model`.
- [Reasoning effort](/docs/en/build/reasoning-effort): keep thinking from spending your `max_tokens`.
- [Known limitations](/docs/en/build/known-limitations): the limits to plan around.
================================================================================
Page: OpenAI SDK compatibility
URL: https://learn.cruxyai.com/docs/en/migrate/openai-sdk-compatibility
Markdown: https://learn.cruxyai.com/docs/en/migrate/openai-sdk-compatibility.md
================================================================================
# OpenAI SDK compatibility
What POST /v1/chat/completions supports, ignores, and rejects from an OpenAI request, and how its streams, tool calls, and errors behave in the openai SDK.
`POST /v1/chat/completions` is the route the official `openai` npm package
calls when you set `baseURL` to `https://api.cruxy.ai/v1`. It runs your
messages on the tier you name and nothing else: no memory, no standing rules,
and no `cruxy` block, so a run is reproducible. This page goes field by field
through what it does with an OpenAI request. To move a client over first, see
[Migrate from OpenAI](/docs/en/migrate/from-openai).
## Request parameters
| Parameter | Status | Behavior |
| --- | --- | --- |
| `model` | Supported | `canto`, `saga`, `epos`, or `auto`. Required. The former ids `kavi`, `vaani`, and `mira` are still accepted; see [Deprecated aliases](/docs/en/reference/models#deprecated-aliases). |
| `messages` | Supported | Required. Each message has a `role`, one of `system`, `user`, `assistant`, or `tool`, and a string `content`. An assistant message can replay `tool_calls`, and a `tool` message needs `tool_call_id`. |
| `max_tokens` | Supported, clamped | Unset, or above the tier's cap, is clamped to the cap: 65,536 on `canto`, 131,072 on `saga` and `epos`. Thinking is generated inside it. Ignored when `max_completion_tokens` is also sent. |
| `max_completion_tokens` | Supported, clamped | OpenAI's newer name for `max_tokens`, with the same meaning and the same clamp. When a request sends both, `max_completion_tokens` is used and `max_tokens` is ignored. |
| `temperature` | Supported | Forwarded upstream unchanged; the gateway adds no bound of its own. Omit it for the model's default. |
| `top_p` | Supported | Forwarded upstream unchanged. `0` is a real value, so sending it is not the same as omitting it. Omit it for the model's default. |
| `stream` | Supported | `false` by default. |
| `stream_options` | Supported | `include_usage: true` adds the usage frame to a stream. Ignored when `stream` is `false`. |
| `reasoning_effort` | Supported | `none`, `low`, `medium`, or `high`. Omit it for the tier's default. |
| `tools` | Supported | Functions the model may call: `type: "function"`, with `function.name`, `description`, and a JSON Schema `parameters`. |
| `tool_choice` | Supported | `"none"`, `"auto"`, `"required"`, or `{"type": "function", "function": {"name": "…"}}` to force one function. |
| `response_format` | Supported | `{"type": "text"}`, `{"type": "json_object"}`, or `{"type": "json_schema", "json_schema": {"name", "schema", "strict"}}`. |
| `n`, `stop`, `seed`, `presence_penalty`, `frequency_penalty`, `logit_bias`, `logprobs`, `user` | Ignored | Accepted and not applied, so a request that sends them still runs. |
| Any other OpenAI field | Ignored | The route ignores the OpenAI fields it does not implement. |
| `file_ids` | Rejected | The file attachments of `POST /api/v1/chat` are refused here. |
`messages` takes the four roles above; OpenAI's `developer` role is not one of
them, so send your instructions as a `system` message.
### Requests the route rejects
These requests fail with a `4xx` and the code shown:
| Request | Status and `code` |
| --- | --- |
| `model` other than `canto`, `saga`, `epos`, `auto`, or a deprecated alias | `400` `invalid_request` |
| `reasoning_effort` other than `none`, `low`, `medium`, or `high`, OpenAI's `minimal` included | `400` `invalid_request` |
| `tool_choice` names a function that is not in `tools`, or is `"required"` with no tools | `400` `invalid_request` |
| A tool's `parameters` schema exceeds a bound on bytes, depth, or nodes | `400` `invalid_tool_schema` |
| A `response_format` JSON schema is malformed, oversized, too deep, or uses an unsupported keyword | `400` `invalid_schema` |
| The input exceeds the tier's context window | `400` `context_overflow` |
| The server-side input moderation gate refuses the request's text | `400` `moderation_rejected`, not billed |
| The turn brings more new text than the moderation gate reads in one request | `400` `moderation_input_too_large`, not billed; `limit_bytes` is not carried in this envelope |
| `response_format` on a tier that does not do constrained decoding | `422` `structured_output_unsupported` |
| An `Idempotency-Key` reused with a different body | `409` `idempotency_conflict` |
One more `422` comes after the model call: `structured_output_unsatisfied`
means the output did not satisfy your `json_schema`. That request is billed,
and it may pass on a retry.
## Response fields
With `stream: false`, the response is a standard `chat.completion` object:
| Field | Value |
| --- | --- |
| `id` | The completion id, such as `chatcmpl-8f3a1c2d`. |
| `object` | `chat.completion`. |
| `created` | Unix seconds at which the completion was produced. |
| `model` | The tier that served the request. With `auto`, the tier the router chose. |
| `choices[].message` | The assistant message, with `tool_calls` when the model called tools. |
| `choices[].finish_reason` | `stop`, `tool_calls`, `length`, or `content_filter`. |
| `choices[].logprobs` | Always `null`. The key is there because OpenAI sends it. |
| `usage` | `prompt_tokens`, `completion_tokens`, and `total_tokens`, plus `prompt_tokens_details.cached_tokens` when the prompt cache was involved. |
The response has no `cruxy` block, no `request_id`, and no cost.
## Cruxy extensions in the SDK
Every Cruxy extension this route honors is either a typed SDK parameter or a
header, so you never need an untyped body field:
- **`reasoning_effort`** is OpenAI's own parameter, typed in
`chat.completions.create`, with Cruxy's values: send only `none`, `low`,
`medium`, or `high`, although the SDK's type allows more. `canto` thinks at
`high` by default, `saga` at `low`, and `epos` not at all. See
[Reasoning effort](/docs/en/build/reasoning-effort).
- **`Idempotency-Key`** is a request header; pass it in the SDK's per-request
`headers` option. The first success under a key is stored for 24 hours, and
a retry with the same key and body replays it without calling the model or
billing again. Only a success is stored, and only with `stream: false`. The
SDK does not send this header for you. See
[Reliability](/docs/en/build/reliability).
- **`X-Request-Id`** is on every response, and it is the only request ID this
route returns. The SDK exposes it as `_request_id` on a result and as
`requestID` on an error. Send your own, up to 128 characters of
`[A-Za-z0-9_-]`, and the gateway reuses it. See
[Request IDs and metadata](/docs/en/build/request-ids).
```bash title="curl"
# -i prints the response headers, including X-Request-Id.
curl -i https://api.cruxy.ai/v1/chat/completions \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{
"model": "canto",
"reasoning_effort": "none",
"max_tokens": 256,
"messages": [
{ "role": "user", "content": "In one sentence, what is a monsoon?" }
]
}'
```
```ts title="TypeScript"
import { randomUUID } from 'node:crypto'
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.CRUXY_API_KEY,
baseURL: 'https://api.cruxy.ai/v1',
})
const completion = await client.chat.completions.create(
{
model: 'canto',
// Thinking off: the whole max_tokens budget goes to the answer.
reasoning_effort: 'none',
max_tokens: 256,
messages: [{ role: 'user', content: 'In one sentence, what is a monsoon?' }],
},
// The SDK resends the same headers when it retries, so a retry after a
// dropped connection replays a stored success instead of billing again.
{ headers: { 'Idempotency-Key': randomUUID() } },
)
console.log(completion.choices[0].message.content)
console.log(completion.model, completion._request_id)
```
### Memory and standing rules
This route does no memory recall, no memory capture, and no standing-rules
injection, and it takes no `vault_id`. There is nothing to pass through the
SDK to turn them on. To use them, call `POST /api/v1/chat` or
`POST /api/v2/chat` with `fetch`: the SDK's `chat.completions` methods call
`/chat/completions` under the base URL, so they cannot reach either route. See
[Memory](/docs/en/build/memory) and [Standing rules](/docs/en/build/rules).
## Tool calls
Tool calling keeps OpenAI's shapes. Declare `tools`, and when `finish_reason`
is `tool_calls`, run each call in `message.tool_calls`, append the assistant
message and one `tool` message per call, and call again.
A tool call's `id` is a string with no documented format. It may not start
with `call_`, even though examples show `call_abc123`. Copy it unchanged into
`tool_call_id`, and never parse, validate, or build one; see
[Tool-call IDs may not use OpenAI's call\_ format](/docs/en/build/known-limitations#tool-call-ids-may-not-use-openais-call_-format).
```ts
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.CRUXY_API_KEY,
baseURL: 'https://api.cruxy.ai/v1',
})
const tools: OpenAI.ChatCompletionTool[] = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a city',
parameters: {
type: 'object',
properties: { city: { type: 'string' } },
required: ['city'],
},
},
},
]
const messages: OpenAI.ChatCompletionMessageParam[] = [
{ role: 'user', content: 'Is it raining in Pune?' },
]
let completion = await client.chat.completions.create({ model: 'saga', tools, messages })
while (completion.choices[0].finish_reason === 'tool_calls') {
const message = completion.choices[0].message
messages.push(message)
for (const call of message.tool_calls ?? []) {
if (call.type !== 'function') continue
const args = JSON.parse(call.function.arguments)
// Echo the id exactly as it arrived; do not assume a call_ prefix.
messages.push({
role: 'tool',
tool_call_id: call.id,
content: JSON.stringify({ city: args.city, raining: true }),
})
}
completion = await client.chat.completions.create({ model: 'saga', tools, messages })
}
console.log(completion.choices[0].message.content)
```
## Streaming
With `stream: true`, the route speaks OpenAI's Server-Sent Events protocol, in
this order:
1. A role primer: the first `chat.completion.chunk`.
2. Content deltas in `choices[0].delta.content`, or tool-call fragments in
`choices[0].delta.tool_calls`, which you assemble per `index`.
3. A frame that carries `finish_reason`.
4. With `stream_options: {"include_usage": true}` only, a usage frame:
`choices` is an empty array and `usage` is complete. Check `choices` before
you index it.
5. `data: [DONE]`.
If the model fails after the stream has opened, the stream ends with a
`data: {"error": {...}}` frame instead of `[DONE]`, so a cut-off answer never
looks finished. The SDK throws that frame as an `APIError` from the loop:
catch it and discard the partial text. The gateway retries a failing model
only while the stream opens, never after a byte has reached you, and a stream
may run for up to five minutes.
```bash title="curl"
# -N turns off buffering so frames print as they arrive.
curl -N https://api.cruxy.ai/v1/chat/completions \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "saga",
"stream": true,
"stream_options": { "include_usage": true },
"messages": [
{ "role": "user", "content": "Write a haiku about monsoon rain." }
]
}'
```
```ts title="TypeScript"
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.CRUXY_API_KEY,
baseURL: 'https://api.cruxy.ai/v1',
})
const stream = await client.chat.completions.create({
model: 'saga',
stream: true,
stream_options: { include_usage: true },
messages: [{ role: 'user', content: 'Write a haiku about monsoon rain.' }],
})
let text = ''
try {
for await (const chunk of stream) {
// The usage frame has an empty choices array.
if (chunk.choices.length > 0) text += chunk.choices[0].delta.content ?? ''
if (chunk.usage) console.log('usage:', chunk.usage)
}
} catch (err) {
// A failure after the stream opened arrives as a data: {"error": ...} frame,
// and the SDK throws it here. The partial text is not a finished answer.
throw new Error('stream failed before it finished', { cause: err })
}
console.log(text)
```
A stream stores and replays nothing under `Idempotency-Key`. A retry with the
same key and body streams a new generation; when both attempts complete, the
request is billed once. [Streaming](/docs/en/build/streaming) covers the
stream of `POST /api/v1/chat`, which differs: it sends no error frame.
## Errors
Errors on `POST /v1/chat/completions` and `GET /v1/models` use OpenAI's
envelope:
```json
{
"error": {
"code": "unauthorized",
"message": "unauthorized",
"param": null,
"type": "authentication_error"
}
}
```
- **`code`** is the stable Cruxy error code, the same vocabulary as the rest of
the API. Branch on it.
- **`type`** is a coarse category derived from the HTTP status:
`invalid_request_error`, `authentication_error`, `permission_error`,
`not_found_error`, `rate_limit_error`, or `server_error`.
- **`param`** is always `null`: no error on this route names the field at
fault.
Only the code travels. The detail fields a Cruxy error adds next to `code`,
such as `tier`, `reason`, or `hazard_codes`, are not part of this envelope, on
any code.
The `/api/v1` routes use Cruxy's own envelope instead. The two differ in where
each value sits:
| Item | `/v1/chat/completions` and `/v1/models` | `/api/v1` routes |
| --- | --- | --- |
| Envelope | `{"error": {"message", "type", "param", "code"}}` | `{"code", "error", "request_id", ...}` |
| The code to branch on | `error.code` | `code` |
| The message | `error.message` | `error` |
| The request ID | The `X-Request-Id` header only | `request_id` in the body |
| Detail fields such as `reset_at`, `epos_available`, or `limit` | Not carried | In the body |
Because this envelope has no place for the detail fields, read the headers
instead: every `429` carries `Retry-After`, and the `X-RateLimit-*` headers
describe the per-minute request window.
[Errors and retries](/docs/en/build/errors-and-retries) lists every code and
which ones to retry.
The SDK throws a non-2xx response as an `APIError`, with the status in
`status`, the Cruxy code in `code`, and `X-Request-Id` in `requestID`:
```bash title="curl"
# OpenAI's "minimal" is not a Cruxy reasoning_effort, so this is a 400.
curl -i https://api.cruxy.ai/v1/chat/completions \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "canto",
"reasoning_effort": "minimal",
"messages": [
{ "role": "user", "content": "Hello" }
]
}'
```
```ts title="TypeScript"
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.CRUXY_API_KEY,
baseURL: 'https://api.cruxy.ai/v1',
})
try {
await client.chat.completions.create({
model: 'canto',
// OpenAI's "minimal" is not a Cruxy reasoning_effort, so this is a 400.
reasoning_effort: 'minimal',
messages: [{ role: 'user', content: 'Hello' }],
})
} catch (err) {
if (!(err instanceof OpenAI.APIError)) throw err
// 400 invalid_request, and the X-Request-Id to quote in a support request.
console.error(err.status, err.code, err.requestID)
}
```
### Retries in the SDK
The `openai` package retries a failed request twice by default: after a
connection error, and on `408`, `409`, `429`, and any `5xx`. It waits for
`Retry-After` when that is 60 seconds or less. Against Cruxy, three of those
retries are unlikely to help:
- **`504` `upstream_timeout`.** The gateway made one attempt and does not retry
it, and the same request usually takes as long again. Stream long turns, or
lower `max_tokens` or `reasoning_effort`, rather than rely on the retry.
- **`429` spend and budget codes,** such as `budget_exhausted`. They clear only
at their reset, and their `Retry-After` can be days away, so a retry sooner
fails the same way. Branch on `code` and stop retrying them.
- **`409` `idempotency_conflict`.** The key was reused with a different body,
and the SDK resends that same body. Send the new body under a new key.
A `504` or a `429` is not billed. To make the SDK's retries after a dropped
connection safe, send an `Idempotency-Key`, as the extensions sample does.
## Known differences
These are the places where this route behaves unlike OpenAI's, collected in
one list:
- `model` takes a tier or `auto`, and the response's `model` is the tier that
served, never an upstream model id.
- Fields the route does not implement are ignored without an error, so a
`stop` sequence or `n` has no effect.
- Thinking spends `max_tokens` and is not returned; see
[Canto's thinking spends max_tokens](/docs/en/build/known-limitations#cantos-thinking-spends-max_tokens).
- A non-streaming call ends at the tier's deadline with `504`
`upstream_timeout`; see
[Non-streaming calls have a 90-second ceiling](/docs/en/build/known-limitations#non-streaming-calls-have-a-90-second-ceiling).
- A tool call's `id` may not use the `call_` format; see
[Tool-call IDs may not use OpenAI's call\_ format](/docs/en/build/known-limitations#tool-call-ids-may-not-use-openais-call_-format).
- `error.code` is a Cruxy code, `param` is always `null`, and the detail fields
of a Cruxy error do not travel.
- The response carries no request ID in its body and no cost in its `usage`.
- No memory, standing rules, vaults, or file attachments.
[Known limitations](/docs/en/build/known-limitations) lists the limits that
apply across the API, each with a workaround.
## Next steps
- [Migrate from OpenAI](/docs/en/migrate/from-openai): change the base URL, the key, and `model`.
- [Chat](/docs/en/build/chat): the Cruxy-native route, with memory, standing rules, and the `cruxy` block.
- [Errors and retries](/docs/en/build/errors-and-retries): every error code, and which ones to retry.
- [Known limitations](/docs/en/build/known-limitations): the limits to plan around.
================================================================================
Page: Chat
URL: https://learn.cruxyai.com/docs/en/build/chat
Markdown: https://learn.cruxyai.com/docs/en/build/chat.md
================================================================================
# Chat
OpenAI-compatible chat completions across the Cruxy tiers, streaming or not.
`POST /api/v1/chat` runs an OpenAI-compatible chat completion. You pick a
public tier, send a list of messages, and read back either a single JSON
completion or a stream of Server-Sent Events. Here is the smallest
complete call:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "canto",
"stream": false,
"messages": [
{ "role": "user", "content": "Hello" }
]
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'canto',
stream: false,
messages: [{ role: 'user', content: 'Hello' }],
}),
})
const completion = await res.json()
```
## Tiers, not provider models
`model` is a **public tier**, one of:
| Tier | Profile |
| --- | --- |
| `canto` | Deep |
| `saga` | Balanced |
| `epos` | Fast |
The gateway resolves each tier to an upstream provider model on the
server. You never send or receive an upstream provider id. Tiers are the
stable contract, so your code keeps working when the backing model
changes. [Choosing a tier](/docs/en/build/choosing-a-tier) compares them; to
discover the catalog at runtime rather than hardcoding these strings, see
[Models](/docs/en/reference/models).
`model` also accepts `"auto"`, which hands tier selection to the gateway's
router. It resolves to a concrete tier before billing and the upstream call,
and `routed_tier` on the response (see [The cruxy block](#the-cruxy-block))
names the tier that served. [Auto routing](/docs/en/build/auto-routing) covers
what the router guarantees.
## Request options
Beyond `model` and `messages`, the request accepts:
| Field | Type | Example | Purpose |
| --- | --- | --- | --- |
| `stream` | boolean | `true` | Stream SSE chunks. Optional and `false` by default, which returns one JSON object. |
| `temperature` | number | `0.7` | Sampling temperature. |
| `max_tokens` | integer | `1024` | Cap on tokens generated. |
| `reasoning_effort` | string | `"medium"` | How much the model thinks before answering: `none`, `low`, `medium`, or `high` (see [Reasoning effort](/docs/en/build/reasoning-effort)). |
| `tools` | array | | Functions the model may call (see [Define tools](/docs/en/build/tools)). |
| `tool_choice` | string or object | `"auto"` | Whether and which tool the model calls. |
| `memory` | object | `{ "recall": true }` | Memory recall and capture for this request (see [Memory](/docs/en/build/memory)). |
| `rules` | boolean | `false` | Standing-rules injection for this request (see [Standing rules](/docs/en/build/rules)). |
Each `messages` entry has a `role` and `content`. Assistant turns that
call tools carry `tool_calls`, and your replies to those calls carry a
`tool_call_id`; both are covered in
[Handle tool calls](/docs/en/build/tool-calls).
## Streaming vs. not
`stream` controls the response format.
### stream: false
This is what you get when you omit `stream`: a single JSON object. Cruxy
flattens it: the assistant reply is the
top-level `message`, alongside `finish_reason`, `usage`, and the `cruxy`
block. There is no OpenAI `choices[]` array. Read `message.content`
directly, not `choices[0].message`.
```json
{
"id": "chatcmpl-8f3a1c2d",
"model": "canto",
"message": { "role": "assistant", "content": "Namaste." },
"finish_reason": "stop",
"usage": {
"input_tokens": 12,
"output_tokens": 48,
"billable_input_tokens": 12,
"cache_read_tokens": 0,
"cache_write_tokens": 0,
"cost": { "tier": "canto", "amount": "", "currency": "" }
},
"cruxy": {
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"routed_tier": "canto",
"reasoning_effort": "high"
}
}
```
`` and `` are placeholders: a real `amount` is a number. See
[Usage and cost](/docs/en/build/usage-and-cost) for the full `cost` object.
### stream: true
You get an OpenAI-style Server-Sent Events stream: a sequence of
`chat.completion.chunk` events, each on a `data:` line, terminated by a
final `data: [DONE]`. Unlike the non-streaming reply, a chunk uses the
OpenAI `choices[]` shape: each token arrives in `choices[0].delta.content`.
```bash
curl -N https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "canto",
"stream": true,
"messages": [
{ "role": "user", "content": "Write a haiku about monsoon rain." }
]
}'
```
`data: [DONE]` is the only signal that the completion finished: a stream that
ends without it failed partway. [Streaming](/docs/en/build/streaming) covers
the frame order, the chunk and usage shapes, a complete TypeScript reader, and
how to detect a failed stream.
## The cruxy block
Cruxy-native fields are additive and namespaced under a `cruxy` key. They
never alter the OpenAI-shaped fields, so a vanilla OpenAI client parses the
response unchanged and ignores the extra key. On a chat response the block
carries three fields for the turn:
- `request_id`: a UUID for the call, e.g.
`a1b2c3d4-e5f6-7890-abcd-ef1234567890`. Log it; it identifies the call in
support requests and pairs with [Feedback](/docs/en/build/feedback).
- `routed_tier`: the tier that actually served the request. This is how you
learn which tier ran when you send `model: "auto"`.
- `reasoning_effort`: the effort the call ran at, after any tier default is
applied.
When they apply, the block also reports what the turn recalled, captured, or
injected: `memory_recall`, `memory_capture`, `rules_applied`, `files_omitted`,
and `undeclared_tool_replay`. When the tier reasoned, `reasoning` holds its
thinking trace, kept out of `message`; see
[Reasoning effort](/docs/en/build/reasoning-effort#see-the-thinking).
[Request IDs and metadata](/docs/en/build/request-ids#the-cruxy-block) lists
every field, and how to quote `request_id` when you report a problem.
Cost and token counts are not in this block; they live in `usage`.
## Usage
Every chat response carries a `usage` object with the token accounting and
the call's `cost`. The token fields depend on the response mode:
non-streaming uses `input_tokens` and `output_tokens`; streaming uses
`prompt_tokens`, `completion_tokens`, and `total_tokens`. Both can also carry
`billable_input_tokens`, `cache_read_tokens`, and `cache_write_tokens`; on a
stream each is omitted when it is zero.
`usage.cost` breaks down the price of the call:
| Field | Meaning |
| --- | --- |
| `tier` | The public tier that served the request. |
| `amount` | Total cost of the call. |
| `currency` | Currency of `amount`. |
| `input_unit` | Input rate, per million tokens. |
| `output_unit` | Output rate, per million tokens. |
| `cache_read_unit` | The rate for cached reads, per million tokens: the tier's own cache-read rate where it has one, otherwise `input_unit` × the cache discount. |
Amounts and rates are specific to your account and shown in your Cruxy
Console. [Usage and cost](/docs/en/build/usage-and-cost) covers which tokens
are billed and how a stream reports usage.
## Errors
Chat returns `400` for a malformed request, `401` `unauthorized` for a
missing, expired, or revoked key, `429` when you hit a limit, such as
`rate_limited` or `budget_exhausted`, and `5xx` when the model call fails.
With `stream: false` the gateway makes one attempt, bounded by the tier's
deadline; an attempt that runs past it is `504` `upstream_timeout`, which the
gateway does not retry and does not bill (see
[Timeouts](/docs/en/build/errors-and-retries#timeouts)). See
[Errors](/docs/en/reference/errors) for the response shape,
[Errors and retries](/docs/en/build/errors-and-retries) for every code and
which ones to retry, and [Rate limits and budgets](/docs/en/build/rate-limits)
for how to tell a rate limit from a budget.
## Next steps
- [Streaming](/docs/en/build/streaming): read the SSE stream and detect a failed one.
- [Define tools](/docs/en/build/tools): let the model call your functions.
- [Reasoning effort](/docs/en/build/reasoning-effort): keep thinking from spending your `max_tokens`.
- [Known limitations](/docs/en/build/known-limitations): the limits to plan around, such as the 90-second non-streaming ceiling.
- [Streaming chat in the browser](/docs/en/resources/cookbook/streaming-ui): consume the SSE stream in a UI.
- [Models](/docs/en/reference/models): discover tiers at runtime.
- [Errors and retries](/docs/en/build/errors-and-retries): handle `400`, `401`, and `429`, and retry safely.
================================================================================
Page: Streaming
URL: https://learn.cruxyai.com/docs/en/build/streaming
Markdown: https://learn.cruxyai.com/docs/en/build/streaming.md
================================================================================
# Streaming
Read chat completions as Server-Sent Events: the frame order, the chunk and usage shapes, and how to tell a finished stream from a failed one.
`stream` is optional and `false` by default. With `stream: true`,
`POST /api/v1/chat` answers with `text/event-stream`: OpenAI-style Server-Sent
Events in which every event is one `chat.completion.chunk` JSON object on a
`data:` line. The stream ends with the literal line `data: [DONE]`, and that
line is the only signal that the completion finished.
```bash title="curl"
curl -N https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "saga",
"stream": true,
"messages": [
{ "role": "user", "content": "Write a haiku about monsoon rain." }
]
}' | tee stream.txt
# The completion finished only if the stream ended with [DONE].
grep -q '^data: \[DONE\]' stream.txt || echo "stream failed before [DONE]"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'saga',
stream: true,
messages: [{ role: 'user', content: 'Write a haiku about monsoon rain.' }],
}),
})
if (!res.ok || !res.body) throw new Error(`chat failed: ${res.status} ${await res.text()}`)
const reader = res.body.getReader()
const decoder = new TextDecoder()
let buffer = ''
let text = ''
let finishReason: string | null = null
let usage: Record | undefined
let sawDone = false
read: while (true) {
const { value, done } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
const lines = buffer.split('\n')
buffer = lines.pop() ?? '' // keep a partial line for the next read
for (const raw of lines) {
const line = raw.trim()
if (!line.startsWith('data:')) continue
const data = line.slice('data:'.length).trim()
if (data === '[DONE]') {
sawDone = true
break read
}
const chunk = JSON.parse(data)
if (chunk.usage) usage = chunk.usage
const choice = chunk.choices[0] // undefined on the usage frame
if (!choice) continue
if (choice.delta.content) text += choice.delta.content
if (choice.finish_reason) finishReason = choice.finish_reason
}
}
if (!sawDone) {
// The stream ended early: a failed completion, not a short one.
throw new Error('stream ended before [DONE]; discard the partial output')
}
console.log(text)
console.log(finishReason, usage)
```
## Frame order
A successful stream sends these frames, in this order:
| # | Frame | `choices` | What it carries |
| --- | --- | --- | --- |
| 1 | Role primer | One element; `delta` is `{"role": "assistant", "content": ""}` | The turn's `cruxy` block: `routed_tier`, `request_id`, and `reasoning_effort`, plus `memory_recall` and `rules_applied` when they apply. |
| 2 | Content or tool-call deltas, zero or more | One element; `delta` has a `content` fragment or `tool_calls` fragments, never both | The answer, piece by piece. `finish_reason` is `null`. |
| 2 | Reasoning frames, zero or more, among the deltas | One element; `delta` is `{}` and `finish_reason` is `null` | `cruxy.reasoning`: one fragment of the tier's thinking trace. See [Reasoning frames](#reasoning-frames). |
| 3 | Finish | One element; `delta` is `{}` | `finish_reason`. No usage. |
| 4 | Usage | `[]`, an empty array | The final `usage` object, plus `cruxy.memory_capture` when the turn stored memory. |
| 5 | `data: [DONE]` | | Nothing. It is not JSON; do not parse it. |
On the wire:
```text
data: {"id":"chatcmpl-8f3a1c2d","object":"chat.completion.chunk","model":"saga","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}],"cruxy":{"routed_tier":"saga","request_id":"a1b2c3d4-e5f6-7890-abcd-ef1234567890","reasoning_effort":"low"}}
data: {"id":"chatcmpl-8f3a1c2d","object":"chat.completion.chunk","model":"saga","choices":[{"index":0,"delta":{"content":"Clouds lean"},"finish_reason":null}]}
data: {"id":"chatcmpl-8f3a1c2d","object":"chat.completion.chunk","model":"saga","choices":[{"index":0,"delta":{"content":" on the hills"},"finish_reason":null}]}
data: {"id":"chatcmpl-8f3a1c2d","object":"chat.completion.chunk","model":"saga","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"chatcmpl-8f3a1c2d","object":"chat.completion.chunk","model":"saga","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":48,"total_tokens":60,"cost":{"amount":,"currency":"","tier":"saga",...}}}
data: [DONE]
```
Changing the framing or this order is a breaking change that ships only under a
new dated `Cruxy-Version`, so you can rely on it. New `cruxy.*` fields can
appear without a version change, so ignore fields you do not recognize.
## The chunk
| Field | Meaning |
| --- | --- |
| `id` | The completion id. |
| `object` | `chat.completion.chunk`. |
| `model` | The tier. |
| `choices` | One element on every frame that carries output; an empty array on the usage frame. |
| `choices[0].index` | `0`. |
| `choices[0].delta` | The fragment: `role` on the primer, then `content` or `tool_calls`, then `{}` on the finish frame. A reasoning frame's `delta` is `{}` too. |
| `choices[0].finish_reason` | `null` until the finish frame, then `stop`, `tool_calls`, `length`, or `content_filter`. |
| `usage` | Present only on the usage frame. |
| `cruxy` | The Cruxy-native block; see [Chat](/docs/en/build/chat#the-cruxy-block). |
> **Warning:** Check that `choices` is non-empty before you read `choices[0]`. The usage frame
> has `choices: []` and arrives on every successful stream, so indexing it
> unguarded crashes every time.
The role primer's `content` is an empty string, not absent. Appending every
`content` you see is safe; the primer adds nothing.
### Tool-call deltas
When the model calls a tool, deltas carry `tool_calls` fragments instead of
`content`. Every fragment has an `index` and a piece of `function.arguments`,
a JSON string; fragments can also carry the call's `id`, `type`, and
`function.name`. Assemble the fragments per `index`, concatenating the
`arguments` pieces. A finish frame with `finish_reason: "tool_calls"` means the
fragments are complete: run the calls and answer them as
[Handle tool calls](/docs/en/build/tool-calls) describes.
## Reasoning frames
When a tier reasons, the stream carries its thinking trace beside the answer.
Each fragment rides a frame of its own, among the content deltas: `delta` is
an empty object, `finish_reason` is `null`, and the `cruxy` block holds only
`reasoning`.
```text
data: {"id":"chatcmpl-8f3a1c2d","object":"chat.completion.chunk","model":"canto","choices":[{"index":0,"delta":{},"finish_reason":null}],"cruxy":{"reasoning":"The user wants 17*23."}}
```
- Concatenate the fragments in order for the trace. It is not part of the
answer: never append it to `content`, and do not send it back in the
conversation.
- A reasoning frame has an empty `delta`, as the finish frame does. Detect the
finish frame by a non-null `finish_reason`, never by the empty `delta`.
- The trace changes no figure and no bill. `usage` already counts the thinking
inside the output tokens, and there is no separate reasoning-token count.
Reasoning frames are sent from `Cruxy-Version: 2026-11-12`, and when a request
sends no `Cruxy-Version`. A request pinned to an earlier date receives none,
and its stream is the one in the table above without them. A non-streaming
response carries the whole trace as `cruxy.reasoning`; see
[Reasoning effort](/docs/en/build/reasoning-effort#see-the-thinking).
### Thought lines
A thought line is a few words saying what one part of the thinking trace is
doing, such as `Comparing the three forecasts`, so a client can show progress
in plain words. It arrives as `cruxy.reasoning_summary` on a frame of its own,
shaped like a reasoning frame: an empty `delta`, a `null` `finish_reason`, and
a `cruxy` block that holds only `reasoning_summary`.
| Field | Meaning |
| --- | --- |
| `index` | Which part of the trace the line describes, counted from 0. |
| `text` | The line: plain text, under 60 characters, in the language of the user's message. |
| `start`, `end` | The half-open span `[start, end)` of that part in the concatenated `reasoning` fragments, counted in Unicode code points. |
A turn carries at most 12 thought lines, always before the finish frame. They
are best-effort: a line that is late or fails is never sent, so a part of the
trace can have none, and lines can arrive out of order. Key on `index`. The
gateway writes them and does not bill them: they add nothing to `usage`.
Whether a deployment sends thought lines at all is a switch you read, not
assume: `cruxy.reasoning_summaries` on `GET /api/v1/models`.
- **`false`:** no turn gets thought lines.
- **`true`:** a turn gets them only when it is a stream from a signed-in
credential that pins `Cruxy-Version: 2026-11-13` or later. A request with no
`Cruxy-Version` does not get them.
A developer or headless key never gets them, whatever the switch says, and
`POST /v1/chat/completions` never sends them. When a turn does not qualify, the
stream is exactly the one before `2026-11-13`. `GET /v1/models` does not carry
the switch.
```bash title="curl"
curl -s https://api.cruxy.ai/api/v1/models \
-H "Authorization: Bearer $CRUXY_API_KEY" \
| jq '.cruxy.reasoning_summaries'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/models', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
const { cruxy } = await res.json()
// Only a signed-in credential's stream, pinned to 2026-11-13 or later, can
// get thought lines, and only when this is true.
const showThoughtLines: boolean = cruxy.reasoning_summaries
console.log('thought lines on this deployment:', showThoughtLines)
```
## The usage frame
The usage frame is the only frame with `usage`, and it is never merged into the
finish frame before it, because an OpenAI-compatible client reads usage only
from the frame whose `choices` is empty.
| Field | Meaning |
| --- | --- |
| `prompt_tokens` | Input tokens. |
| `completion_tokens` | Output tokens. |
| `total_tokens` | The sum. |
| `billable_input_tokens`, `cache_read_tokens`, `cache_write_tokens` | Omitted when zero; absence means zero. |
| `prompt_tokens_details` | The prompt-cache breakdown, present only when the prompt cache was used. |
| `cost` | An object, not a number: `amount`, `currency`, `tier`, and the per-million-token rates `input_unit`, `output_unit`, and `cache_read_unit`. Read `cost.amount` for a scalar. |
A stream names its token fields the OpenAI way (`prompt_tokens`,
`completion_tokens`); a non-streaming response uses `input_tokens` and
`output_tokens`. [Usage and cost](/docs/en/build/usage-and-cost) covers both
shapes, the `cost` object, and which tokens are billed.
## [DONE] is the only success signal
Once the stream opens, the `200` is already sent. If the completion fails after
that, no error frame follows: the gateway stops sending and closes the
response without the finish frame, the usage frame, or `data: [DONE]`.
> **Important:** A stream that ends before `data: [DONE]`, whether the body closes or a read
> returns end-of-stream, is a failed, incomplete completion. Discard the partial
> output or retry; never present it as a finished answer. The tokens already
> delivered are real and are billed as a partial usage row.
To detect it on the client:
1. Set a flag when you read `data: [DONE]`, as the sample above does with
`sawDone`.
2. When the read loop ends, treat a missing flag as a failure.
3. Treat an exception from the read itself, such as a dropped connection, as a
failure too.
A stream that ends with `[DONE]` is complete, but the answer can still be cut
short: `finish_reason: "length"` means the output reached `max_tokens` or the
tier's cap. On `canto`, which reasons by default, a small `max_tokens` can be
spent entirely on thinking; see
[Features overview](/docs/en/get-started/features#three-tiers-and-auto).
The gateway retries a failing model only while the stream opens, never after a
byte has reached you. A stream may run for up to five minutes.
A stream stores and replays nothing under `Idempotency-Key`, since a stream
cannot be replayed: a retry with the same key and body streams a new
generation. When both attempts complete, the request is billed once. An attempt
that did not complete is billed for the tokens it delivered, and the retry is
billed as well.
## Other chat wires
The rules above are for `POST /api/v1/chat`. The other two chat endpoints end
their streams differently:
- `POST /api/v2/chat` sends named events (`start`, `reasoning`, `delta`, and
`reasoning_summary` for a thought line, on the same terms) and
always ends with exactly one `finish` or `error` frame. It has no `[DONE]`;
a stream that closes without `finish` or `error` failed.
- `POST /v1/chat/completions` ends a stream that fails after it opens with a
`data: {"error": {...}}` frame instead of `[DONE]`. It sends the usage frame
only when the request sets `stream_options: {"include_usage": true}`.
To render a stream in a browser without exposing your key, see
[Streaming chat in the browser](/docs/en/resources/cookbook/streaming-ui).
## Next steps
- [Chat](/docs/en/build/chat): the request options and the non-streaming response.
- [Reliability](/docs/en/build/reliability): retry a stream that failed, and the deadlines it avoids.
- [Handle tool calls](/docs/en/build/tool-calls): answer the tool calls a stream returns.
- [Errors and retries](/docs/en/build/errors-and-retries): what to retry, and how.
================================================================================
Page: Embeddings
URL: https://learn.cruxyai.com/docs/en/build/embeddings
Markdown: https://learn.cruxyai.com/docs/en/build/embeddings.md
================================================================================
# Embeddings
Turn text into vectors with the cruxy-embed model.
`POST /api/v1/embeddings` takes one string or an array and returns a
vector per input, ready to store and compare for search, clustering, or
retrieval. It is OpenAI-compatible. Here is a single-string embedding:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/embeddings \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "cruxy-embed",
"input": "The quick brown fox"
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/embeddings', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'cruxy-embed',
input: 'The quick brown fox',
}),
})
const { data } = await res.json()
const vector = data[0].embedding // number[]
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
// The SDK's `input` is an array: send one string as a one-element array.
const { result } = await cruxy.embeddings.createEmbeddings({
body: { model: 'cruxy-embed', input: ['The quick brown fox'] },
})
const vector = result.data[0]?.embedding // number[]
```
## The model
`model` must be `"cruxy-embed"`. It is the only embedding model on the
gateway.
## Single string or array
`input` accepts either a single string or an array of strings. Pass an
array to embed a batch in one call:
```bash
curl https://api.cruxy.ai/api/v1/embeddings \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "cruxy-embed",
"input": ["The quick brown fox", "jumps over the lazy dog"]
}'
```
Each input maps to one entry in `data`, and the entry's `index` matches
the position of its input.
## Response shape
```json
{
"object": "list",
"model": "cruxy-embed",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": [0.0023, -0.0117]
}
],
"usage": {
"prompt_tokens": 8,
"total_tokens": 8
},
"cruxy": {
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"cost": { "tier": "cruxy-embed" }
}
}
```
The `embedding` array is shown truncated; a real vector holds many more
floats. The `cost` object carries the same fields documented under
[Chat](/docs/en/build/chat#usage).
| Field | Meaning |
| --- | --- |
| `object` | Always `"list"`. |
| `data` | One `embedding` entry per input, each with its `index` and `embedding` vector. |
| `usage` | `prompt_tokens` and `total_tokens` for the request. |
| `cruxy` | Additive block with the `request_id` and `cost` (see [Chat](/docs/en/build/chat#the-cruxy-block)). |
The `embedding` array holds the vector as floating-point numbers. Read a
result at `data[i].embedding`, matching `data[i].index` to your input's
position.
## Errors
Embeddings returns `400` for a malformed request, `401` for a bad key,
`429` for `rate_limited` or `budget_exhausted`, and the model failures, which nothing bills: `502` `upstream_error` (the provider
rejected the request; it fails the same way again, so do not retry it), `503`
`upstream_overloaded`, `model_unavailable`, `service_unavailable`,
`server_shutting_down`, or `limits_unavailable`, and `504` `upstream_timeout`.
Retry the `503`s and the `504` with backoff; see
[Embeddings, rerank, and moderations](/docs/en/build/reliability#embeddings-rerank-and-moderations).
[Errors](/docs/en/reference/errors) lists every code.
## Next steps
- [Retrieval-augmented generation](/docs/en/resources/cookbook/rag): index a corpus and answer from it, end to end.
- [Rerank](/docs/en/build/rerank): order retrieved candidates against a query.
- [Chat](/docs/en/build/chat): completions across the tiers.
- [Errors](/docs/en/reference/errors): the error shape.
================================================================================
Page: Rerank
URL: https://learn.cruxyai.com/docs/en/build/rerank
Markdown: https://learn.cruxyai.com/docs/en/build/rerank.md
================================================================================
# Rerank
Order candidate documents against a query with the cruxy-rerank model.
`POST /api/v1/rerank` scores each candidate document against your query
and returns them best-first, so you can pick the strongest matches from a
shortlist. Here is a working request:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/rerank \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "cruxy-rerank",
"query": "What is the capital of France?",
"documents": [
"Paris is the capital and largest city of France.",
"Berlin is the capital of Germany.",
"The Louvre is a museum in Paris.",
"France is in Western Europe."
],
"top_n": 3
}'
```
```ts title="TypeScript"
const documents = [
'Paris is the capital and largest city of France.',
'Berlin is the capital of Germany.',
'The Louvre is a museum in Paris.',
'France is in Western Europe.',
]
const res = await fetch('https://api.cruxy.ai/api/v1/rerank', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'cruxy-rerank',
query: 'What is the capital of France?',
documents,
top_n: 3,
}),
})
const { results } = await res.json()
const top = results[0] // highest relevance_score
const topDocument = documents[top.index]
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
const documents = [
'Paris is the capital and largest city of France.',
'Berlin is the capital of Germany.',
'The Louvre is a museum in Paris.',
'France is in Western Europe.',
]
const { result } = await cruxy.rerank.rerankDocuments({
body: {
model: 'cruxy-rerank',
query: 'What is the capital of France?',
documents,
topN: 3,
},
})
const top = result.results[0] // highest relevanceScore
const topDocument = top && documents[top.index]
```
## The model
`model` must be `"cruxy-rerank"`. It is the only rerank model on the
gateway.
## Not OpenAI-compatible
Most of the gateway mirrors the OpenAI shape. This endpoint does not;
it follows a Cohere-style rerank contract instead. There is no `object`
envelope, and the results carry **indices into your input**, not the
document text. Map each `index` back to your own `documents` array to
recover the text, as the TypeScript example does above.
## Request fields
| Field | Type | Example | Purpose |
| --- | --- | --- | --- |
| `model` | string | `"cruxy-rerank"` | Must be `"cruxy-rerank"`. |
| `query` | string | `"What is the capital of France?"` | Scored against each document. |
| `documents` | array | | Candidate strings to rank. |
| `top_n` | integer | `3` | Cap on how many results come back. Omit to rank every document. |
## Ranking and top_n
Every document is scored against `query`, and the response is sorted by
`relevance_score` in **descending** order, the most relevant document
first. `top_n` caps how many results return; with `top_n: 3` over four
documents, only the top three come back. Omit `top_n` to get all of
them, still ranked.
## Response shape
```json
{
"model": "cruxy-rerank",
"results": [
{ "index": 0, "relevance_score": 0.98 },
{ "index": 2, "relevance_score": 0.71 },
{ "index": 3, "relevance_score": 0.12 }
],
"usage": {
"prompt_tokens": 8,
"total_tokens": 8
},
"cruxy": {
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"cost": { "tier": "cruxy-rerank" }
}
}
```
| Field | Meaning |
| --- | --- |
| `model` | Always `"cruxy-rerank"`. |
| `results` | Ranked entries, `relevance_score` descending, capped to `top_n`. |
| `index` | Position of the document in the request `documents` array. |
| `relevance_score` | Relevance of that document to `query`; higher is more relevant. |
| `usage` | `prompt_tokens` and `total_tokens` for the request. |
| `cruxy` | Additive block with the `request_id` and `cost` (see [Chat](/docs/en/build/chat#the-cruxy-block)). |
Each result's `index` points back into the `documents` you sent, so read
the ranked text as `documents[results[i].index]`. A dropped document
(one below the `top_n` cut) has no entry.
## Where it fits
Rerank and [Embeddings](/docs/en/build/embeddings) are the two retrieval
primitives on the gateway, and they work at different stages. A common
pattern is to retrieve broadly with embeddings, then rerank the top
candidates to settle their final order.
| | Embeddings | Rerank |
| --- | --- | --- |
| Model id | `cruxy-embed` | `cruxy-rerank` |
| What it does | Turns text into vectors you compare with cosine similarity. | Scores each candidate document against the query directly. |
| Best for | A first-pass search over a large corpus. | Ordering a shortlist before you use it. |
| Learn more | [Embeddings](/docs/en/build/embeddings) | [Retrieval-augmented generation](/docs/en/resources/cookbook/rag) |
## Errors
Rerank returns `400` for a malformed request, `401` for a bad key,
`429` for `rate_limited` or `budget_exhausted`, and the model failures, which nothing bills: `502` `upstream_error` (the provider
rejected the request; it fails the same way again, so do not retry it), `503`
`upstream_overloaded`, `model_unavailable`, `service_unavailable`,
`server_shutting_down`, or `limits_unavailable`, and `504` `upstream_timeout`.
Retry the `503`s and the `504` with backoff; see
[Embeddings, rerank, and moderations](/docs/en/build/reliability#embeddings-rerank-and-moderations).
[Errors](/docs/en/reference/errors) lists every code.
## Next steps
- [Retrieval-augmented generation](/docs/en/resources/cookbook/rag): the embeddings-to-chat recipe this endpoint sits in.
- [Embeddings](/docs/en/build/embeddings): the vectors you retrieve candidates with.
- [Models](/docs/en/reference/models): discover tiers at runtime.
- [Errors](/docs/en/reference/errors): the error shape.
================================================================================
Page: Moderations
URL: https://learn.cruxyai.com/docs/en/build/moderations
Markdown: https://learn.cruxyai.com/docs/en/build/moderations.md
================================================================================
# Moderations
Classify text against content-safety categories with the cruxy-guard model.
`POST /api/v1/moderations` checks text against the content-safety
categories and returns whether each one matched, plus an overall
`flagged`. It is OpenAI-compatible. Here is a single-string check:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/moderations \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "cruxy-guard",
"input": "I want to learn how to bake bread."
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/moderations', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'cruxy-guard',
input: 'I want to learn how to bake bread.',
}),
})
const { results } = await res.json()
if (results[0].flagged) {
// handle flagged content
}
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
// The SDK's `input` is an array: send one string as a one-element array.
const { result } = await cruxy.moderations.createModeration({
body: { model: 'cruxy-guard', input: ['I want to learn how to bake bread.'] },
})
if (result.results[0]?.flagged) {
// handle flagged content
}
```
## The model
`model` must be `"cruxy-guard"`. It is the only moderation model on the
gateway.
## Single string or array
`input` accepts a single string or an array of strings. With an array,
`results` holds one entry per input, in order.
## Response shape
```json
{
"id": "modr-a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"model": "cruxy-guard",
"results": [
{
"flagged": false,
"categories": { "example_category": false },
"category_scores": { "example_category": 0.0 }
}
]
}
```
| Field | Meaning |
| --- | --- |
| `flagged` | `true` when the guard flagged the input. Check this first. A flagged result can have every category `false`; `cruxy.hazard_codes` on the result then names the guard's own codes. |
| `categories` | Map of category name to a boolean: whether that category matched. |
| `category_scores` | Map of category name to a score. See the caveat below. |
`cruxy.hazard_codes` holds the guard's raw codes (MLCommons S-codes),
uppercased, in the order the guard emitted them. Nine of the fourteen codes map
onto an OpenAI category. The other five map onto none: `S6` (specialized
advice), `S7` (privacy), `S8` (intellectual property), `S13` (elections), and
`S14` (code interpreter abuse). That is why a flagged result can have every
category `false`. On a flagged result the list is never null and may be empty,
when the guard flagged the input without naming a code; an unflagged result
has no `cruxy` key.
`categories` and `category_scores` share the same keys, one per
moderation category. The keys shown above are illustrative; treat the
category set as whatever the response returns rather than a fixed list.
## category_scores is binary
The scores are **not** calibrated probabilities. The guard model emits
labels, not likelihoods, so each value in `category_scores` is either
`1.0` or `0.0`, the same information as the boolean in `categories`,
expressed as a number. Do not threshold these scores or read them as
confidence. To decide whether to act, use `flagged` (or the per-category
booleans in `categories`). To run this check in front of a chat call, see
the [Moderation gate](/docs/en/resources/cookbook/moderation-gate) recipe.
## Errors
Moderations returns `400` for a malformed request, `401` for a bad key,
`429` for `rate_limited` or a spend cap (`key_spend_cap_exceeded`,
`workspace_spend_cap_exceeded`, `credits_exhausted`), never `budget_exhausted`,
and the model failures, which nothing bills: `502` `upstream_error` (the provider
rejected the request; it fails the same way again, so do not retry it), `503`
`upstream_overloaded`, `model_unavailable`, `service_unavailable`,
`server_shutting_down`, or `limits_unavailable`, and `504` `upstream_timeout`.
Retry the `503`s and the `504` with backoff; see
[Embeddings, rerank, and moderations](/docs/en/build/reliability#embeddings-rerank-and-moderations).
[Errors](/docs/en/reference/errors) lists every code.
## Next steps
- [Moderation gate](/docs/en/resources/cookbook/moderation-gate): screen input before it reaches chat.
- [Chat](/docs/en/build/chat): completions across the tiers.
- [Errors](/docs/en/reference/errors): the error shape.
================================================================================
Page: Feedback
URL: https://learn.cruxyai.com/docs/en/build/feedback
Markdown: https://learn.cruxyai.com/docs/en/build/feedback.md
================================================================================
# Feedback
Report the outcome of a prior call: accepts, rejects, thumbs, test results.
`POST /api/v1/feedback` records how an earlier call turned out (an edit
accepted or rejected, a thumbs rating, or a generated test's result),
attached to that call by its `request_id`. Here is a working request:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/feedback \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"type": "thumbs",
"consent": true,
"payload": { "value": "up" }
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/feedback', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
request_id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
type: 'thumbs',
consent: true,
payload: { value: 'up' },
}),
})
const { stored } = await res.json() // 202 Accepted; see below
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
const { result } = await cruxy.feedback.submitFeedback({
body: {
requestId: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
type: 'thumbs',
consent: true,
payload: { value: 'up' },
},
})
const { stored } = result // 202 Accepted; see below
```
## Joining to a prior call
`request_id` ties the feedback to the call it is about. It is the same
UUID the gateway returns in the `cruxy.request_id` field of a chat,
embeddings, or rerank response. A moderations response carries it as the
top-level `id`, prefixed `modr-`, which you strip; see
[the cruxy block](/docs/en/build/chat#the-cruxy-block). Log that id when you make
a call, then send it back here when you learn the outcome.
## Event types
`type` must be one of:
| Value | |
| --- | --- |
| `edit_accepted` | |
| `edit_rejected` | |
| `thumbs` | |
| `test_result` | |
Each `type` fixes the shape of `payload`, and the gateway validates it,
rejecting unknown keys, wrong types, missing required fields, and bad enum
values with a `400`. `edit_accepted` and `edit_rejected` share one shape;
`thumbs` and `test_result` each have their own. The
[API Reference](/docs/en/reference/api) carries the exact fields for each;
the request above sends a `thumbs` payload.
## Request fields
| Field | Type | Example | Purpose |
| --- | --- | --- | --- |
| `request_id` | string | `"a1b2c3d4-…"` | The prior call this feedback is about. |
| `type` | string | `"thumbs"` | One of the values above. |
| `consent` | boolean | `true` | Whether this event may be stored. See [What gets stored](#what-gets-stored). |
| `payload` | object | | Event data; its shape is fixed by `type` and validated (see [Event types](#event-types)). |
## What gets stored
Feedback is privacy-gated, and the gate is strict. Two things decide
whether an event is kept:
- `consent`: the per-event boolean you send.
- The account's opt-out: a setting outside this request.
The outcomes are:
- **Not consented, or the account has opted out**: the event is
dropped entirely. No row is written and the payload is never stored.
The response has `stored: false`.
- **Consented, and not opted out**: a row is written, but its payload
is redacted. The event is recorded; the payload you sent is not kept
verbatim.
The payload you send is never persisted as-is. A dropped event keeps
nothing; a stored event keeps its payload with secrets and personal data
redacted. Do not rely on
reading the raw payload back later.
## The response
A valid event **always returns `202 Accepted`**, for a stored event and
for a dropped one alike. A `202` means the event was accepted for
processing, not that it was stored, so read `stored` to tell the two
apart.
```json
{
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"stored": true
}
```
| Field | Meaning |
| --- | --- |
| `stored` | `true` if a row was written (payload redacted), `false` if the event was dropped. |
| `id` | Identifier for the stored event. |
## Errors
Feedback returns `400` for a malformed request, `401` for a bad key, and `404`
`request_not_found` for a `request_id` that is not a call your account made.
A privacy drop is **not** an error; it still returns `202` with
`stored: false`. See [Errors](/docs/en/reference/errors).
## Next steps
- [Chat](/docs/en/build/chat#the-cruxy-block): where `request_id` comes from.
- [Errors](/docs/en/reference/errors): the error shape.
================================================================================
Page: Conversations
URL: https://learn.cruxyai.com/docs/en/build/conversations
Markdown: https://learn.cruxyai.com/docs/en/build/conversations.md
================================================================================
# Conversations
Store chat history on the server: create a conversation, append each turn, list and count the shelves, search your own messages, and archive.
A conversation is chat history the gateway stores for you, so a thread can be
reopened on another device. You store each turn yourself: send it to
[Chat](/docs/en/build/chat), then append the user message and the reply.
Conversations are private to their author. No other member of the
organization, an owner or admin included, can read, list, or search one.
## Create a conversation and append a turn
`POST /api/v1/conversations` creates a conversation. Every field is optional:
`title` and `model` are presentation only, `vault_id` holds it inside a vault
you can write to, and `source` is `chat` (the default) or `cli` for a Cruxy
Code session. `POST /api/v1/conversations/{id}/messages` appends one message
with a `role` of `system`, `user`, `assistant`, or `tool`, and its `content`.
```bash title="curl"
# 1. Create the conversation and keep its id.
CONVERSATION_ID=$(curl -s https://api.cruxy.ai/api/v1/conversations \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{}' | sed -E 's/.*"id":"([^"]+)".*/\1/')
# 2. Append the user's message. The first user message titles the conversation.
curl https://api.cruxy.ai/api/v1/conversations/$CONVERSATION_ID/messages \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{ "role": "user", "content": "Why is this query doing a seq scan?" }'
# 3. Append the reply you got from chat, with what you know about it.
curl https://api.cruxy.ai/api/v1/conversations/$CONVERSATION_ID/messages \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{
"role": "assistant",
"content": "The planner chose a seq scan because the estimate is low.",
"status": "complete",
"routed_tier": "saga",
"routing_mode": "explicit",
"reply_ms": 14200,
"input_tokens": 48210
}'
```
```ts title="TypeScript"
const base = 'https://api.cruxy.ai/api/v1'
const headers = {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
}
async function post(path: string, body: unknown, idempotencyKey?: string) {
const res = await fetch(`${base}${path}`, {
method: 'POST',
headers: idempotencyKey ? { ...headers, 'Idempotency-Key': idempotencyKey } : headers,
body: JSON.stringify(body),
})
if (!res.ok) throw new Error(`${path} failed: ${res.status} ${await res.text()}`)
return res.json()
}
// 1. Create the conversation.
const conversation = await post('/conversations', {})
// 2. Append the user's message. The first user message titles the conversation.
const question = 'Why is this query doing a seq scan?'
await post(
`/conversations/${conversation.id}/messages`,
{ role: 'user', content: question },
crypto.randomUUID(),
)
// 3. Ask chat, timing the reply.
const sentAt = Date.now()
const completion = await post('/chat', {
model: 'saga',
stream: false,
messages: [{ role: 'user', content: question }],
})
// 4. Append the reply, with what the response says about it.
const stored = await post(
`/conversations/${conversation.id}/messages`,
{
role: 'assistant',
content: completion.message.content,
status: completion.finish_reason === 'length' ? 'incomplete' : 'complete',
...(completion.finish_reason === 'length' && { incomplete_reason: 'length' }),
routed_tier: completion.cruxy.routed_tier,
routing_mode: 'explicit', // 'auto' when you send model: 'auto'
reply_ms: Date.now() - sentAt,
input_tokens: completion.usage.input_tokens,
},
crypto.randomUUID(),
)
console.log(stored.message.id, stored.conversation.title)
```
The `201` carries the stored `message`, the `conversation` as it now stands,
and `leaf_moved`. The gateway assigns each message's `sequence`, so concurrent
appends cannot collide or reorder the transcript. An append moves the
conversation's `updated_at` and its `retention_until`, the deadline after which
the conversation and its messages are deleted: 365 days from last activity by
default.
Send an `Idempotency-Key` header with every append. A retry with the same key
and body replays the stored `201` instead of writing the turn twice, and the
same key with a different body is `409` `idempotency_conflict`; see
[Errors and retries](/docs/en/build/errors-and-retries#make-retries-idempotent).
`content` is required, except on an assistant message that carries
`tool_calls`, and holds at most 1,048,576 bytes of UTF-8; longer is `400`
`invalid_request`. An agentic turn must carry its tool fields: `tool_calls` on
the assistant message that requested them, and `tool_call_id` on each `tool`
result. A user message can attach up to 20 files in `file_ids`, on a request
that pins `Cruxy-Version: 2026-10-06` or later.
## Record what a reply did
An append can store facts about the reply beside its text, so a client can
show them again when the conversation is reopened. All are optional, each is
written once, and a read omits the ones that were never stored.
| Field | On | Meaning |
| --- | --- | --- |
| `status` | Any message | `complete` (the default), `incomplete` when the answer is not whole, or `cancelled` when a person stopped it. A reply that ended with `finish_reason: "length"` is `incomplete` even though its stream closed cleanly. |
| `incomplete_reason` | An `incomplete` message | Why the answer is not whole: `length`, `content_filter`, `error` (the stream ended in an error), or `connection` (the stream ended with no finish at all). Any other value, or a reason on a message that is not `incomplete`, is `400` `invalid_request`. |
| `routed_tier` | Assistant messages | The tier that answered, as the chat response named it in `cruxy.routed_tier`: `canto`, `saga`, or `epos`. `auto` is `400` `invalid_request`. |
| `routing_mode` | Assistant messages | How the tier was chosen: `explicit` when you named a tier, `auto` when you sent `model: "auto"`, or `auto_degraded`, which the native wire reports in `routing.mode`. |
| `reply_ms` | Assistant messages | How long the reply took by your measure, from sending the chat request to its last token, in whole milliseconds from 0 to 86,400,000. |
| `input_tokens` | Assistant messages | The chat response's `usage.input_tokens` for the request that produced the message, from 0 to 16,777,216. On a turn that called tools, send that one request's count, never a sum. Billing never reads it. |
| `activity` | Assistant messages | A JSON object of your own shape recording what the turn did, at most 65,536 bytes of compact JSON. The gateway stores and returns it as sent, never reads inside it, and never sends it to a model. |
On a read, treat `incomplete_reason` and `routing_mode` as open lists: show a
value you do not know as you would an absent one. To show how full the context
window is, divide the newest reply's `input_tokens` by the `context_window`
that `GET /api/v1/limits` reports for its tier.
## Titles
Every conversation carries `title` and `title_source`, which says who wrote
the title:
| `title_source` | Meaning |
| --- | --- |
| `auto` | Derived from the first user message, in the same append: its first line, truncated to 80 characters on a word boundary. |
| `generated` | Written by the gateway a few seconds after the first reply, replacing an `auto` title with one of 2 to 6 words. This happens for a `chat` conversation written through a first-party login. |
| `user` | Set at create or with `PATCH /api/v1/conversations/{id}`. It is never replaced. |
| `null` | The conversation is untitled, or was titled before the field existed. |
A client that drew an `auto` title reads the conversation again shortly after
a reply to pick up a generated one. `PATCH /api/v1/conversations/{id}` changes
`title`, `model`, or both; an explicit `null` clears a field, and an empty
patch is `400` `invalid_request`.
## List and count
`GET /api/v1/conversations` lists your own conversations, most recently active
first, without their messages. Page with `limit` (default 50, at most 100) and
the `cursor` from the previous page's `next_cursor`, which is `null` on the
last page. Three filters narrow it: `vault_id`, `archived` (`false`, the
default, lists live conversations; `true` lists the archived shelf), and
`source` (`chat`, the default, `cli`, or `agent`).
`GET /api/v1/conversations/counts` counts both shelves in one read: `live` is
how many conversations the listing pages through with `archived=false`, and
`archived` how many with `archived=true`. It applies the listing's own
visibility and its `vault_id` and `source` filters, so a count never includes
a conversation the list cannot return. It takes no `archived` parameter.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/conversations/counts \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/conversations/counts', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
if (!res.ok) throw new Error(`counts request failed: ${res.status}`)
const counts = await res.json()
console.log(`${counts.live} live, ${counts.archived} archived (${counts.source})`)
```
```json
{ "object": "conversation_counts", "source": "chat", "live": 42, "archived": 7 }
```
## Read a conversation
`GET /api/v1/conversations/{id}` returns the conversation and a page of its
messages under `messages`. Page with `message_limit` (default 100, at most
200) and the `message_cursor` from `messages.next_cursor`. `message_order` is
`asc` (the default, oldest first) or `desc`, which returns the newest messages
first; a cursor works only with the order it was issued for.
A conversation can branch: an append that names a `parent_id` answers that
message instead of the newest one, which is how an edit or a regeneration is
stored. A read returns one path through the tree, the active branch on a first
page, and each message lists its `sibling_ids`.
`POST /api/v1/conversations/{id}/switch` with a `message_id` moves the
conversation onto the branch through that message for every device, and
returns the branch's `leaf_id`. To read another branch without switching to
it, pass `message_branch` on the read.
## Search your messages
`GET /api/v1/conversations/search` runs a full-text search over your own
messages, most relevant first. It sees exactly what the listing sees, so it
never matches another person's message. Archived conversations are searched
too, and so is every branch of a branched conversation.
```bash title="curl"
curl -G https://api.cruxy.ai/api/v1/conversations/search \
-H "Authorization: Bearer $CRUXY_API_KEY" \
--data-urlencode 'q="seq scan" -vacuum' \
--data-urlencode 'limit=10'
```
```ts title="TypeScript"
const params = new URLSearchParams({ q: '"seq scan" -vacuum', limit: '10' })
const res = await fetch(`https://api.cruxy.ai/api/v1/conversations/search?${params}`, {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
if (!res.ok) throw new Error(`search failed: ${res.status}`)
const escapeHtml = (s: string) =>
s.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"')
const { data } = await res.json()
for (const hit of data) {
// Escape the whole snippet first, then turn the markers into markup.
const html = escapeHtml(hit.snippet)
.replaceAll('[[hl]]', '')
.replaceAll('[[/hl]]', '')
console.log(hit.conversation_id, hit.message_id, hit.on_active_path, html)
}
```
`q` is required and takes web-search syntax: quoted phrases, `or`, and a
leading `-` to exclude a term. No input is a syntax error. `limit` defaults to
20, at most 50, and `source` filters as it does on the listing.
Each hit carries `conversation_id`, `conversation_title`, `message_id`,
`role`, `sequence`, `status`, `created_at`, `rank`, `on_active_path`, and a
`snippet`.
> **Warning:** A `snippet` is not HTML. It mixes raw text a person wrote with the literal
> markers `[[hl]]` and `[[/hl]]` around matched terms. Escape the whole snippet
> first and substitute the markers afterwards, or strip the markers if you do
> not highlight.
`on_active_path` is `false` for a hit on a branch that is not the active one.
To open it, send its `message_id` to
`POST /api/v1/conversations/{id}/switch`.
## Archive, restore, and delete
- `PUT /api/v1/conversations/{id}/archive` archives a conversation: it leaves
the default listing and appears only with `archived=true`. Nothing is
deleted, and archived conversations are still searched. Archiving does not
extend the retention deadline.
- `DELETE /api/v1/conversations/{id}/archive` restores it to the default
listing.
- `DELETE /api/v1/conversations/{id}` deletes the conversation and every
message in it, immediately and irreversibly. Files its messages attached as
chat attachments are deleted with it, unless a message in another
conversation still attaches them.
Both archive calls are idempotent and answer `204`.
## Append a run of messages
`POST /api/v1/conversations/{id}/messages/batch` appends up to 100 messages in
one transaction: either every message is stored or none is. It is built for
copying in a transcript you already have. Each message is validated as a
single append validates one, every message after the first answers the one
before it, and `file_ids` is not accepted. The body may be at most 1 MiB;
send a longer transcript as several batches. Send an `Idempotency-Key` here
too.
## Errors
| Status | `code` | When |
| --- | --- | --- |
| `400` | `invalid_request` | A field is malformed, out of range, or set on a role that cannot carry it. |
| `403` | `conversation_forbidden` | A write to a conversation held in a vault you may read but not write. |
| `404` | `conversation_not_found` | The id is not a conversation of yours. Another member's conversation reads the same as one that never existed. |
| `404` | `message_not_found` | A `message_id` or `message_branch` that is not a message of this conversation. |
| `409` | `idempotency_conflict` | An `Idempotency-Key` reused with a different body. |
| `413` | `payload_too_large` | The request body is over the route's limit. Nothing is stored. |
See [Errors and retries](/docs/en/build/errors-and-retries) for the envelope
and what to retry.
## Next steps
- [Chat](/docs/en/build/chat): send the turn whose messages you store here.
- [Streaming](/docs/en/build/streaming): read a reply's `finish_reason`, the source of `status` and `incomplete_reason`.
- [Rate limits and budgets](/docs/en/build/rate-limits): the `context_window` that `input_tokens` is measured against.
- [API Reference](/docs/en/reference/api): every conversation operation with its exact request and response shape.
================================================================================
Page: Files
URL: https://learn.cruxyai.com/docs/en/build/files
Markdown: https://learn.cruxyai.com/docs/en/build/files.md
================================================================================
# Files
Upload a file, read its metadata, download its content, and attach it to a chat turn with file_ids. Chat reads text, PDF, Word, and image files.
A file is content you upload once and name by id afterwards. A chat message
attaches files with `file_ids`, and the gateway reads each one into the turn.
A file belongs to you, within the organization your key names: only you can
list, read, or delete it. To hold a file where a team can read it, put it in a
vault; see [Vaults](/docs/en/build/vaults#files-in-a-vault).
## Upload a file
`POST /api/v1/files` takes `multipart/form-data` with the content in a part
named `file`. The upload is rate-limited and not billed.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/files \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-F "file=@report.pdf" \
-F "purpose=general"
```
```ts title="TypeScript"
import { openAsBlob } from 'node:fs'
const form = new FormData()
form.append('file', await openAsBlob('report.pdf'), 'report.pdf')
form.append('purpose', 'general')
const res = await fetch('https://api.cruxy.ai/api/v1/files', {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
body: form,
})
if (!res.ok) throw new Error(`upload failed: ${res.status} ${await res.text()}`)
const file = await res.json()
console.log(file.id, file.content_type, file.text_status)
```
```ts title="SDK"
import { openAsBlob } from 'node:fs'
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
const { result: file } = await cruxy.files.uploadFile({
body: { file: await openAsBlob('report.pdf'), purpose: 'general' },
})
console.log(file.id, file.contentType, file.textStatus)
```
The `201` describes the stored file:
| Field | Meaning |
| --- | --- |
| `id` | The id you name in `file_ids` and in every other file call. |
| `filename` | The name the upload carried. The content is never addressed by it. |
| `bytes` | The size of the content. |
| `content_type` | The media type the gateway detected from the content's first bytes. The `Content-Type` you declared on the part is ignored. |
| `purpose` | The label you sent: a short token of letters, digits, `.`, `_`, and `-`. It defaults to `general`. |
| `expires_at` | When the file stops existing, or `null` for a file kept until you delete it. |
| `text_status`, `text_tokens` | For a PDF or Word document, what reading its text at upload found. Both are `null` for every other file. |
Two optional fields change how long a file lives:
- `expires_in` gives the file a lifetime in seconds, from 60 to 604800 (7
days). Once it passes, the file is gone to every read and to chat, and its
bytes are deleted within 15 minutes. Setting it requires
`Cruxy-Version: 2026-10-07` or later; without the pin, or with a value out
of range, the upload is `400` `invalid_request` and nothing is stored.
- `purpose=chat_attachment` marks a file uploaded to be attached to a stored
conversation message. One that no message has attached 24 hours after upload
is deleted. See [Conversations](/docs/en/build/conversations).
Without either, a file lives until you delete it.
## Documents
A PDF or a Word document (`.docx`) has its text read once, as part of the
upload, so chat can read it like a text file. Reading never fails the upload:
the file is stored whatever it holds, and `text_status` says what was found.
| `text_status` | Meaning |
| --- | --- |
| `extracted` | Chat can read the document. `text_tokens` is the size of its text, by a characters-over-four estimate. |
| `no_text` | No text was found, as in a scanned PDF. |
| `encrypted` | The document needs a password. |
| `corrupt` | The document is damaged, or uses a feature the reader does not support. |
| `too_long` | The text is over 1 MiB and is not kept. `text_tokens` says how much there was. |
| `too_many_pages`, `too_big` | The document is over the reader's bounds: 500 pages, or 64 MiB. |
| `failed` | Reading ran out of time or memory. |
| `busy` | Every reader stayed busy while the document waited. Nothing is wrong with the file: upload it again and it is read. |
Chat refuses a turn that attaches a document with any status but `extracted`.
A legacy Word 97-2003 document (`.doc`) is stored and not read.
## Attach files to a chat turn
Name the files on a user message in `file_ids`. The gateway places each one
in the turn immediately before that message. The request must pin
`Cruxy-Version: 2026-10-05` or later; without the pin, `file_ids` is `400`
`invalid_request` with `required_version`.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-H "Cruxy-Version: 2026-10-05" \
-d "{
\"model\": \"saga\",
\"stream\": false,
\"messages\": [
{
\"role\": \"user\",
\"content\": \"List the three main findings.\",
\"file_ids\": [\"$FILE_ID\"]
}
]
}"
```
```ts title="TypeScript"
const fileId = process.env.FILE_ID // the id an upload returned
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
'Cruxy-Version': '2026-10-05',
},
body: JSON.stringify({
model: 'saga',
stream: false,
messages: [
{ role: 'user', content: 'List the three main findings.', file_ids: [fileId] },
],
}),
})
if (!res.ok) throw new Error(`chat failed: ${res.status} ${await res.text()}`)
const completion = await res.json()
console.log(completion.message.content)
// Files on earlier messages that this turn left out, and why.
console.log(completion.cruxy.files_omitted?.items ?? [])
```
What chat reads:
- **Text.** A UTF-8 text file, as it is.
- **Documents.** A PDF or Word document, as the text read from it at upload,
measured and billed as that text.
- **Images.** A JPEG, PNG, or WebP image, on a tier that reads images:
`supports_vision` in `GET /api/v1/models`. `model: "auto"` routes a turn
with an image to a tier that does. You do not need to resize an image first.
The rules for a turn:
- `file_ids` is valid on user messages only, on `POST /api/v1/chat` and
`POST /api/v2/chat`. `POST /v1/chat/completions` and
`POST /api/v1/tokens/count` refuse it.
- The messages after the last assistant message may name at most 20 distinct
files, and each must be readable, or the request fails with one of the
codes below.
- A file on an earlier message never fails the request. It is sent again
while it still resolves and fits, newest first; otherwise it is left out,
the model is told so, and the response lists it in `cruxy.files_omitted`
with a `reason`, such as `not_found`, `over_budget`, `context_window`, or
`no_vision`. More reasons may be added, so read one you do not know as "left
out".
- A file's tokens are input like any other. They are billed in full on the
turn that attaches the file, and again as history on each later turn that
replays the message; see [Usage and cost](/docs/en/build/usage-and-cost).
A file on the current turn that cannot be read fails the request:
| Status | `code` | When |
| --- | --- | --- |
| `404` | `file_not_found` | An id that is not a file of yours. |
| `400` | `file_not_text` | A file that is not text, a document whose text could not be read, or an image that does not open. |
| `400` | `file_too_large` | A file over the token caps, a PDF too big to read, an image over 15,000,000 bytes or with one side over 200 times the other, or images over 30,000,000 bytes together. |
| `400` | `image_not_supported` | An image on a tier that does not read images. |
## List, read, and download
`GET /api/v1/files` lists your files, newest first. Page with `limit` (default
20, at most 100) and the `cursor` from the previous page's `next_cursor`,
which is `null` on the last page. `total` is how many files the listing pages
through. `GET /api/v1/files/{id}` returns one file's metadata. Both name the
conversations of yours a file is attached in, under `conversations`.
`GET /api/v1/files/{id}/content` streams the file's raw bytes. The response is
always `application/octet-stream` with `Content-Disposition: attachment`, so
content is never rendered inline.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/files/$FILE_ID/content \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-o report-copy.pdf
```
```ts title="TypeScript"
import { writeFile } from 'node:fs/promises'
const fileId = process.env.FILE_ID // the id an upload returned
const res = await fetch(`https://api.cruxy.ai/api/v1/files/${fileId}/content`, {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
if (!res.ok) throw new Error(`download failed: ${res.status}`)
await writeFile('report-copy.pdf', Buffer.from(await res.arrayBuffer()))
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
const { result: files } = await cruxy.files.listFiles({ limit: 20 })
for (const file of files.data) {
console.log(file.id, file.filename, file.bytes)
}
```
A file that belongs to another user, or to another of your own organizations,
answers `404` `file_not_found` on every one of these calls, the same as an id
that never existed.
## Delete a file
`DELETE /api/v1/files/{id}` deletes the file and its content, and answers
`200` with `deleted: true`. A repeat delete is `404` `file_not_found`.
## Limits
Your plan sets how many files and images you may upload a month, the largest
single file, and a storage pool. An upload past one of them is refused before
a byte is stored: `429` `file_limit_reached` or `image_limit_reached`, or
`413` `payload_too_large` or `storage_quota_exceeded`.
[Plan limits](/docs/en/build/plan-limits#uploads-and-storage) has each plan's
numbers, and `GET /api/v1/limits` reports yours as `files`, `images`,
`storage`, and `plan_limits`.
## Next steps
- [Vaults](/docs/en/build/vaults): hold files where every member of a vault can read them.
- [Chat](/docs/en/build/chat): the request a file is attached to.
- [Conversations](/docs/en/build/conversations): keep a message's files with the stored conversation.
- [Errors and retries](/docs/en/build/errors-and-retries): the error envelope and what to retry.
================================================================================
Page: Choosing a tier
URL: https://learn.cruxyai.com/docs/en/build/choosing-a-tier
Markdown: https://learn.cruxyai.com/docs/en/build/choosing-a-tier.md
================================================================================
# Choosing a tier
Compare canto, saga, and epos by profile, output cap, deadline, and reasoning default, then pick the tier that fits each call.
You choose a tier on every chat request with the `model` field. A tier is a
stable public id: the gateway resolves it to an upstream model on the server
and never exposes which one, so the tier is the contract your code depends on.
`GET /api/v1/models` returns the live catalog, so read it at runtime instead of
hardcoding what this page shows:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/models \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/models', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
const { data } = await res.json()
for (const tier of data) {
console.log(tier.id, tier.label, tier.profile, tier.max_output, tier.reasoning)
}
```
```ts title="SDK"
import { Cruxy } from '@cruxy/api'
const cruxy = new Cruxy({ bearerAuth: process.env.CRUXY_API_KEY })
const { result } = await cruxy.models.listModels()
for (const tier of result.data) {
console.log(tier.id, tier.label, tier.profile, tier.maxOutput, tier.reasoning)
}
```
## The three tiers
Each tier has a display name (`label`) and a one-word `profile` that names its
role. The profile is a qualitative descriptor, not a performance number.
The former ids `kavi`, `vaani`, and `mira` still work; see
[Deprecated aliases](/docs/en/reference/models#deprecated-aliases).
| Tier | Label | Profile | Output cap | Non-streaming deadline | Structured output |
| --- | --- | --- | --- | --- | --- |
| `canto` | Canto | Deep | 65,536 tokens | 90 s | Yes |
| `saga` | Saga | Balanced | 131,072 tokens | 90 s | Yes |
| `epos` | Epos | Fast | 131,072 tokens | 45 s | Yes |
- **Output cap** is the tier's `max_output`. A `max_tokens` you send above it,
or no `max_tokens` at all, is clamped to the cap, so the gateway never
generates more than the cap.
- **Non-streaming deadline** bounds a `stream: false` call. The gateway makes
one attempt; a call that runs past the deadline is cancelled and answered
`504` `upstream_timeout`, and nothing is billed. A streaming call may run for
up to five minutes.
- **Structured output** is the tier's `supports_structured_output`: whether it
honours `response_format` `json_object` and `json_schema`. A structured
request that names a tier without it is `422`
`structured_output_unsupported`. With `model: "auto"`, a structured request
is routed only to a tier that has it; see
[Ask for structured output](/docs/en/best-practices/prompting#ask-for-structured-output).
`saga` and `epos` read images (`supports_vision` is `true` on each); `canto`
does not, so an explicit `canto` turn that attaches an image is `400`
`image_not_supported`, and `auto` routes a turn with an image to `saga` or
`epos`. Every tier's `context_window` is 1,048,576 tokens, and your plan holds
a turn to its own window, 256,000 on Free; see
[Plan limits](/docs/en/build/plan-limits). Read each tier's `context_window`,
`latency_target_ms`, `supports_tools`, and `pricing` from the catalog
response.
## How each tier reasons
A tier can think before it answers. Each catalog entry's `reasoning` object
gives the effort a request gets when it sends no `reasoning_effort`
(`default_effort`) and whether `low`, `medium`, and `high` are different depths
on that tier (`graded`):
| Tier | Profile | `default_effort` | `graded` | What that means |
| --- | --- | --- | --- | --- |
| `canto` | Deep | `high` | `true` | Thinks deeply by default; `low` and `medium` think less. |
| `saga` | Balanced | `low` | `true` | Thinks lightly by default; `medium` and `high` think longer. |
| `epos` | Fast | `none` | `true` | Answers without thinking unless you ask; `low`, `medium`, and `high` are graded. |
These are the shipped defaults; the catalog reports the live ones.
[Reasoning effort](/docs/en/build/reasoning-effort) covers every value and when
to change it.
> **Warning:** `canto` thinks by default, and its thinking spends the same `max_tokens` budget
> as the answer without coming back as content. A small `max_tokens` on `canto`
> can run out before any answer is written: the turn ends with
> `finish_reason: "length"` and empty content. Send `reasoning_effort: "none"`
> for a short, direct answer, or leave `max_tokens` unset to keep the tier's cap.
## Pick a tier for the job
| | Canto | Saga | Epos |
| --- | --- | --- | --- |
| Model id | `canto` | `saga` | `epos` |
| Reach for it when | The task needs multi-step reasoning. It thinks deeply by default. Text only: it does not read images. | Everyday calls. It thinks lightly by default, and its graded effort gives you more depth when a request needs it. | Short, direct calls. It answers without thinking unless you ask. |
| Watch for | Thinking spends `max_tokens`. Leave `max_tokens` unset or large, or send `reasoning_effort: "none"` for a direct answer. | An answer past its 131,072-token output cap ends with `finish_reason: "length"`. It reads images. | A 45 s non-streaming deadline: stream a long turn. It reads images. On a signed-in plan, `epos` has a monthly request cap of its own. |
[Rate limits and budgets](/docs/en/build/rate-limits) shows how to read the
`epos` request cap and the other budgets in `GET /api/v1/limits`, where it sits
in the signed-in plan's `token_pool` as `token_pool.epos`. The same cap is also
under its former name, `token_pool.mira`, so existing code keeps working.
## Pin a tier or let the router choose
Name a tier when you need its behavior to hold on every call: the same output
cap, deadline, and reasoning default. Send `model: "auto"` when your requests
vary and you would rather the gateway pick per request;
[Auto routing](/docs/en/build/auto-routing) covers what the router guarantees
and how to read the tier it chose.
A developer API key can carry a model allowlist. A request for a tier the
allowlist excludes is `403` `model_not_allowed`; see
[Get your API key](/docs/en/get-started/api-keys#what-a-key-is-allowed-to-do).
## Next steps
- [Auto routing](/docs/en/build/auto-routing): let the gateway pick the tier per request.
- [Reasoning effort](/docs/en/build/reasoning-effort): control how much a tier thinks.
- [Models](/docs/en/reference/models): every field in the catalog response.
- [Chat](/docs/en/build/chat): send your first request to a tier.
- [Tier and effort for cost and latency](/docs/en/best-practices/tiers-and-effort): a default tier and effort for each kind of call.
================================================================================
Page: Auto routing
URL: https://learn.cruxyai.com/docs/en/build/auto-routing
Markdown: https://learn.cruxyai.com/docs/en/build/auto-routing.md
================================================================================
# Auto routing
Send model auto and the gateway picks a tier for each request before billing; read cruxy.routed_tier to see which tier served it.
Send `model: "auto"` instead of a tier id and the gateway's server-side router
picks the tier from the request content. It resolves `auto` to `canto`, `saga`,
or `epos` before billing and before the upstream call, so `auto` never reaches
a model, and every response names the tier that served it.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "auto",
"stream": false,
"messages": [
{ "role": "user", "content": "Summarize the causes of the monsoon in three sentences." }
]
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'auto',
stream: false,
messages: [
{ role: 'user', content: 'Summarize the causes of the monsoon in three sentences.' },
],
}),
})
const completion = await res.json()
console.log(completion.cruxy.routed_tier) // the tier that served the call
console.log(completion.cruxy.reasoning_effort) // the effort it ran at
console.log(completion.message.content)
```
## Read the tier that served
The routed tier comes back on every response, in a field that depends on the
endpoint:
| Endpoint | Where the routed tier is |
| --- | --- |
| `POST /api/v1/chat` | `cruxy.routed_tier`. On a stream it rides the role primer, the first frame, before any model output. |
| `POST /api/v2/chat` | `routing.tier`, with `routing.mode` set to `explicit`, `auto`, or `auto_degraded`. On a stream it rides the `start` frame. |
| `POST /v1/chat/completions` | The response's `model`. |
`usage.cost.tier` also names the tier that served the request. The router can
choose differently for two similar requests, so log `routed_tier` with each
`request_id` and branch on the value you read, never on the tier you expected.
```json
{
"cruxy": {
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"routed_tier": "canto",
"reasoning_effort": "high"
}
}
```
## What auto does for you
- **Picks a healthy tier.** The router chooses a tier that is available before
the call is sent. Once sent, a call never moves to another tier. When every
tier is unavailable, the request is `503` `all_models_unavailable`; retry it
with backoff.
- **Chooses the reasoning effort.** With `reasoning_effort` omitted, `auto`
chooses the effort per request and echoes it as `cruxy.reasoning_effort`
(`routing.reasoning_effort` on the native wire). Send `reasoning_effort` to
set it yourself.
- **Routes images to a tier that reads them.** A turn whose messages attach an
image goes to a tier with `supports_vision`: `saga` or `epos`, never `canto`.
- **Routes within your plan.** The pick, and every move above, stays among
the tiers your plan includes (`models` on `GET /api/v1/limits`), so `auto`
on Free is always served `saga`; see [Plan limits](/docs/en/build/plan-limits).
- **Moves to `epos` instead of failing a budget.** When a budget would refuse
the request and `epos` can serve it, the gateway moves an `auto` request to
`epos` rather than answering `429` `budget_exhausted`. On the native wire,
`routing.mode` is `auto_degraded`.
[Rate limits and budgets](/docs/en/build/rate-limits) explains the budgets.
## Size max_tokens for any tier
`max_tokens` is clamped to the cap of the tier the router picks: 65,536 on
`canto`, and 131,072 on `saga` and `epos`. If you set a small `max_tokens` on
an `auto` request, also send `reasoning_effort`. Otherwise the router may pick
`canto`, whose default thinking can spend the whole budget and end the turn with
`finish_reason: "length"` and empty content; see
[Reasoning effort](/docs/en/build/reasoning-effort).
## When to name a tier instead
- **You need the same behavior on every call:** one output cap, one
non-streaming deadline, one reasoning default.
- **You are measuring.** An evaluation or benchmark compares like with like,
so pin the tier it measures.
- **You count tokens first.** `POST /api/v1/tokens/count` rejects `auto` with
`400`; count against the tier you plan to send.
[Choosing a tier](/docs/en/build/choosing-a-tier) compares the three tiers.
## Next steps
- [Choosing a tier](/docs/en/build/choosing-a-tier): what each tier is for.
- [Reasoning effort](/docs/en/build/reasoning-effort): set the effort instead of letting `auto` choose.
- [Streaming](/docs/en/build/streaming): read the routed tier from the role primer.
- [Errors and retries](/docs/en/build/errors-and-retries): retry `503` `all_models_unavailable`.
================================================================================
Page: Reasoning effort
URL: https://learn.cruxyai.com/docs/en/build/reasoning-effort
Markdown: https://learn.cruxyai.com/docs/en/build/reasoning-effort.md
================================================================================
# Reasoning effort
Set how much a tier thinks before it answers, from none to high, and keep hidden thinking from spending your whole max_tokens budget.
`reasoning_effort` caps the hidden thinking a tier does before it answers. You
send one of four values on a chat request, or omit it and the tier applies its
default. The effort the call ran at comes back on the response as
`cruxy.reasoning_effort`.
This request asks `canto` for a one-line answer, so it turns thinking off and
keeps the budget small:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "canto",
"stream": false,
"reasoning_effort": "none",
"max_tokens": 256,
"messages": [
{ "role": "user", "content": "Is Mumbai north or south of Chennai? Answer in one sentence." }
]
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'canto',
stream: false,
reasoning_effort: 'none',
max_tokens: 256,
messages: [
{ role: 'user', content: 'Is Mumbai north or south of Chennai? Answer in one sentence.' },
],
}),
})
const completion = await res.json()
console.log(completion.cruxy.reasoning_effort) // "none"
console.log(completion.message.content)
```
## The four values
| Value | Effect |
| --- | --- |
| `none` | Turns thinking off. The tier answers directly. |
| `low`, `medium`, `high` | Turn thinking on. On a graded tier the three are different depths, from `low` to `high`; on an on-or-off tier each one turns thinking on. |
Any other value is `400`. The field is accepted on `POST /api/v1/chat`,
`POST /api/v2/chat`, and `POST /v1/chat/completions`.
## Defaults per tier
Omit `reasoning_effort` and each tier applies its own default. `graded` says
whether `low`, `medium`, and `high` are different depths on the tier. Every
tier is graded today; on a tier where it is `false` the control is on or off:
`none` turns thinking off, every other value turns it on, `low` thinks as long
as `high`, and only `max_tokens` bounds it.
| Tier | Profile | Default | `graded` |
| --- | --- | --- | --- |
| `canto` | Deep | `high` | `true` |
| `saga` | Balanced | `low` | `true` |
| `epos` | Fast | `none` | `true` |
With `model: "auto"`, the router chooses the effort per request unless you send
one. These are the shipped defaults; `GET /api/v1/models` reports each tier's
live `reasoning.default_effort` and `reasoning.graded`, read from the same
configuration the chat endpoints apply.
## Thinking spends max_tokens
A tier that thinks generates its thinking inside the same output budget as its
answer. On `POST /api/v1/chat` and `POST /v1/chat/completions`, the thinking is
not returned as content. So a small `max_tokens` on a tier that thinks can be
used up before the answer starts.
> **Warning:** `canto` thinks by default (`high`), so `canto` with a small `max_tokens` and no
> `reasoning_effort` can return nothing: the turn ends with
> `finish_reason: "length"` and empty content. For a short, direct answer, send
> `reasoning_effort: "none"`. To
> keep the thinking, leave `max_tokens` unset, which clamps it to the tier's cap,
> or set it large.
Check for the trap when you read a response, so a truncated turn never reaches
your user as an empty answer:
```ts
const { message, finish_reason } = completion
if (finish_reason === 'length' && !message.content) {
// The budget ran out before the answer began. Resend with
// reasoning_effort: 'none', or with max_tokens unset.
}
```
## When to send none
Send `none` when the answer needs no deliberation and you want it fast:
- **Short, direct answers:** a classification, a yes or no, an extracted field,
a one-line reply.
- **A small `max_tokens`:** any time you cap the output tightly, so thinking
cannot consume the cap.
- **A turn that times out:** a `stream: false` call is bounded by the tier's
deadline and answered `504` `upstream_timeout` past it. Lowering
`reasoning_effort` or `max_tokens` shortens the turn; streaming lifts the
limit to five minutes.
Keep thinking on when the answer needs deliberation, and leave `max_tokens`
unset there so the thinking and the answer share the tier's full cap.
## See the thinking
The thinking is never part of the answer's content, and two of the three chat
wires return it beside the answer. Either way it is present only when the tier
reasoned.
- `POST /api/v1/chat` returns it as `cruxy.reasoning`, from
`Cruxy-Version: 2026-11-12` and when a request sends no `Cruxy-Version`. A
request pinned to an earlier date never receives it. A non-streaming response
carries the whole trace; a stream sends it as reasoning frames, one fragment
each. See [Streaming](/docs/en/build/streaming#reasoning-frames).
- `POST /api/v2/chat`, the native wire, carries a top-level `reasoning` string
on a non-streaming response, and a stream sends the thinking as `reasoning`
events, separate from the answer's `delta` events. It is the same field as
`cruxy.reasoning`, with the same meaning.
- `POST /v1/chat/completions` does not return the thinking.
Whichever wire you read it on:
- It is not part of the answer, and it sits outside `message` on purpose. Do
not send it back in the conversation.
- There is no separate reasoning-token count. `usage.output_tokens` already
includes the thinking, undivided, on every API version, so receiving the
trace changes no figure and no bill.
- The effort is echoed as `cruxy.reasoning_effort`, and as
`routing.reasoning_effort` on the native wire.
A stream can also carry thought lines, short plain-text notes on what each
part of the trace is doing. Read `cruxy.reasoning_summaries` on
`GET /api/v1/models` to learn whether the deployment sends them; when it is
`true`, a stream from a signed-in credential that pins
`Cruxy-Version: 2026-11-13` or later gets them, and a developer or headless
key never does. See [Streaming](/docs/en/build/streaming#thought-lines).
## Next steps
- [Choosing a tier](/docs/en/build/choosing-a-tier): each tier's profile, output cap, and default.
- [Auto routing](/docs/en/build/auto-routing): let the router choose the tier and the effort.
- [Streaming](/docs/en/build/streaming): read the stream and its `finish_reason`.
- [Errors and retries](/docs/en/build/errors-and-retries#timeouts): what to do about `504` `upstream_timeout`.
- [Tier and effort for cost and latency](/docs/en/best-practices/tiers-and-effort): pick an effort by cost and latency.
================================================================================
Page: Memory
URL: https://learn.cruxyai.com/docs/en/build/memory
Markdown: https://learn.cruxyai.com/docs/en/build/memory.md
================================================================================
# Memory
Let chat recall stored facts and capture new ones. Memory is on by default for signed-in keys and off for developer keys until a request opts in.
Memory gives chat two independent abilities, each set per request in the
`memory` object. **Recall** reads the stored memories most relevant to the turn
into it as background context. **Capture** lets the gateway store a durable
fact from the turn for later ones. When a request sends no `memory` object, the
key decides whether each one runs.
## Defaults depend on the key
| Key | Recall and capture when `memory` is omitted | `bucket` in `GET /api/v1/limits` |
| --- | --- | --- |
| Signed-in credential: device login, OAuth login, and the Cruxy web app, desktop, CLI, and mobile | On | `subscription` |
| Developer API key, created in the Cruxy Console | Off | `apikey` |
| Headless key | Off | `headless` |
A key minted by device login or OAuth login is a signed-in credential whichever
client ran the flow, a third-party tool included. A developer key's memory is
off because the key often serves people other than its owner: recall would put
the owner's stored memory into their turns, and capture would store what they
say. Use a developer API key for a server; see
[Get your API key](/docs/en/get-started/api-keys#two-kinds-of-key).
## Opt in from a developer key
Send `true` for each side you want. This request comes from an internal tool
that only the key's owner uses, so it recalls the owner's memory and lets the
turn store a new fact:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "saga",
"stream": false,
"memory": { "recall": true, "capture": true },
"messages": [
{ "role": "user", "content": "I work in IST. When should our weekly sync with the London team be?" }
]
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'saga',
stream: false,
memory: { recall: true, capture: true },
messages: [
{
role: 'user',
content: 'I work in IST. When should our weekly sync with the London team be?',
},
],
}),
})
const completion = await res.json()
console.log(completion.message.content)
console.log(completion.cruxy.memory_recall) // what was recalled, or undefined
console.log(completion.cruxy.memory_capture) // what was stored, or undefined
```
`true` needs a key whose allowed scopes include the `memory` family. An
unscoped key has every family. A key without it gets `403`
`key_scope_forbidden`, refused before anything is billed:
| Error field | Value |
| --- | --- |
| `code` | `key_scope_forbidden` |
| `required_scope` | `memory` |
| `allowed_scopes` | The key's own families, for example `["chat"]` |
A `403` never clears by waiting. A key whose allowed scopes omit `memory` can
never use memory through chat, which makes it the key to issue for a service
that must never see anyone's memory.
> **Warning:** Never store secrets in memory, and never enable capture on anonymous or public
> traffic. Memory has no expiry: a stored value stays, and recall can place it in
> later turns, until someone deletes it. The capture filter drops content that
> carries an email address, a phone number, or a card number; it does not
> promise to catch a password, an API key, or a token. Capture on public traffic
> stores what strangers say into the key owner's memory.
## What recall adds
Recall draws on the same resolved view `GET /api/v1/memories` lists: your
personal memory, your organization's, and every vault you can read whose
`memory_scope` is `default`.
- **`vault_id`** names the vault a chat is inside. Inside a `vault_only` vault,
recall draws on that vault's memory alone, without personal or organization
memory. A `vault_id` you cannot read is treated as if you sent none, never
reported.
- **`mentioned_vault_ids`** adds up to three vaults the turn references, such
as an `@VersionGate` your picker resolved. Mentions share the turn's recall
budget rather than enlarging it, and never make a vault writable.
- **Billing.** The recalled block is billed input, so input tokens fall by its
size when recall is off.
The response reports what was recalled in `cruxy.memory_recall`: the scope and
key of each memory, and for a vault memory its `vault_id` (and `vault_name`
when it resolves). It never includes the stored value, and the field is absent
when nothing was recalled.
```json
{
"cruxy": {
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"routed_tier": "saga",
"reasoning_effort": "low",
"memory_recall": {
"count": 1,
"items": [{ "scope": "personal", "key": "timezone" }]
}
}
}
```
## What capture stores
After the turn is answered, capture decides whether the exchange held a durable
fact, such as a stated preference, a stable setting, or a constraint about your
work, and stores it.
- **Where it goes.** Your personal memory, or the vault the request's
`vault_id` names when you can write to it. A vault you can only read demotes
the capture to your personal memory. Capture never writes
organization-wide memory, whatever your role.
- **What it drops.** Content carrying an email address, a phone number, or a
card number is dropped before any write.
- **What it reports.** Every capture appears in `cruxy.memory_capture`, with the
scope and key of each memory stored and never the value. No field means
nothing was stored.
- **What it never does.** Capture cannot fail or delay a turn. If extraction is
unavailable, it stores nothing.
On a stream, `memory_recall` rides the role primer and `memory_capture` rides
the terminal usage frame, because what a turn stored is known only once the
answer is complete.
## Turn memory off for one request
Any key may send `false` for either side, and `false` needs no scope:
```json
{ "memory": { "recall": false, "capture": false } }
```
`recall: false` reads no memory and ignores `mentioned_vault_ids`.
`capture: false` stores nothing. The two are independent, so
`{ "capture": false }` alone keeps the turn personalized by what is already
stored while adding nothing new.
Pin `Cruxy-Version: 2026-09-26` on every request that relies on `false`. A
gateway that predates the field would ignore it and capture anyway; with the
pin, such a gateway refuses the request with `400` `unknown_version` instead.
A server should use a developer API key. If a signed-in credential does serve
other people, every request must send both `false` values, and `rules: false`,
or the owner's memory and rules reach their turns.
## Review and remove what is stored
- `GET /api/v1/memories` lists your effective memory, one entry per key, with
the `id` of the row that won.
- `DELETE /api/v1/memories/{id}` removes that entry. You can delete what you
can write: your own memories, a vault's where your membership grants
`can_write`, and organization memory if you are an owner or admin.
- `PUT /api/v1/memories` stores a value by hand. A key or value carrying an
email address, a phone number, or a card number is refused with `400`
`pii_rejected`, and nothing is stored.
[API Reference](/docs/en/reference/api) has every field of the memories
endpoints.
## Next steps
- [Standing rules](/docs/en/build/rules): the other per-request context, with the same default split.
- [Get your API key](/docs/en/get-started/api-keys): the two kinds of key and their restrictions.
- [Streaming](/docs/en/build/streaming): where the `cruxy` signals ride on a stream.
- [Errors and retries](/docs/en/build/errors-and-retries): handle `403` `key_scope_forbidden`.
- [Security](/docs/en/best-practices/security): keep secrets out of memory and capture off for anonymous traffic.
================================================================================
Page: Standing rules
URL: https://learn.cruxyai.com/docs/en/build/rules
Markdown: https://learn.cruxyai.com/docs/en/build/rules.md
================================================================================
# Standing rules
Inject your organization's standing rules into chat as defaults. Rules are on for signed-in keys and off for developer keys unless you opt in.
Standing rules are instructions your organization sets for chat, such as
"Always answer in British English." A vault can have its own. When rules
apply, the gateway injects them into the turn as defaults that the user's own
message overrides, never as constraints that outrank the user. The `rules`
field on a chat request turns injection on or off for that request.
## Defaults depend on the key
| Key | Rules when `rules` is omitted |
| --- | --- |
| Signed-in credential: device login, OAuth login, and the Cruxy web app, desktop, CLI, and mobile | Injected |
| Developer API key or headless key | Not injected, unless the key was issued with allowed scopes that name the `rules` family |
An unscoped developer key has every family but has not opted in, so it gets no
rules by default. A key's rules are its owner's organization's guidance, and a
developer key often serves people other than its owner.
## Opt in
A developer key opts in one of two ways:
- **Per request:** send `rules: true`.
- **Once, on the key:** issue the key with allowed scopes that name the `rules`
family. Every request from it then gets rules unless it sends `rules: false`.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "saga",
"stream": false,
"rules": true,
"messages": [
{ "role": "user", "content": "Draft a two-line status update for the launch." }
]
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'saga',
stream: false,
rules: true,
messages: [{ role: 'user', content: 'Draft a two-line status update for the launch.' }],
}),
})
const completion = await res.json()
console.log(completion.message.content)
console.log(completion.cruxy.rules_applied) // { count, sources }, or undefined
```
`rules: true` needs a key whose allowed scopes include `rules`. A key without
it gets `403` `key_scope_forbidden` with `required_scope: "rules"`, refused
before anything is billed, and such a key never gets rules. `rules: false`
needs no scope: any key may send it to decline rules for one turn, since the
user's own message already overrides them.
> **Warning:** A model can repeat an injected rule. Anyone chatting through a key that gets
> rules can ask the model to repeat them. Before you opt in a key
> that serves other people, read `GET /api/v1/rules/resolved` and confirm
> nothing in the result is meant to stay internal.
## What gets injected
The switch covers the whole resolved ruleset: a turn gets exactly what
`GET /api/v1/rules/resolved` returns for its context, or nothing.
- **No `vault_id`:** the organization's rules.
- **A `vault_id` for a vault that inherits** (the default): the organization's
rules first, then the vault's own.
- **A `vault_id` for a vault that does not inherit:** the vault's own rules
only.
A `vault_id` you cannot read is treated as none, so the turn runs with
organization rules. Injected rule text is real input and counts in
`usage.input_tokens`. Rules are separate from memory: the `memory` field never
affects them. `POST /v1/chat/completions` never injects rules.
Preview the rules a turn would get. Pass `vault_id` as a query parameter for a
chat inside a vault:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/rules/resolved \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/rules/resolved', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
const resolved = await res.json()
for (const rule of resolved.rules) {
console.log(rule.source, rule.text) // "org" or "vault", then the instruction
}
```
Each rule carries its `source`, `org` or `vault`, so an unexpected instruction
traces back to the ruleset that contributed it. An empty `rules` array is a
real answer: there is no default ruleset. Unlike chat, this endpoint answers
`404` `vault_not_found` for a `vault_id` you cannot read, so a mistyped id is
reported rather than resolved for the wrong context.
## Know when rules applied
When at least one rule applied, the response carries `cruxy.rules_applied`:
how many rules applied and from which rulesets, never the rule text. The field
is absent when nothing was injected. On a stream it rides the role primer.
```json
{
"cruxy": {
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"routed_tier": "saga",
"reasoning_effort": "low",
"rules_applied": {
"count": 3,
"sources": [{ "scope": "org", "count": 3 }]
}
}
}
```
## Edit the rules
`GET /api/v1/rules` returns one stored ruleset, the organization's or, with
`vault_id`, a vault's, and `PUT /api/v1/rules` replaces it whole. Its
`permissions.can_write` tells you whether you may change it: the
organization's ruleset is owner or admin only, and a vault's also admits a
member the vault grants `can_write`. A refused write is `403`
`rules_forbidden`.
Rules are re-injected on every request in scope, so each ruleset is capped:
| Cap | Limit |
| --- | --- |
| Rules per ruleset | 50 |
| Characters per rule | 1000 |
| Characters in total | 8000 |
Exceeding a cap is `400` `rules_too_large`. A rule carrying an email address,
a phone number, or a card number is refused with `400` `pii_rejected`, and
nothing is stored. [API Reference](/docs/en/reference/api) has every field of
the rules endpoints.
## Next steps
- [Memory](/docs/en/build/memory): recall and capture, with the same default split.
- [Get your API key](/docs/en/get-started/api-keys#what-a-key-is-allowed-to-do): allowed scopes and other key restrictions.
- [Chat](/docs/en/build/chat): the rest of the request and the `cruxy` block.
================================================================================
Page: Vaults
URL: https://learn.cruxyai.com/docs/en/build/vaults
Markdown: https://learn.cruxyai.com/docs/en/build/vaults.md
================================================================================
# Vaults
A vault is a shared space in your organization: its members share memory, rules, and files. Create one, manage members, and chat inside it.
A vault is a container inside your organization that a set of members share.
It holds memory that chat can recall, its own standing rules, and files. A
chat request names the vault it is happening inside with `vault_id`. Access is
by membership: administering an organization does not, by itself, let you read
a vault you are not a member of.
## Create a vault
`POST /api/v1/vaults` creates a vault. Only `name` is required, and names are
unique within an organization. You become the owner, and a member with read,
write, and share.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/vaults \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Platform team",
"description": "Shared context for the platform team",
"external_id": "app-vault-8f2c"
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/vaults', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: 'Platform team',
description: 'Shared context for the platform team',
external_id: 'app-vault-8f2c', // your own id for the vault; makes a retry safe
}),
})
if (!res.ok) throw new Error(`create failed: ${res.status} ${await res.text()}`)
const vault = await res.json()
// 201 with created: true the first time; 200 with created: false on a retry.
console.log(vault.id, vault.created, vault.permissions)
```
Send `external_id`, your own system's identifier for the vault, whenever a
retry is possible. A second create naming an `external_id` your organization
has already seen returns the existing vault with `200` and `created: false`,
and writes nothing. Without it, a retry after a timeout hits `409`
`vault_name_exists`, which carries no vault id. Reusing an `external_id` that
names a vault somebody else owns is `409` `vault_external_id_conflict`.
The optional fields:
| Field | Meaning |
| --- | --- |
| `description` | Free text, at most 2000 characters. |
| `icon`, `color` | Presentation, each from a fixed vocabulary that [API Reference](/docs/en/reference/api) lists. An unknown value is `400` `invalid_request`. |
| `memory_scope` | `default` or `vault_only`; see [Chat inside a vault](#chat-inside-a-vault). |
| `external_id` | Your identifier for the vault, which makes the create idempotent. |
`PATCH /api/v1/vaults/{id}` changes `name`, `description`, `icon`, `color`, or
`memory_scope`, and requires `can_write`. `GET /api/v1/vaults/{id}` reads one
vault, and `GET /api/v1/vaults` lists the vaults you can use, with two
filters: `scope` (`all`, `owned`, or `shared`) and `archived`.
## Permissions
Every vault read carries a `permissions` block with your own four flags:
| Flag | What it allows |
| --- | --- |
| `can_read` | Read the vault's content. |
| `can_write` | Change the vault's content and metadata, and archive it. |
| `can_share` | Add and remove members, granting only permissions you hold yourself. |
| `can_administer` | Delete the vault, and hold or restore its members. It is `true` for the vault's owner and for an admin of its organization. |
The four do not imply one another: a member with read, write, and share does
not administer the vault. Each flag is resolved from the same check that gates
the matching request, so you can show or hide controls from this block alone.
Treat an absent `can_administer` as `false`.
A vault you cannot read answers `404` `vault_not_found`, whether it is in
another organization, you are not a member, or you are held. A member who is
refused an action they lack the flag for gets `403` `vault_forbidden`.
## Members
`POST /api/v1/vaults/{id}/members` adds a member of your organization with
the permissions you name. All four fields are required.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/vaults/$VAULT_ID/members \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"user_id\": \"$USER_ID\",
\"can_read\": true,
\"can_write\": true,
\"can_share\": false
}"
```
```ts title="TypeScript"
const vaultId = process.env.VAULT_ID
const userId = process.env.USER_ID // a member of your organization
const res = await fetch(`https://api.cruxy.ai/api/v1/vaults/${vaultId}/members`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ user_id: userId, can_read: true, can_write: true, can_share: false }),
})
if (!res.ok) throw new Error(`add member failed: ${res.status} ${await res.text()}`)
const member = await res.json()
console.log(member.user_id, member.email, member.held)
```
- **Who may add.** The vault's owner and organization admins may grant
anything. A member with `can_share` may grant only permissions they hold: a
member who cannot write cannot create a member who can.
- **Errors.** A user outside your organization is `404` `user_not_found`, and
one who is already a member is `409` `vault_member_exists`.
- **List.** `GET /api/v1/vaults/{id}/members` lists the members with their
permissions, held members included.
- **Change.** `PATCH /api/v1/vaults/{id}/members/{user_id}` changes a member's
read, write, and share flags; omitted fields keep their value. The rule
against granting more than you hold applies to the resulting set of
permissions.
- **Remove.** `DELETE /api/v1/vaults/{id}/members/{user_id}` removes a member,
and their access ends immediately. The owner cannot be removed, and nobody
can modify the owner's membership; delete the vault instead.
## Hold a member
A hold suspends a member without changing their permissions.
`PUT /api/v1/vaults/{id}/members/{user_id}/held` with `{"held": true}` makes
the member's read, write, and share inert, and the vault reads to them as if
it did not exist. `{"held": false}` restores exactly the permissions they had.
```bash title="curl"
curl -X PUT https://api.cruxy.ai/api/v1/vaults/$VAULT_ID/members/$USER_ID/held \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "held": true }'
```
```ts title="TypeScript"
const vaultId = process.env.VAULT_ID
const userId = process.env.USER_ID
const res = await fetch(
`https://api.cruxy.ai/api/v1/vaults/${vaultId}/members/${userId}/held`,
{
method: 'PUT',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ held: true }),
},
)
if (!res.ok) throw new Error(`hold failed: ${res.status} ${await res.text()}`)
const member = await res.json()
console.log(member.user_id, member.held) // the flags are unchanged
```
Only the vault's owner or an organization admin may set or clear a hold, which
is what `can_administer` reports; `can_share` does not allow it. The owner
cannot be held.
## Archive and pin
- **Archive.** `PUT /api/v1/vaults/{id}/archive` hides the vault from the
default list; it appears only with `archived=true`, and reads report
`archived: true`. The vault and all its memory are untouched.
`DELETE /api/v1/vaults/{id}/archive` restores it. Both require `can_write`,
are idempotent, and answer `204`.
- **Pin.** `PUT /api/v1/vaults/{id}/pin` pins the vault for you alone: a
personal bookmark, reported as `pinned` on your reads of the vault and never
visible to other members. `DELETE /api/v1/vaults/{id}/pin` removes it. Both
need only that you can read the vault, are idempotent, and answer `204`.
- **Delete.** `DELETE /api/v1/vaults/{id}` deletes the vault, its membership
list, and all memory stored in it. It requires `can_administer`.
## Files in a vault
A vault holds files for every member who can read it.
- `POST /api/v1/vaults/{id}/files` uploads a file into the vault. It is the
same multipart upload as `POST /api/v1/files`, with the same detection,
document reading, and limits; see [Files](/docs/en/build/files). It requires
`can_write`, and `purpose=chat_attachment` is refused.
- `POST /api/v1/vaults/{id}/files/share` copies one of your own files into the
vault, named by `file_id`, as a new vault file with its own id. The original
stays yours and unchanged, and deleting either one never touches the other.
The copy counts against your storage pool and spends no monthly upload
allowance.
- `GET /api/v1/vaults/{id}/files` lists the vault's files, newest first, paged
like `GET /api/v1/files`. `GET /api/v1/vaults/{id}/files/{file_id}` reads one,
and `GET /api/v1/vaults/{id}/files/{file_id}/content` downloads its bytes.
All three require `can_read`.
- `DELETE /api/v1/vaults/{id}/files/{file_id}` deletes a vault file for every
member. Any member with `can_write` may delete any file in the vault, not
only their own.
```bash title="curl"
# Upload a file straight into the vault.
curl https://api.cruxy.ai/api/v1/vaults/$VAULT_ID/files \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-F "file=@handbook.pdf"
# Or copy in a file you already uploaded.
curl https://api.cruxy.ai/api/v1/vaults/$VAULT_ID/files/share \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d "{ \"file_id\": \"$FILE_ID\" }"
```
```ts title="TypeScript"
import { openAsBlob } from 'node:fs'
const vaultId = process.env.VAULT_ID
const auth = { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` }
// Upload a file straight into the vault.
const form = new FormData()
form.append('file', await openAsBlob('handbook.pdf'), 'handbook.pdf')
const uploaded = await fetch(`https://api.cruxy.ai/api/v1/vaults/${vaultId}/files`, {
method: 'POST',
headers: auth,
body: form,
})
if (!uploaded.ok) throw new Error(`upload failed: ${uploaded.status}`)
const vaultFile = await uploaded.json()
console.log(vaultFile.id, vaultFile.retention_until, vaultFile.shared_with)
// List what the vault holds.
const listed = await fetch(`https://api.cruxy.ai/api/v1/vaults/${vaultId}/files`, {
headers: auth,
})
if (!listed.ok) throw new Error(`list failed: ${listed.status}`)
const { data, total } = await listed.json()
console.log(`${total} files`, data.map((f: { filename: string }) => f.filename))
```
A vault file carries the fields of any file, plus `vault_id`, `uploaded_by`
(the user who uploaded or shared it, kept after they leave), `shared_with`
(how many members can read the vault), and `retention_until`. A vault file has
no `expires_at`: it is deleted at `retention_until`, 365 days from its last
use. An upload, a share, and a download each count as a use; reading metadata
does not.
## Chat inside a vault
Send `vault_id` on `POST /api/v1/chat` to say which vault the chat is
happening inside. It changes two things for the turn:
- **Memory.** Which stored memories recall may draw on. With no vault, that is
your personal memory, your organization's, and every vault you can read
whose `memory_scope` is `default`. Inside a `vault_only` vault, recall draws
on that vault's memory alone, and its memory is withheld everywhere outside
it. See [Memory](/docs/en/build/memory).
- **Rules.** Which standing rules apply: the vault's own ruleset, merged with
the organization's when the vault inherits them. See
[Standing rules](/docs/en/build/rules).
A user message can also attach a file the vault holds, by naming its id in
`file_ids` like any other file. The gateway reads a vault file only while you
are a member who reads that vault and is not held.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-H "Cruxy-Version: 2026-10-05" \
-d "{
\"model\": \"saga\",
\"stream\": false,
\"vault_id\": \"$VAULT_ID\",
\"messages\": [
{
\"role\": \"user\",
\"content\": \"What does the handbook say about on-call handover?\",
\"file_ids\": [\"$FILE_ID\"]
}
]
}"
```
```ts title="TypeScript"
const vaultId = process.env.VAULT_ID
const vaultFileId = process.env.FILE_ID // a file the vault holds
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
'Cruxy-Version': '2026-10-05', // file_ids needs this pin or later
},
body: JSON.stringify({
model: 'saga',
stream: false,
vault_id: vaultId,
messages: [
{
role: 'user',
content: 'What does the handbook say about on-call handover?',
file_ids: [vaultFileId],
},
],
}),
})
if (!res.ok) throw new Error(`chat failed: ${res.status} ${await res.text()}`)
const completion = await res.json()
console.log(completion.message.content)
console.log(completion.cruxy.rules_applied) // the rules the vault added, or undefined
```
A `vault_id` you cannot read is not an error and is never reported: the turn
runs exactly as if you had sent none, so a chat request cannot be used to
discover whether a vault id is real. Sending a vault id grants no access you
do not already have.
`mentioned_vault_ids` is different from `vault_id`. It names up to three
vaults the turn references without being inside them, and adds their memory
to the turn's recall. It changes nothing about isolation and never makes a
vault writable.
## Next steps
- [Memory](/docs/en/build/memory): what recall and capture do inside a vault.
- [Standing rules](/docs/en/build/rules): give a vault its own rules.
- [Files](/docs/en/build/files): upload files and attach them to a chat turn.
- [Conversations](/docs/en/build/conversations): hold a conversation inside a vault.
================================================================================
Page: Indic languages
URL: https://learn.cruxyai.com/docs/en/build/indic-languages
Markdown: https://learn.cruxyai.com/docs/en/build/indic-languages.md
================================================================================
# Indic languages
Cruxy supports 20+ Indic languages. Send text as UTF-8, measure token counts per script with the tokens endpoint, and test each language before launch.
Cruxy supports 20+ Indic languages. It publishes no list of which ones, and
these docs make no claim about how well a tier handles any one of them; see
[No per-language list for Indic languages](/docs/en/build/known-limitations#no-per-language-list-for-indic-languages).
This page covers the practical side: sending the text, measuring what it costs
in tokens, and testing it before you launch.
## Send the text as UTF-8
Request bodies are JSON, and JSON travels as UTF-8. Put the text in `content`
in its own script, as your users wrote it. It needs no escaping beyond what
JSON itself requires.
- **From a shell,** make sure the terminal and the file you send are UTF-8. For
anything longer than a line, keep the body in a file and send it with
`curl --data-binary @body.json`.
- **Attached files** named in `file_ids` must be UTF-8 text files, PDFs, Word
documents (`.docx`), or JPEG, PNG, or WebP images; see the
[API Reference](/docs/en/reference/api).
- **Keep one Unicode normalization form,** such as NFC, across your
application. The same visible text can be encoded as different sequences of
code points, and different bytes can count as different tokens.
## Token counts vary by script
A token is neither a character nor a word, and the same sentence can count as
a different number of tokens in a different script, and on a different tier.
Do not estimate tokens from character counts: measure them.
`POST /api/v1/tokens/count` returns the input tokens a chat request would bill
on a tier. It calls no model, is not billed, and is exact for single-turn
prompts on every tier. It takes a tier, not `auto`.
This sample counts the same question in Hindi and in English on each tier:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/tokens/count \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "saga",
"messages": [
{ "role": "user", "content": "भारत में मानसून कब आता है?" }
]
}'
```
```ts title="TypeScript"
const texts = {
hindi: 'भारत में मानसून कब आता है?',
english: 'When does the monsoon reach India?',
}
for (const model of ['canto', 'saga', 'epos']) {
for (const [name, content] of Object.entries(texts)) {
const res = await fetch('https://api.cruxy.ai/api/v1/tokens/count', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ model, messages: [{ role: 'user', content }] }),
})
if (!res.ok) throw new Error(`count failed: ${res.status} ${await res.text()}`)
const { input_tokens } = await res.json()
console.log(`${model} ${name}: ${input_tokens} input tokens`)
}
}
```
The count includes the tier's own prompt, which the gateway adds to every turn,
so compare counts on the same tier rather than reading one as the cost of your
text alone.
## Size max_tokens in tokens, not characters
`max_tokens` caps output tokens, so an answer of the same length can need a
different budget in a different script. Read `usage.output_tokens` from real
responses in each language you serve, and size `max_tokens` from those, with
room to spare. On a tier that thinks, the thinking spends the same budget; see
[Reasoning effort](/docs/en/build/reasoning-effort).
## Byte limits count bytes
Most Indic scripts take three bytes per character in UTF-8, where ASCII text
takes one, so a limit in bytes is reached in fewer characters.
`POST /api/v1/tokens/count` refuses with `400` `input_not_countable` text that
carries a single unbroken run of more than 1024 bytes, with no word, digit, or
punctuation boundary in it, or more than 262144 bytes of text in one request.
Ordinary prose with spaces between words stays far inside both.
## Test each language before launch
Before you launch in a language, test it on each tier you plan to use, with
prompts from your own product. An eval suite under `/api/v1/evals/suites` runs
the same cases against the tier you name; each case is billed as a chat
completion. Test embeddings, rerank, and moderation in that language too, if
you use them: these docs make no per-language claim for them either.
## Next steps
- [Prompting basics](/docs/en/best-practices/prompting): what the model reads on every turn.
- [Usage and cost](/docs/en/build/usage-and-cost): the token counts each response reports.
- [Known limitations](/docs/en/build/known-limitations): the limits to plan around.
================================================================================
Page: Prompts
URL: https://learn.cruxyai.com/docs/en/build/prompts
Markdown: https://learn.cruxyai.com/docs/en/build/prompts.md
================================================================================
# Prompts
Store a prompt under a stable name, add immutable versions as you revise it, and fetch a version at runtime to send to chat.
A prompt is a named container for template text that you revise over time.
Each revision is an immutable, numbered version, so the text a release was
tested with is always retrievable. Prompts live in the workspace your key
names: there is no workspace or organization id in any path or body, and no
way to address another workspace.
## Create a prompt
`POST /api/v1/prompts` creates a prompt whose first version holds the
`content` you send.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/prompts \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Code Review",
"slug": "code-review",
"content": "You are a meticulous code reviewer. Review this diff:\n\n{{diff}}"
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/prompts', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: 'Code Review',
slug: 'code-review',
content: 'You are a meticulous code reviewer. Review this diff:\n\n{{diff}}',
}),
})
if (!res.ok) throw new Error(`create failed: ${res.status} ${await res.text()}`)
const prompt = await res.json()
console.log(prompt.id, prompt.slug, prompt.version.version) // version 1
```
| Field | Required | Meaning |
| --- | --- | --- |
| `name` | Yes | The display name, 1 to 128 bytes. |
| `content` | Yes | The template text of version 1, 1 to 102,400 bytes. It is stored and returned verbatim. |
| `slug` | No | A stable, URL-safe handle. Omitted, it is derived from the name: lowercased, with runs of other characters collapsed to hyphens. Send it yourself when the name has no usable characters. |
A slug already used in the workspace is `409` `prompt_slug_exists`. The `201`
is the prompt with its version 1 embedded under `version`.
> **Note:** Content is stored as you wrote it and is not filtered for personal data, so
> keep secrets and personal details out of a prompt.
## Add a version
`POST /api/v1/prompts/{id}/versions` appends a new version. Its number is the
previous highest plus one, and existing versions are never modified.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/prompts/$PROMPT_ID/versions \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"content": "You are a meticulous code reviewer. Be concise. Review this diff:\n\n{{diff}}"
}'
```
```ts title="TypeScript"
const promptId = process.env.PROMPT_ID // the id a create returned
const res = await fetch(`https://api.cruxy.ai/api/v1/prompts/${promptId}/versions`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: 'You are a meticulous code reviewer. Be concise. Review this diff:\n\n{{diff}}',
}),
})
if (!res.ok) throw new Error(`add version failed: ${res.status} ${await res.text()}`)
const version = await res.json()
console.log(version.prompt_id, version.version, version.created_at)
```
A version carries `prompt_id`, `version`, `content`, `created_by`, and
`created_at`.
## Read prompts and versions
| Call | Returns |
| --- | --- |
| `GET /api/v1/prompts` | Every prompt in the workspace, oldest first, each with its `slug`, `latest_version`, and `version_count`, without content. |
| `GET /api/v1/prompts/{id}` | One prompt with a single version embedded: the latest, or the one `version` names, as in `?version=2`. |
| `GET /api/v1/prompts/{id}/versions` | Every version of the prompt, newest first, each with its content. |
| `DELETE /api/v1/prompts/{id}` | Nothing: `204`. The prompt and all of its versions are deleted. |
A prompt id in another workspace or organization, or one that names nothing,
is `404` `prompt_not_found`. A `version` the prompt does not have is `404`
`prompt_version_not_found`.
## Use a prompt in chat
A chat request has no field that names a stored prompt, and the gateway
returns `content` verbatim. So your code fetches the version it wants, fills
in any placeholders its own templates use, and sends the text as a message.
Pin a `version` number in production, so that adding a version never changes
what a deployed build sends.
```bash title="curl"
# 1. Fetch version 2 of the prompt and read its content.
curl "https://api.cruxy.ai/api/v1/prompts/$PROMPT_ID?version=2" \
-H "Authorization: Bearer $CRUXY_API_KEY"
# 2. Send the content, with your own values filled in, as the system message.
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "saga",
"stream": false,
"messages": [
{ "role": "system", "content": "You are a meticulous code reviewer. Be concise." },
{ "role": "user", "content": "Review this diff:\n\n- const retries = 3\n+ const retries = 30" }
]
}'
```
```ts title="TypeScript"
const promptId = process.env.PROMPT_ID
const headers = {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
}
// 1. Fetch the pinned version.
const promptRes = await fetch(`https://api.cruxy.ai/api/v1/prompts/${promptId}?version=2`, {
headers,
})
if (!promptRes.ok) throw new Error(`prompt fetch failed: ${promptRes.status}`)
const prompt = await promptRes.json()
// 2. Fill in the placeholder. The gateway does not do this for you.
const diff = '- const retries = 3\n+ const retries = 30'
const content = prompt.version.content.replaceAll('{{diff}}', diff)
// 3. Send it to chat.
const chatRes = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers,
body: JSON.stringify({
model: 'saga',
stream: false,
messages: [{ role: 'user', content }],
}),
})
if (!chatRes.ok) throw new Error(`chat failed: ${chatRes.status} ${await chatRes.text()}`)
const completion = await chatRes.json()
console.log(`prompt v${prompt.version.version}:`, completion.message.content)
```
The `{{diff}}` marker is this sample's own convention. Use whatever
placeholder syntax suits your templates; the gateway treats the content as
opaque text.
An eval suite can record which prompt it tests: `prompt_id` on
`POST /api/v1/evals/suites` links a prompt in the same workspace. The link is
advisory metadata, and an eval run sends each case's `input` as it is.
## Errors
| Status | `code` | When |
| --- | --- | --- |
| `400` | `invalid_request` | A missing or empty `name` or `content`, or a field over its size. |
| `404` | `prompt_not_found` | The id is not a prompt in your workspace. |
| `404` | `prompt_version_not_found` | The prompt exists and has no such version. |
| `409` | `prompt_slug_exists` | The slug is already used in the workspace. |
| `413` | `payload_too_large` | The request body is over the route's 64 KiB limit. |
See [Errors and retries](/docs/en/build/errors-and-retries) for the envelope
and what to retry.
## Next steps
- [Chat](/docs/en/build/chat): the request the prompt's content goes into.
- [Prompting basics](/docs/en/best-practices/prompting): what to put in a prompt.
- [API Reference](/docs/en/reference/api): every prompt operation with its exact request and response shape.
================================================================================
Page: Define tools
URL: https://learn.cruxyai.com/docs/en/build/tools
Markdown: https://learn.cruxyai.com/docs/en/build/tools.md
================================================================================
# Define tools
Describe your functions in the tools array with a JSON Schema for their arguments, and use tool_choice to control when the model calls one.
Tool calling lets a chat model ask your code to run a function. You describe
the functions on the request; when the model decides to use one, the turn ends
with the call and its arguments, you run it, and you send the result back. This
page covers the first part. [Handle tool calls](/docs/en/build/tool-calls)
covers answering the calls, and
[Tutorial: build a tool-using agent](/docs/en/build/tool-agent) runs the whole
loop.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "canto",
"stream": false,
"messages": [
{ "role": "user", "content": "What is the weather in Paris?" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "City name, for example Paris" }
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}'
```
```ts title="TypeScript"
const tools = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a city',
parameters: {
type: 'object',
properties: {
city: { type: 'string', description: 'City name, for example Paris' },
},
required: ['city'],
},
},
},
]
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'canto',
stream: false,
messages: [{ role: 'user', content: 'What is the weather in Paris?' }],
tools,
tool_choice: 'auto',
}),
})
const completion = await res.json()
console.log(completion.finish_reason) // "tool_calls" when the model wants get_weather
console.log(completion.message.tool_calls)
```
## Describe a tool
`tools` is an array. Each entry describes one function:
| Field | Required | What it is |
| --- | --- | --- |
| `type` | Yes | `"function"`. |
| `function.name` | Yes | The name the model calls the function by, and the name it returns in a tool call. |
| `function.description` | No | What the function does, rendered into the prompt the model reads. |
| `function.parameters` | No | A JSON Schema object describing the function's arguments, also rendered into the prompt. |
Write the `description` and each parameter's `description` for the model: say
what the function returns and when to use it, the way you would brief a
colleague.
## Control tool use
`tool_choice` controls whether the model may call your tools, and which:
| `tool_choice` | Effect |
| --- | --- |
| Omitted | The provider default. |
| `"none"` | The model calls no tool. |
| `"auto"` | The model decides whether to call a tool. |
| `"required"` | The model calls at least one tool. |
| `{ "type": "function", "function": { "name": "get_weather" } }` | The model calls that function. This is the only form that makes a tool call deterministic. |
A forced function must appear in `tools`, and `"required"` needs at least one
tool. Either mistake is `400` `invalid_request`, answered before anything goes
upstream.
## Schema bounds
The gateway bounds each tool's `parameters` schema by size, depth, and node
count. A schema past a bound is `400` `invalid_tool_schema`, which adds three
fields:
| Field | Meaning |
| --- | --- |
| `tool` | The function whose schema broke the bound. |
| `bound` | Which bound: `bytes`, `depth`, or `nodes`. |
| `limit` | That bound's value. |
Retrying never helps, because the fix belongs in the tool definitions your
client ships. To check them ahead of time, send the same `tools` to
`POST /api/v1/tokens/count`: it applies the same bounds with the same `400`
answer, is not billed, and returns the input tokens the request would bill,
tools included.
## Tools are billed input
The provider renders each function's name, description, and parameters into
the prompt, so every request that sends `tools` is billed for them as input
tokens. Count them with `POST /api/v1/tokens/count`, and keep descriptions as
long as the model needs and no longer.
## Where tools work
`tools` and `tool_choice` are accepted on `POST /api/v1/chat`,
`POST /api/v2/chat`, and `POST /v1/chat/completions`. Each tier reports
`supports_tools` in `GET /api/v1/models`; [Models](/docs/en/reference/models)
shows how to read the catalog.
## Next steps
- [Handle tool calls](/docs/en/build/tool-calls): run the calls the model returns and send the results back.
- [Tutorial: build a tool-using agent](/docs/en/build/tool-agent): the full loop in one program.
- [Chat](/docs/en/build/chat): the rest of the request and the response.
================================================================================
Page: Handle tool calls
URL: https://learn.cruxyai.com/docs/en/build/tool-calls
Markdown: https://learn.cruxyai.com/docs/en/build/tool-calls.md
================================================================================
# Handle tool calls
Read the tool calls the model returns, run each function, send every result back by tool_call_id, and repeat until finish_reason is stop.
When the model decides to call one of the tools you defined, the turn ends with
`finish_reason: "tool_calls"` and the assistant message carries `tool_calls`.
You run each function, then send a new request with the results; the model
reads them and continues. [Define tools](/docs/en/build/tools) covers the
request that offers the tools.
## Read the tool calls
On `POST /api/v1/chat` with `stream: false`, the assistant reply is the
top-level `message`. A turn that calls a tool looks like this, trimmed to the
fields that matter here:
```json
{
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\":\"Paris\"}" }
}
]
},
"finish_reason": "tool_calls"
}
```
Each entry in `tool_calls` has three fields:
| Field | What it is |
| --- | --- |
| `id` | The call's id. Your result for this call carries it back as `tool_call_id`. |
| `type` | `"function"`. |
| `function` | The `name` of the function to run, and its `arguments`. |
`arguments` is a JSON-encoded string, not an object. Parse it before you
dispatch:
```ts
const args = JSON.parse(call.function.arguments) // { city: "Paris" }
```
## Send the results back
1. **Append the assistant message whole**
Push the `message` the gateway returned onto your conversation, `tool_calls`
included. The model needs its own call in the history next to the result that
answers it.
2. **Append one tool message per call**
For every entry in `tool_calls`, run the function and append a message with
`role: "tool"`, the call's `id` as `tool_call_id`, and the result as a string in
`content`.
3. **Call chat again with the same tools**
Send the whole conversation back with the same `tools`. The model reads the
results and either answers or calls another tool.
This request carries a finished tool exchange back to the model:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "canto",
"stream": false,
"messages": [
{ "role": "user", "content": "What is the weather in Paris?" },
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\":\"Paris\"}" }
}
]
},
{
"role": "tool",
"tool_call_id": "call_abc123",
"content": "{\"city\":\"Paris\",\"tempC\":18,\"sky\":\"clear\"}"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
}
]
}'
```
```ts title="TypeScript"
const tools = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a city',
parameters: {
type: 'object',
properties: { city: { type: 'string' } },
required: ['city'],
},
},
},
]
// The assistant turn the gateway returned, kept whole.
const assistantTurn = {
role: 'assistant',
content: '',
tool_calls: [
{
id: 'call_abc123',
type: 'function',
function: { name: 'get_weather', arguments: '{"city":"Paris"}' },
},
],
}
const messages = [
{ role: 'user', content: 'What is the weather in Paris?' },
assistantTurn,
// Your result, tied back to the call by its id.
{
role: 'tool',
tool_call_id: 'call_abc123',
content: JSON.stringify({ city: 'Paris', tempC: 18, sky: 'clear' }),
},
]
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ model: 'canto', stream: false, messages, tools }),
})
const completion = await res.json()
console.log(completion.finish_reason) // "stop" once the model has its answer
console.log(completion.message.content)
```
## Loop on finish_reason
`finish_reason` tells you what to do with each turn:
| `finish_reason` | What it means | What you do |
| --- | --- | --- |
| `tool_calls` | The model wants the calls in `message.tool_calls`. | Run them, append the results, and call again. |
| `stop` | The model finished its reply. | Read the answer in `message.content`. |
| `length` | The output hit `max_tokens` or the tier's cap. | Raise `max_tokens` or lower `reasoning_effort`; see [Reasoning effort](/docs/en/build/reasoning-effort). |
| `content_filter` | The upstream model stopped for safety. | Stop the loop and handle the turn as unanswered. |
Keep calling while `finish_reason` is `tool_calls`, and stop when it returns
`stop`. Put a ceiling on the number of rounds too, so a model that keeps
calling tools cannot run your loop forever.
## Several calls in one turn
`tool_calls` is an array, so one turn can ask for several calls. Run all of
them and append one `tool` message for each before you call chat again. The
`tool_call_id` on each result is what pairs it with its call.
If a function fails, still answer its call: put a short description of the
failure in that tool message's `content`, so every call in the turn has a
result.
## Send the tools on every request
Keep sending the same `tools` for as long as the conversation holds tool
exchanges. When the history replays a tool call to a function that the
request's `tools` does not declare, the gateway sends that exchange to the
model as prose instead of as a tool call, and reports it in
`cruxy.undeclared_tool_replay`:
| Field | Meaning |
| --- | --- |
| `exchanges` | How many assistant messages were sent as prose. |
| `tool_calls` | How many tool calls those messages carried. |
The field is absent when nothing was rewritten, and nothing you store or send
is changed: only what the model reads that turn.
## Tool calls on a stream
With `stream: true`, the calls arrive in pieces. Deltas carry `tool_calls`
fragments instead of `content`; each fragment has an `index` and a piece of
`function.arguments`, and may carry the call's `id`, `type`, and
`function.name`. Assemble the fragments per `index`, concatenating the
`arguments` pieces. The finish frame with `finish_reason: "tool_calls"` means
the calls are complete; answer them as above.
[Tool-call deltas](/docs/en/build/streaming#tool-call-deltas) shows the frames.
On the native wire, `POST /api/v2/chat`, the fragments ride `delta` events and
the terminal `finish` event carries `finish_reason`.
## Next steps
- [Tutorial: build a tool-using agent](/docs/en/build/tool-agent): this loop as one working program.
- [Define tools](/docs/en/build/tools): the tool shape, `tool_choice`, and schema bounds.
- [Streaming](/docs/en/build/streaming): the full frame order.
================================================================================
Page: Tutorial: build a tool-using agent
URL: https://learn.cruxyai.com/docs/en/build/tool-agent
Markdown: https://learn.cruxyai.com/docs/en/build/tool-agent.md
================================================================================
# Tutorial: build a tool-using agent
Build a working TypeScript agent that offers two tools, runs the calls the model returns, and loops on finish_reason until it answers.
In this tutorial you build a small agent: one TypeScript file that answers a
travel question by calling two functions of yours. It uses `POST /api/v1/chat`
with `stream: false` and the loop from
[Handle tool calls](/docs/en/build/tool-calls). You finish with a program you
can run, and a loop you can reuse with your own tools.
## Before you start
- Node.js 22 or later, for the built-in `fetch`.
- A developer API key in `CRUXY_API_KEY`; see
[Get your API key](/docs/en/get-started/api-keys).
- A way to run TypeScript directly. The commands here use `npx tsx`.
## Step 1: Watch one tool call
Before writing the loop, send one request by hand and look at what comes back.
Offer a `get_weather` tool and ask about the weather:
```bash title="curl"
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "canto",
"stream": false,
"messages": [
{ "role": "user", "content": "What is the weather in Paris?" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
}
]
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'canto',
stream: false,
messages: [{ role: 'user', content: 'What is the weather in Paris?' }],
tools: [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a city',
parameters: {
type: 'object',
properties: { city: { type: 'string' } },
required: ['city'],
},
},
},
],
}),
})
const { message, finish_reason } = await res.json()
console.log(finish_reason) // "tool_calls"
console.log(message.tool_calls)
```
When the model chooses the tool, `finish_reason` is `"tool_calls"` and
`message.tool_calls` holds the call: an `id`, the function `name`, and its
`arguments` as a JSON string. The model does not run anything. Your program
runs the function and sends the result back, and the rest of this tutorial
automates that.
## Step 2: Describe the tools
Create `agent.mts`. Start with the types the loop passes around and the two
tools the model may call:
```ts
const BASE = 'https://api.cruxy.ai'
interface ToolCall {
id: string
type: 'function'
function: { name: string; arguments: string }
}
type Message =
| { role: 'system' | 'user'; content: string }
| { role: 'assistant'; content: string; tool_calls?: ToolCall[] }
| { role: 'tool'; tool_call_id: string; content: string }
interface Completion {
message: { role: 'assistant'; content: string; tool_calls?: ToolCall[] }
finish_reason: 'stop' | 'tool_calls' | 'length' | 'content_filter'
}
const tools = [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a city: temperature in Celsius and the sky.',
parameters: {
type: 'object',
properties: { city: { type: 'string', description: 'City name, for example Paris' } },
required: ['city'],
},
},
},
{
type: 'function',
function: {
name: 'get_local_time',
description: 'Get the current local time in a city.',
parameters: {
type: 'object',
properties: { city: { type: 'string', description: 'City name, for example Mumbai' } },
required: ['city'],
},
},
},
]
```
[Define tools](/docs/en/build/tools) covers every field of a tool.
## Step 3: Write the handlers
Each tool name maps to a function of yours. `runTool` parses the call's
`arguments`, runs the matching handler, and always returns a string for the
tool message, even when the call names an unknown tool or the handler throws:
```ts
const TIME_ZONES: Record = {
paris: 'Europe/Paris',
mumbai: 'Asia/Kolkata',
london: 'Europe/London',
}
const handlers: Record Promise> = {
async get_weather({ city }) {
// Replace with a call to your weather service.
return { city, tempC: 18, sky: 'clear' }
},
async get_local_time({ city }) {
const timeZone = TIME_ZONES[city.toLowerCase()]
if (!timeZone) return { city, error: 'unknown city' }
return { city, time: new Date().toLocaleTimeString('en-GB', { timeZone }) }
},
}
async function runTool(call: ToolCall): Promise {
const handler = handlers[call.function.name]
if (!handler) return JSON.stringify({ error: `no tool named ${call.function.name}` })
try {
const result = await handler(JSON.parse(call.function.arguments))
return JSON.stringify(result)
} catch (err) {
return JSON.stringify({ error: String(err) })
}
}
```
## Step 4: Call the model
`chat` sends the conversation so far, with the tools, and returns the turn. It
throws on an error response, naming the stable `code` and the `request_id`:
```ts
async function chat(messages: Message[]): Promise {
const res = await fetch(`${BASE}/api/v1/chat`, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ model: 'canto', stream: false, messages, tools }),
})
if (!res.ok) {
const { code, error, request_id } = await res.json()
throw new Error(`${res.status} ${code}: ${error} (request ${request_id})`)
}
return res.json()
}
```
The request leaves `max_tokens` unset. `canto` thinks by default, and its
thinking shares the `max_tokens` budget, so a small cap could end a turn with
`finish_reason: "length"` before the model writes a call or an answer; see
[Reasoning effort](/docs/en/build/reasoning-effort).
## Step 5: Run the loop
The loop calls the model, keeps the assistant turn, and branches on
`finish_reason`. On `tool_calls` it answers every call by `id` and goes around
again; on `stop` the turn's `content` is the answer:
```ts
async function runAgent(question: string, maxRounds = 8): Promise {
const messages: Message[] = [{ role: 'user', content: question }]
for (let round = 0; round < maxRounds; round++) {
const { message, finish_reason } = await chat(messages)
messages.push(message) // the assistant turn, tool_calls and all
if (finish_reason === 'tool_calls') {
for (const call of message.tool_calls ?? []) {
messages.push({ role: 'tool', tool_call_id: call.id, content: await runTool(call) })
}
continue
}
if (finish_reason === 'stop') return message.content
throw new Error(`the turn ended with finish_reason "${finish_reason}"`)
}
throw new Error(`no answer after ${maxRounds} rounds`)
}
console.log(
await runAgent(
'I fly from Mumbai to Paris tonight. What is the weather in Paris, and what time is it there now?',
),
)
```
## Step 6: Run it
With the four pieces in `agent.mts`, in order, run:
```bash
npx tsx agent.mts
```
The program prints the model's answer. Along the way the model asks for
`get_weather` and `get_local_time`, possibly both in one turn, and the loop
answers each call before it asks again.
## Why the loop is shaped this way
- **It branches on `finish_reason`.** `tool_calls` means the model is waiting
on your results, and `stop` means it has answered. `length` and
`content_filter` end the loop with an error, because neither carries a
finished answer.
- **It appends the assistant turn whole.** The model needs to see its own call
next to the result that answers it, so the turn goes into the history,
`tool_calls` included, before any result.
- **It answers every call by `id`.** One turn can request several calls. Each
result carries its call's `id` as `tool_call_id`, which is how the model
pairs results with calls.
- **It sends the same `tools` every round.** A replayed call to a tool the
request does not declare reaches the model as prose, not as a tool call.
- **It always returns a string from `runTool`.** A failed or unknown call still
gets a result, so no call goes unanswered.
- **It stops after `maxRounds`.** The bound is a backstop for a model that
never settles; reaching it is worth logging, not presenting as an answer.
## Next steps
- [Handle tool calls](/docs/en/build/tool-calls): the response shapes the loop reads, and tool calls on a stream.
- [Define tools](/docs/en/build/tools): `tool_choice` and the schema bounds.
- [Errors and retries](/docs/en/build/errors-and-retries): decide which errors `chat` should retry.
================================================================================
Page: Reliability
URL: https://learn.cruxyai.com/docs/en/build/reliability
Markdown: https://learn.cruxyai.com/docs/en/build/reliability.md
================================================================================
# Reliability
Run Cruxy in production: per-tier deadlines, which errors to retry and how, Idempotency-Key, telling a finished stream from a failed one, and status.
This page collects what the gateway guarantees when a call is slow or fails,
and what it leaves to your client. Each section links to the page that covers
its topic in full. The sample streams a long `canto` turn, accepts the answer
only once the stream finishes, and retries a failed attempt:
```bash title="curl"
# One key per logical request: reuse it if you send this request again.
IDEMPOTENCY_KEY=$(uuidgen)
# A long canto turn: stream it. --max-time sits above the five-minute limit
# on a stream, so the gateway, not curl, decides when the turn has run long.
curl -sN --max-time 330 https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $IDEMPOTENCY_KEY" \
-d '{
"model": "canto",
"stream": true,
"messages": [
{ "role": "user", "content": "Plan a three-day walking tour of Old Delhi." }
]
}' > stream.txt
# The completion finished only if the stream ended with [DONE].
grep -q '^data: \[DONE\]' stream.txt && echo "complete" || echo "incomplete: send it again"
```
```ts title="TypeScript"
// Your retry budget and timeout. Tune them for your workload; they are not API
// limits. A stream may run for up to five minutes, so the timeout sits above it.
const MAX_ATTEMPTS = 3
const BASE_DELAY_MS = 500
const TIMEOUT_MS = 330_000
// Transient failures the API marks retryable.
const RETRYABLE = new Set([
'rate_limited',
'credits_unavailable',
'headless_concurrency_limit',
'limits_unavailable',
'upstream_overloaded',
'model_unavailable',
'all_models_unavailable',
'server_shutting_down',
'internal_error',
])
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
// Exponential backoff with full jitter.
const backoff = (attempt: number) => Math.random() * BASE_DELAY_MS * 2 ** attempt
type Attempt = { answer: string } | { retryInMs: number }
async function streamOnce(
body: Record,
idempotencyKey: string,
attempt: number,
): Promise {
let res: Response
try {
res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': idempotencyKey,
},
body: JSON.stringify({ ...body, stream: true }),
signal: AbortSignal.timeout(TIMEOUT_MS),
})
} catch {
return { retryInMs: backoff(attempt) } // no response at all: send it again
}
if (!res.ok) {
const { code, error, request_id } = await res.json()
if (!RETRYABLE.has(code)) throw new Error(`${res.status} ${code}: ${error} (request_id ${request_id})`)
const retryAfter = Number(res.headers.get('Retry-After'))
return { retryInMs: retryAfter > 0 ? retryAfter * 1000 : backoff(attempt) }
}
if (!res.body) return { retryInMs: backoff(attempt) }
const reader = res.body.getReader()
const decoder = new TextDecoder()
let buffer = ''
let answer = ''
try {
while (true) {
const { value, done } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
const lines = buffer.split('\n')
buffer = lines.pop() ?? '' // keep a partial line for the next read
for (const raw of lines) {
const line = raw.trim()
if (!line.startsWith('data:')) continue
const data = line.slice('data:'.length).trim()
if (data === '[DONE]') return { answer } // the only success signal
const choice = JSON.parse(data).choices[0] // undefined on the usage frame
if (choice?.delta.content) answer += choice.delta.content
}
}
} catch {
// The connection dropped mid-stream: fall through and treat it as a failure.
}
// The stream ended before [DONE]: a failed completion. Discard the partial answer.
return { retryInMs: backoff(attempt) }
}
async function chat(body: Record): Promise {
// One key per logical request, reused on every retry of it: if two attempts
// both complete, the request is billed once.
const idempotencyKey = crypto.randomUUID()
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
const result = await streamOnce(body, idempotencyKey, attempt)
if ('answer' in result) return result.answer
if (attempt < MAX_ATTEMPTS) await sleep(result.retryInMs)
}
throw new Error(`no complete answer after ${MAX_ATTEMPTS} attempts`)
}
const answer = await chat({
model: 'canto',
messages: [{ role: 'user', content: 'Plan a three-day walking tour of Old Delhi.' }],
})
console.log(answer)
```
## Deadlines and timeouts
With `stream: false`, the gateway makes one attempt at the model, bounded by the
tier's deadline:
| Tier | Deadline |
| --- | --- |
| `canto` | 90 seconds |
| `saga` | 90 seconds |
| `epos` | 45 seconds |
An attempt that runs past its deadline is cancelled and answered `504`
`upstream_timeout`. Nothing is billed, and the gateway does not retry it, since
a turn that needed longer needs longer again. The same deadlines apply on all
three chat wires: `POST /api/v1/chat`, `POST /api/v2/chat`, and
`POST /v1/chat/completions`.
- **Stream long turns.** A stream may run for up to five minutes. A long `canto`
turn, with reasoning on and a large output, belongs on a stream.
- **Or ask for less.** A lower `max_tokens` or `reasoning_effort` shortens the
turn; see [Reasoning effort](/docs/en/build/reasoning-effort).
- **Set your client timeout above the deadline,** or above five minutes for a
stream, so a slow model reaches you as a `504` with a `request_id` rather
than as your own abort.
## What the gateway retries for you
- **A failing model, inside the gateway.** An overloaded or failing model, or a
dropped connection to it, is retried a bounded number of times, with
backoff, before the gateway answers `503` `upstream_overloaded` or
`model_unavailable`. On a stream, that happens only while the stream opens,
never after a byte has reached you.
- **Never a non-streaming timeout.** See [Deadlines and timeouts](#deadlines-and-timeouts).
- **Never on another tier.** Once a call is sent, it stays on its tier.
`model: "auto"` picks a healthy tier before the call, and an explicit tier
that keeps failing is refused at once with `503` `model_unavailable` while it
cools down. See [Auto routing](/docs/en/build/auto-routing).
## What to retry
Branch on the error's `code`, never on its message:
| Response | What to do |
| --- | --- |
| `429` `rate_limited`, `credits_unavailable`, `headless_concurrency_limit`, or `503` `limits_unavailable` | Wait the `Retry-After` seconds, then retry. |
| `503` `upstream_overloaded`, `model_unavailable`, `all_models_unavailable`, `service_unavailable`, `server_shutting_down`, or `500` `internal_error` | Back off, then retry. |
| `504` `upstream_timeout` on chat | Change the request first: stream it, or lower `max_tokens` or `reasoning_effort`. On embeddings, rerank, and moderations, retry it with backoff; see [Embeddings, rerank, and moderations](#embeddings-rerank-and-moderations). |
| `502` `upstream_error` | Do not retry. The model provider rejected the request, and it fails the same way again. |
| `429` `budget_exhausted`, `key_spend_cap_exceeded`, `workspace_spend_cap_exceeded`, `credits_exhausted` | Stop. Each clears only at its `reset_at`, or when the cap changes. |
| No response, or a stream that ended before it finished | Send the request again, with the same `Idempotency-Key`. |
| Any other code | Look it up in [Errors and retries](/docs/en/build/errors-and-retries#which-failures-to-retry). Most describe the request or the key, and fail the same way until you change them. |
When you retry:
- **Honour `Retry-After`.** Every `429` sends it, and so does a `503`
`limits_unavailable`.
- **Otherwise back off exponentially, with jitter,** so many clients that failed
together do not retry together.
- **Cap the attempts and the total wait,** so a persistent failure ends instead
of hanging.
- **Log the `request_id` of every failure,** retried or not; see
[Request IDs and metadata](/docs/en/build/request-ids).
### Embeddings, rerank, and moderations
`POST /api/v1/embeddings`, `POST /api/v1/rerank`, and `POST /api/v1/moderations`
fail on the model the same way, with one difference from chat: a timeout is
worth retrying unchanged. Nothing is billed on any of these:
| Response | Retry? |
| --- | --- |
| `502` `upstream_error` | No. The model provider rejected the request the gateway sent, and the same request fails the same way. |
| `503` `upstream_overloaded`, `model_unavailable`, `server_shutting_down`, `limits_unavailable` | Yes, with backoff. `limits_unavailable` sends `Retry-After: 5`. |
| `503` `service_unavailable` | Yes, in a few minutes. The model provider refused to serve Cruxy at all: an outage on Cruxy's side, never caused by the request. |
| `504` `upstream_timeout` | Yes, with backoff. Unlike a chat turn, the same request is not expected to take as long again. |
## Retry safely with Idempotency-Key
Send an `Idempotency-Key` header, one per logical request, and reuse it on every
retry of that request:
- **A success is stored for 24 hours.** A later or concurrent call with the same
key and the same body replays it byte for byte, without calling the model and
without billing again. A concurrent duplicate waits for the first call to
finish.
- **Only a success is stored.** An error, a `429` or any `5xx` included,
releases the key, so a retry with the same key and body runs the request
again. That is the intended way to retry.
- **A different body under the same key** is `409` `idempotency_conflict`.
- **A response over 256 KiB** is served but not stored, so a retry runs again.
It is still billed once.
- **A key is at most 255 characters,** and is scoped to your API key and the
endpoint.
- **A stream stores and replays nothing.** A retry with the same key and body
streams a new generation. When both attempts complete, the request is billed
once; an attempt that did not complete is billed for the tokens it delivered,
and the retry is billed as well.
[Errors and retries](/docs/en/build/errors-and-retries#make-retries-idempotent)
lists the endpoints that accept the header.
## Confirm a stream finished
Once a stream opens, its `200` is already sent, so a failure after that point
cannot change the status. How the stream tells you it failed depends on the
wire:
- **`POST /api/v1/chat`:** `data: [DONE]` is the only success signal. A failure
after the stream opens sends no error frame; the stream stops early. Treat a
stream that ends before `[DONE]` as a failed completion: discard the partial
output or retry, and never show it as a finished answer.
- **`POST /api/v2/chat`:** the stream always ends with exactly one `finish` or
`error` frame. It has no `[DONE]`.
- **`POST /v1/chat/completions`:** a failure after the stream opens ends it with
a `data: {"error": {...}}` frame instead of `[DONE]`.
The tokens a failed stream delivered are billed as a partial usage row.
[Streaming](/docs/en/build/streaming#done-is-the-only-success-signal) shows how
to detect each case.
## Check platform status
[status.cruxyai.com](https://status.cruxyai.com) is the Cruxy status page. For
the same picture in code, `GET /api/status` returns a status document. It takes
no key.
```bash title="curl"
curl https://api.cruxy.ai/api/status
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/status')
// A 503 still carries the document, with status "unknown": no model component
// has a fresh measurement, so the document cannot be trusted.
const status = await res.json()
console.log(`platform: ${status.status}`)
// public_id names a chat tier by its public id. A chat tier's id keeps the
// tier's former name, so map it only when public_id is absent.
const FORMER_IDS: Record = { kavi: 'canto', vaani: 'saga', mira: 'epos' }
// Iterate the array and read each row's kind; new components are appended.
for (const component of status.components) {
const id = component.public_id ?? FORMER_IDS[component.id] ?? component.id
console.log(`${id} (${component.kind}): ${component.status}`)
}
```
- **`status`** is the whole platform in one word: `healthy`, `degraded`,
`down`, or `unknown`.
- **`components`** has a row for the API, the database, each chat tier, and
the `cruxy-embed`, `cruxy-rerank`, and `cruxy-guard` models, each with its own
`status` and when it was last checked. A component that is measurably down
is a `200` carrying `down`; the document is a `503` only when no model
component has a fresh measurement.
- **`public_id` names each component by its public id.** A chat tier's
component `id` keeps its former name on every version to date (`kavi` is
Canto, `vaani` is Saga, and `mira` is Epos), and its `public_id` is `canto`,
`saga`, or `epos`. On every other component, `public_id` equals `id`.
Compare `public_id` against the tier ids. If a component has no `public_id`,
map its former id, as the sample does.
- **The verdicts come from active probes,** not from live traffic, and the
response is never cached.
`GET /api/health` reports only that the gateway's HTTP listener is up. It checks
no model or database, so do not read it as platform status.
## Production checklist
The short list for reliability is below. The full list, with keys, memory,
and logging, is [Production checklist](/docs/en/best-practices/production-checklist).
- **Use a developer API key on a server.** See
[Get your API key](/docs/en/get-started/api-keys).
- **Pin `Cruxy-Version`,** and ignore JSON fields you do not recognize.
- **Set client timeouts above the tier deadline,** or above five minutes for a
stream.
- **Stream long `canto` turns,** and size `max_tokens` for the thinking as well
as the answer.
- **Send an `Idempotency-Key`** on every request that takes one, and reuse it
on that request's retries.
- **Retry only what the table above says,** honouring `Retry-After`, with a
cap on attempts.
- **Check for `[DONE]`,** or a `finish` frame on v2, before you show a streamed
answer.
- **Log the `X-Request-Id`, the `request_id`, and the `Cruxy-Version`** of
every call.
- **Read [Known limitations](/docs/en/build/known-limitations)** before you
launch.
## Next steps
- [Errors and retries](/docs/en/build/errors-and-retries): every error code, and a retrying client.
- [Streaming](/docs/en/build/streaming): the frames, and a complete stream reader.
- [Rate limits and budgets](/docs/en/build/rate-limits): read your headroom before you hit a `429`.
- [Known limitations](/docs/en/build/known-limitations): what the API does not do yet, and the workarounds.
================================================================================
Page: Errors and retries
URL: https://learn.cruxyai.com/docs/en/build/errors-and-retries
Markdown: https://learn.cruxyai.com/docs/en/build/errors-and-retries.md
================================================================================
# Errors and retries
Read the error envelope, look up every error code the API defines, and decide which failures to retry and how long to wait.
A failed request returns a non-2xx status and a JSON error body. Read the HTTP
status first, then branch on the body's `code`: the code is stable, while the
wording of the message can change.
## The error envelope
```json
{
"code": "invalid_request",
"error": "invalid JSON body",
"request_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
}
```
| Field | Meaning |
| --- | --- |
| `code` | A stable, machine-readable error code. Branch on this. |
| `error` | A human-readable message. Log it; do not parse it. |
| `request_id` | The id of the failed call, equal to the response's `X-Request-Id` header. Log it with every failure; see [Request IDs and metadata](/docs/en/build/request-ids). |
Some codes add fields next to these three. Each is optional, and present only
on the codes listed:
| Field | Sent on |
| --- | --- |
| `tier` | The tier a model failure or context check was about: `upstream_error`, `upstream_overloaded`, `upstream_timeout`, `model_unavailable`, `service_unavailable`, `server_shutting_down`, and `client_closed_request` on chat, `context_overflow`, and `image_not_supported`. |
| `reason` | Why: `rate` or `concurrency` on `rate_limited` from chat; why a document's text could not be read on `file_not_text`. |
| `retry_after` | The `Retry-After` value in seconds: `rate_limited`, `limits_unavailable`, `credits_unavailable`, `headless_concurrency_limit`. |
| `reset_at` | When the refusal clears (RFC 3339): `budget_exhausted`, `api_share_exhausted`, `key_spend_cap_exceeded`, `workspace_spend_cap_exceeded`, `credits_exhausted`, `file_limit_reached`, and `image_limit_reached`. `null` on `model_not_allowed`, since waiting never clears an allowlist. |
| `model`, `state`, `window`, `epos_available`, `mira_available` | A spend or budget refusal: `budget_exhausted`, `api_share_exhausted`, `key_spend_cap_exceeded`, `workspace_spend_cap_exceeded`, `model_not_allowed`. `model_not_in_plan` sends `model` and `plan` alone, and `file_limit_reached` and `image_limit_reached` send `state` and `window` with no `model`. `window` is `monthly` or `burst`; `epos_available` says whether the same request on `epos` would be served now. `mira_available` is the same value under the field's former name, kept so existing code keeps working; read `epos_available`. |
| `balance`, `currency` | A headless key's remaining credit, on `credits_exhausted`. |
| `dimension` | The check that faulted on `limits_unavailable`: `rate` or `budget`. |
| `required_scope`, `allowed_scopes` | The family the key would need and the families it has, on `key_scope_forbidden`. |
| `required_version` | The `Cruxy-Version` a request field needs pinned, on `invalid_request`. |
| `hazard_codes` | The blocking classifier codes behind `moderation_rejected`. |
| `tool`, `bound`, `limit` | The function and the cap it broke, on `invalid_tool_schema`. `bound` and `limit` also name the cap on `file_too_large`, and `bound` is `chunk` or `input` on `input_not_countable`. `limit` is the context window on `context_overflow`, the plan's monthly allowance on `file_limit_reached` and `image_limit_reached`, the plan's largest upload in bytes on `payload_too_large`, and its storage pool in bytes on `storage_quota_exceeded`. |
| `file_id`, `filename`, `content_type`, `tokens` | The refused file, on `file_not_found`, `file_not_text`, `file_too_large`, and `image_not_supported`. |
| `vision_tiers` | The tiers that read images, heaviest first, on `image_not_supported`. |
| `measured_bytes`, `limit_bytes` | The size and the cap on `input_not_countable`. `limit_bytes` alone is the moderation gate's limit on `moderation_input_too_large`. |
Ignore fields you do not recognize.
Three surfaces use a different shape:
- **`POST /v1/chat/completions`** uses the OpenAI envelope,
`{"error": {"message", "type", "param", "code"}}`. Its `code` is the same
Cruxy code listed here; `type` is a coarse category derived from the status
(`invalid_request_error`, `authentication_error`, `permission_error`,
`not_found_error`, `rate_limit_error`, or `server_error`); `param` is always
`null`. Only the code travels: none of the fields in the table above, such
as `tier` or `hazard_codes`, are carried.
- **Device login** answers a pending or refused poll with an OAuth-style body;
see [Get your API key](/docs/en/get-started/api-keys#device-login).
- **A `POST /api/v2/chat` stream** that fails after it opens ends with an
`error` event carrying `code`, `error`, and `request_id`.
## Handle errors and retry safely
This request sends an `Idempotency-Key`, so once it succeeds, running it again
with the same key replays the stored response instead of calling the model and
billing again. The gateway stores only a success, so a retry after an error
runs the request again under the same key. The TypeScript version retries the
codes the API marks retryable, waiting `Retry-After` when it is sent, and
retries a request that got no response with the same key:
```bash title="curl"
IDEMPOTENCY_KEY=$(uuidgen)
# -i prints the status line and headers, including Cruxy-Version and the
# X-RateLimit-* headers. Run it twice: once the first call succeeds, the
# second replays it.
curl -i https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $IDEMPOTENCY_KEY" \
-d '{
"model": "saga",
"stream": false,
"messages": [
{ "role": "user", "content": "Hello" }
]
}'
```
```ts title="TypeScript"
class CruxyError extends Error {
constructor(
readonly status: number,
readonly code: string,
readonly requestId: string,
message: string,
) {
super(`${status} ${code}: ${message} (request_id ${requestId})`)
}
}
// Your retry budget and timeout. Tune them for your workload; they are not API limits.
// TIMEOUT_MS sits above the longest tier deadline (90 s), so a slow model
// reaches you as 504 upstream_timeout rather than as your own abort.
const MAX_ATTEMPTS = 4
const BASE_DELAY_MS = 500
const TIMEOUT_MS = 120_000
// Transient failures the API marks retryable. upstream_timeout is left out:
// the gateway already made its one attempt, and the same request usually
// times out again.
const RETRYABLE = new Set([
'rate_limited',
'credits_unavailable',
'headless_concurrency_limit',
'limits_unavailable',
'upstream_overloaded',
'model_unavailable',
'all_models_unavailable',
'server_shutting_down',
'internal_error',
])
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
// Exponential backoff with full jitter.
const backoff = (attempt: number) => Math.random() * BASE_DELAY_MS * 2 ** attempt
async function chat(body: Record) {
// One key per logical request, reused on every retry of it.
const idempotencyKey = crypto.randomUUID()
for (let attempt = 1; ; attempt++) {
let res: Response
try {
res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': idempotencyKey,
},
body: JSON.stringify({ ...body, stream: false }),
signal: AbortSignal.timeout(TIMEOUT_MS),
})
} catch (err) {
// No response: the call may or may not have run. If it succeeded, the
// same key replays its stored response rather than running and billing
// it a second time; if it failed, the same key runs it again.
if (attempt >= MAX_ATTEMPTS) throw err
await sleep(backoff(attempt))
continue
}
if (res.ok) return res.json()
const { code, error, request_id } = await res.json()
// An error is never stored, so the same key runs the request again.
if (RETRYABLE.has(code) && attempt < MAX_ATTEMPTS) {
const retryAfter = Number(res.headers.get('Retry-After'))
await sleep(retryAfter > 0 ? retryAfter * 1000 : backoff(attempt))
continue
}
throw new CruxyError(res.status, code, request_id, error)
}
}
const completion = await chat({
model: 'saga',
messages: [{ role: 'user', content: 'Hello' }],
})
console.log(completion.message.content)
```
## Which failures to retry
| Failure | Retry? | How |
| --- | --- | --- |
| `429` `rate_limited`, `credits_unavailable`, `headless_concurrency_limit` | Yes | Wait `Retry-After` seconds, then retry. |
| `503` `upstream_overloaded`, `model_unavailable`, `all_models_unavailable`, `service_unavailable`, `server_shutting_down`, `limits_unavailable` | Yes | Back off, honouring `Retry-After` where it is sent. The gateway already retried the model before it answered `upstream_overloaded` or `model_unavailable`. |
| `500` `internal_error` | Yes | Back off. A write may have run, so retry with the same `Idempotency-Key` where the route takes one. |
| `504` `upstream_timeout` on chat | Only if changed | The same request usually times out again. Stream it, or lower `max_tokens` or `reasoning_effort`; see [Timeouts](#timeouts). |
| `504` `upstream_timeout` on embeddings, rerank, or moderations | Yes | Back off, then retry unchanged; see [Embeddings, rerank, and moderations](/docs/en/build/reliability#embeddings-rerank-and-moderations). |
| `429` `budget_exhausted`, `api_share_exhausted`, `key_spend_cap_exceeded`, `workspace_spend_cap_exceeded`, `credits_exhausted`, `file_limit_reached`, `image_limit_reached` | Not now | The allowance clears only at `reset_at`, or when the cap changes. `Retry-After` counts down to that moment. |
| `403` `model_not_in_plan` | No | The account's plan does not include the tier the request named. Send a tier from `models` on `GET /api/v1/limits`, or `auto`; see [Plan limits](/docs/en/build/plan-limits). |
| `422` `structured_output_unsatisfied` | Yes | It may pass on a retry. The attempt was billed; a retry with the same key and body is not billed again. |
| `409` `idempotency_conflict` | Yes, adjusted | Resend the original body to replay the stored response, or send the new body under a new key. |
| No response at all: a dropped connection or your own timeout | Yes | Retry with the same `Idempotency-Key`. |
| A stream that ended before `data: [DONE]` | Yes | Send the request again; see [Streaming](/docs/en/build/streaming#done-is-the-only-success-signal). |
| `502` `upstream_error` from a model call | No | The model provider rejected the request, and it fails the same way again. From native sign-in it is retryable. |
| `503` `count_unavailable` | No | A fact of the deployment; it does not clear. |
| Every other code | No | It describes the request or the credential. Fix the request, the key, or the target first. |
[Rate limits and budgets](/docs/en/build/rate-limits) explains the difference
between a rate limit and a budget, and how to read your remaining headroom.
### Timeouts
With `stream: false`, the gateway makes one attempt at the model, bounded by
the tier's deadline:
| Tier | Deadline |
| --- | --- |
| `canto` | 90 seconds |
| `saga` | 90 seconds |
| `epos` | 45 seconds |
An attempt that runs past the deadline is cancelled and answered `504`
`upstream_timeout`, and nothing is billed. The gateway does not retry a
timeout: a turn that needed longer needs longer again. Send `stream: true`
instead, since a stream may run for up to five minutes, or ask for less with a
lower `max_tokens` or `reasoning_effort`. [Reliability](/docs/en/build/reliability)
collects the deadlines, retries, and stream checks in one place.
Other upstream failures, such as an overloaded or failing model or a dropped
connection, are retried inside the gateway a bounded number of times, with
backoff, before it answers `503` `upstream_overloaded` or `model_unavailable`.
On `stream: true` that happens only while the stream opens, never after a byte
has reached you. A call never moves to another tier once it is sent:
`model: "auto"` picks a healthy tier before the call, and an explicit tier that
keeps failing is refused at once with `503` `model_unavailable` while it cools
down.
### Back off
- **Honour `Retry-After`.** Every `429` carries it, and so does a `503`
`limits_unavailable`. On `rate_limited`, `X-RateLimit-Reset` gives the same
moment as an absolute Unix time in seconds. On a budget or spend-cap refusal,
`Retry-After` counts down to `reset_at` instead, while the `X-RateLimit-*`
headers still describe the per-minute request window.
- **Otherwise back off exponentially, with jitter.** Double the wait after each
failed attempt and randomize it, so many clients that failed together do not
retry together.
- **Cap the attempts and the total wait.** A retry budget keeps a persistent
failure from turning into a stuck request.
- **Log `request_id` for every failure,** retried or not.
### Make retries idempotent
`Idempotency-Key` makes a retry safe. The first call with a key is served, and
when it succeeds its response is stored for 24 hours. A later or concurrent
call with the same key and the same body replays that response byte for byte,
without calling the model again and without billing again; a concurrent
duplicate waits for the first call to finish.
Only a success is stored. An error, a `429` or any `5xx` included, releases the
key, so a retry with the same key and body runs the request again. Reusing the
key is the intended way to retry a `429` or a `5xx`.
- **Scope:** a key belongs to your API key and to the endpoint. On a
conversation append, it belongs to the conversation.
- **Conflicts:** the same key with a different body is `409`
`idempotency_conflict`.
- **Large responses:** a response over 256 KiB is served but not stored, so a
retry runs again. It is still billed once.
- **Length:** keys are at most 255 characters.
These endpoints accept it:
- `POST /api/v1/chat`, `POST /api/v2/chat`, and `POST /v1/chat/completions`
- `POST /api/v1/embeddings`, `POST /api/v1/rerank`, and `POST /api/v1/moderations`
- `POST /api/v1/conversations/{id}/messages` and
`POST /api/v1/conversations/{id}/messages/batch`, which store their `201`
and replay it without appending a second time
`POST /api/v1/files` reads the header for the upload count alone: a retry
under the same key and content is counted once, but every attempt stores a
file.
> **Note:** A `stream: true` chat stores and replays nothing, because a stream cannot be
> replayed. A retry with the same key and body streams a new generation; when
> both attempts complete, the request is billed once. An attempt that did not
> complete is billed for the tokens it delivered, and the retry is billed as well.
## Error codes
The codes the API defines, grouped by the status each is returned with. Codes that belong to a feature that is switched off are left out.
### 400 Bad Request
The request itself is wrong. Fix it; sending it again unchanged fails the same
way.
| `code` | Meaning |
| --- | --- |
| `invalid_request` | Malformed JSON, or a field that is missing or invalid. Carries `required_version` when the request uses a field that needs a newer `Cruxy-Version` pin. |
| `context_overflow` | The input exceeds the context window: the smaller of the tier's and the plan's, named in `limit`. |
| `moderation_input_too_large` | Chat only (`POST /api/v1/chat`, `POST /api/v2/chat`, and `POST /v1/chat/completions`). The input moderation gate reads the whole request: every message of every role, tool-call arguments, and the text of attached files. Text it already passed for your account, such as earlier turns of the same thread, is not read again. A turn that brings more new text than the gate reads in one request is refused rather than served unread. Carries `limit_bytes`, that limit in bytes (262,144 by default), except on `POST /v1/chat/completions`, whose envelope has no place for it. Nothing is billed. The same body fails the same way: send the new text over several turns. |
| `invalid_schema` | A `response_format` JSON schema is malformed, oversized, too deep, or uses an unsupported keyword. |
| `invalid_tool_schema` | A tool's `parameters` schema exceeds a bound. Adds `tool`, `bound`, and `limit`. The fix belongs in your client, so a retry cannot help. |
| `file_not_text` | A file named in `file_ids` cannot be read as text. Names the file; a document that could not be read also carries a `reason`. |
| `file_too_large` | A file named in `file_ids` exceeds a size cap. Names the file, with `bound`, `limit`, and `tokens`. |
| `image_not_supported` | A file named in `file_ids` on this turn is an image, and the tier serving the turn does not read images. Names the image and the tier, and lists the tiers that do in `vision_tiers`. |
| `moderation_rejected` | The server-side input moderation gate refused the request's text. Carries `hazard_codes`. Not billed. |
| `pii_rejected` | A memory or rule contains an email address, phone number, or card number. Rephrase without it and retry. |
| `rules_too_large` | A ruleset exceeds a size cap. Names the cap. |
| `input_not_countable` | `POST /api/v1/tokens/count` refuses to count this text and returns no count. |
| `invalid_version` | The `Cruxy-Version` header is not a `YYYY-MM-DD` date. Answered on every route, before authentication. |
| `unknown_version` | `Cruxy-Version` is a well-formed date this server does not support. The message lists the supported dates. Answered on every route, before authentication. |
| `invalid_payload` | `POST /api/v1/feedback` only: the payload does not match the schema for its type. |
| `provider_not_supported` | Native sign-in named a provider that is not supported. |
| `redirect_uri_not_allowed` | Native sign-in named a redirect URI that is not allowlisted. |
| `scope_unsupported` | Retired: never sent. It stays in the vocabulary so the string is never reused. |
### 401 Unauthorized
| `code` | Meaning |
| --- | --- |
| `unauthorized` | The Bearer credential is missing, malformed, unknown, expired, or revoked, or its holder no longer belongs to the organization it names. Each case gets the same answer, with `WWW-Authenticate: Bearer`. It is the only `401` code; see [Get your API key](/docs/en/get-started/api-keys#expiry-and-revocation). |
### 403 Forbidden
The credential is genuine, but it cannot make this call. Waiting never widens a
credential, so retrying cannot succeed.
| `code` | Meaning |
| --- | --- |
| `account_suspended` | The key's owner's account is suspended. |
| `credential_quarantined` | The organization the key belongs to could not be established, so the key is refused rather than trusted. Signing in again does not clear it; contact support. |
| `forbidden_bucket` | The credential's billing kind cannot be billed on this route. No key issued today can reach it. |
| `key_scope_forbidden` | The key is not scoped for this endpoint family, or for the memory, rules, or files feature a request field asked for. Names `required_scope` in the second case. Re-scope or re-issue the key. |
| `model_not_allowed` | The key's model allowlist excludes the requested tier. |
| `model_not_in_plan` | Chat only: the request named a tier the account's plan does not include. Carries `model` and `plan`. |
| `plan_required` | A developer API key whose owner's plan does not include API access. A signed-in credential is not refused this way. |
| `web_credential_required` | The route is available to Cruxy's own apps only, not to API keys or CLI logins. |
| `key_read_only` | The key may not perform billable operations. |
| `org_forbidden` | The operation needs an organization owner or admin. |
| `workspace_forbidden` | Workspace management needs an organization owner or admin. |
| `logs_forbidden` | Request logs need an organization owner or admin. |
| `rules_forbidden` | You may not read or change this ruleset. |
| `vault_forbidden` | You take part in the vault but lack the permission the operation needs. |
| `conversation_forbidden` | The conversation is in a vault you may read but not write. |
### 404 Not Found
The target does not exist, or you cannot see it. The API gives the same answer
for an id that is missing and one you cannot see, so a `404` never confirms
that an id exists.
| `code` | What was not found |
| --- | --- |
| `user_not_found` | A user. |
| `file_not_found` | A file of yours under this key's organization, including one named in `file_ids`. |
| `request_not_found` | A `request_id` from a call this account made, on `POST /api/v1/feedback`. |
| `memory_suggestion_not_found` | `POST /api/v1/memories/suggestions/{id}/approve` and `/reject` only: your organization has no pending suggestion with that id. One already approved or rejected, one in another organization, and one that never existed all get this answer. |
| `memory_not_found` | A memory. |
| `vault_not_found` | A vault you take part in. |
| `vault_member_not_found` | A vault member. |
| `org_member_not_found` | An organization member. |
| `invitation_not_found` | An invitation addressed to you that is still open. |
| `workspace_not_found` | A workspace in your organization. |
| `log_not_found` | A request log in your organization. |
| `conversation_not_found` | A conversation you can see. |
| `message_not_found` | A message in the conversation. |
| `prompt_not_found` | A prompt in your workspace. |
| `prompt_version_not_found` | That version of the prompt. |
| `eval_suite_not_found` | An eval suite in your workspace. |
| `eval_run_not_found` | An eval run in your workspace. |
| `eval_case_not_found` | A case in that eval suite. |
### 409 Conflict
The request conflicts with existing state. Only `idempotency_conflict` has a
retry path.
| `code` | Meaning |
| --- | --- |
| `idempotency_conflict` | The `Idempotency-Key` was reused with a different body. Resend the original body to replay the stored response, or use a new key. |
| `vault_name_exists` | A vault with this name already exists in your organization. |
| `vault_external_id_conflict` | The `external_id` names a vault somebody else owns. |
| `vault_member_exists` | The user is already a member of the vault. |
| `org_member_exists` | The user is already a member of the organization. |
| `org_member_active` | The member has not left the organization. |
| `workspace_slug_exists` | A workspace with this slug already exists. |
| `workspace_not_empty` | The workspace holds, or has held, a credential, so it cannot be deleted. |
| `workspace_is_default` | The default workspace cannot be deleted. |
| `prompt_slug_exists` | A prompt with this slug already exists. |
| `eval_suite_slug_exists` | An eval suite with this slug already exists. |
### 413 Payload Too Large
| `code` | Meaning |
| --- | --- |
| `payload_too_large` | The upload or message is over its size cap. |
| `storage_quota_exceeded` | The upload would take your files past your total storage allowance. |
### 422 Unprocessable Content
| `code` | Meaning |
| --- | --- |
| `structured_output_unsupported` | The tier that served the request does not support constrained decoding for `response_format`. |
| `structured_output_unsatisfied` | The model's non-streaming output did not satisfy the requested JSON schema. The request is still billed. |
### 429 Too Many Requests
Every `429` carries `Retry-After`, and nothing is billed.
| `code` | Meaning |
| --- | --- |
| `rate_limited` | Too many requests: `reason` is `rate` (the per-minute request window) or `concurrency` (too many of your requests in flight at once). Retry after `Retry-After`. |
| `budget_exhausted` | A signed-in plan's monthly or 12-hour burst pool is spent, or `epos`'s monthly request cap. `epos_available` says whether the same request on `epos` would be served now. Clears at `reset_at`. |
| `api_share_exhausted` | Chat, embeddings, and rerank, on a developer API key: the account's developer keys have used the plan's API share, 25% of its monthly units. Clears at `reset_at`. |
| `file_limit_reached`, `image_limit_reached` | `POST /api/v1/files`: the plan's monthly allowance of stored files or images is used. Clears at `reset_at`. |
| `message_limit_reached` | Retired: never sent. It stays in the vocabulary so the string is never reused. |
| `key_spend_cap_exceeded` | The key's monthly spend cap is spent. Clears at `reset_at`. |
| `workspace_spend_cap_exceeded` | The workspace's monthly spend cap is spent. Clears at `reset_at`. |
| `credits_exhausted` | A headless key's credit pool is drained. Clears at `reset_at`. |
| `credits_unavailable` | A headless key's credit balance could not be read. Retry after `Retry-After` (2 seconds). |
| `headless_concurrency_limit` | Chat only: a headless key already has its maximum requests in flight. Retry after `Retry-After` (1 second). |
### 5xx Server errors
On chat, every model failure adds `tier`, and none is billed.
| `code` | Status | Meaning |
| --- | --- | --- |
| `internal_error` | `500` | An unexpected server fault, on any route. Retry with backoff. |
| `upstream_error` | `502` | On a model call, the model provider rejected the request the gateway sent, and a retry fails the same way. On native sign-in, the identity provider could not be reached or answered unusably; retry. |
| `upstream_overloaded` | `503` | The model is overloaded, and the gateway's own retries ran out. Retry with backoff. |
| `model_unavailable` | `503` | The tier is temporarily unavailable: it kept failing and is cooling down, or it failed again on this request after the gateway's retries. Retry with backoff. |
| `all_models_unavailable` | `503` | Chat only: `model: "auto"` was sent and every tier is unavailable. Retry with backoff. |
| `service_unavailable` | `503` | The model provider refused to serve Cruxy at all: an outage on Cruxy's side, never caused by the request. Nothing is billed. Retry in a few minutes. |
| `server_shutting_down` | `503` | The gateway restarted, for a deploy, before it answered. Retry with backoff. |
| `limits_unavailable` | `503` | A rate-limit or budget check could not be read, so the request was refused rather than served unmetered. Adds `dimension`. Retry after `Retry-After` (5 seconds). |
| `count_unavailable` | `503` | `POST /api/v1/tokens/count` cannot count for this tier, because the count could disagree with billing. It does not clear on a retry. |
| `upstream_timeout` | `504` | The model did not answer in time. With `stream: false` the gateway made one attempt and does not retry it; see [Timeouts](#timeouts). On embeddings, rerank, and moderations it is transient: retry with backoff. |
### Stream errors
A stream that fails after it opens has already sent its `200`, so the code
arrives in the stream, not the status:
- `POST /api/v2/chat` ends with an `error` event whose `code` is one of
`upstream_error`, `upstream_overloaded`, `upstream_timeout`,
`model_unavailable`, `server_shutting_down`, or `internal_error`.
- `POST /v1/chat/completions` ends with a `data: {"error": {...}}` frame.
- `POST /api/v1/chat` sends no error frame: the stream ends without
`data: [DONE]`; see [Streaming](/docs/en/build/streaming#done-is-the-only-success-signal).
### Codes in request logs
`client_closed_request` (`499`) records that the caller disconnected before the
gateway had an answer. Nobody receives it; it appears only in request logs.
## Next steps
- [Reliability](/docs/en/build/reliability): deadlines, retries, idempotency, and platform status together.
- [Request IDs and metadata](/docs/en/build/request-ids): find the `request_id` and quote it when you report a problem.
- [Rate limits and budgets](/docs/en/build/rate-limits): read your headroom before you hit a `429`.
- [Streaming](/docs/en/build/streaming): detect a stream that failed partway.
- [Get your API key](/docs/en/get-started/api-keys): fix `401` and `403` at the key.
================================================================================
Page: Rate limits and budgets
URL: https://learn.cruxyai.com/docs/en/build/rate-limits
Markdown: https://learn.cruxyai.com/docs/en/build/rate-limits.md
================================================================================
# Rate limits and budgets
How the gateway meters you: rate_limited versus budget_exhausted, the rate-limit headers, spend caps, and reading your live headroom.
Two separate controls can refuse a request with `429`: a **rate limit** on how
fast you send requests, and a **budget** on how much you spend. The body's
`code` tells you which one you hit, and they need different handling. Your
account's limits are shown in your Console, and `GET /api/v1/limits` returns
them, with live headroom, to your code.
## rate_limited or budget_exhausted
| | rate_limited | budget_exhausted |
| --- | --- | --- |
| What ran out | Request rate: too many requests in the current window. | Allowance: the plan's monthly cap, its 12-hour burst pool, or the monthly `epos` request cap is spent. A developer key that has used the plan's API share reads `api_share_exhausted` instead. |
| What to do | Back off and retry after `Retry-After`. | Stop retrying. No immediate retry succeeds. |
| Clears | When the rate window resets. | At `reset_at`, when the spent window resets. |
| Headroom | `rate_limits.chat` on `GET /api/v1/limits` | `token_pool` and `api_share` on `GET /api/v1/limits` |
Spend caps refuse with `429` too, under their own codes: a monthly cap on a
key (`key_spend_cap_exceeded`), a monthly cap on a workspace
(`workspace_spend_cap_exceeded`), and the credit pool (`credits_exhausted`).
Like `budget_exhausted`, they mean an allowance is spent, and they clear only
at `reset_at` or when the cap changes. Every `429` carries `Retry-After`; on
these codes and on `budget_exhausted` it counts down to `reset_at`, and
retrying sooner fails the same way.
`POST /api/v1/moderations` never returns `budget_exhausted`: moderation is
exempt from the subscription pool, so a spent allowance cannot switch it off.
It can still return `rate_limited` or a spend-cap code.
## Rate-limit headers
Metered responses carry the state of the bucket the route is metered on:
| Header | Meaning |
| --- | --- |
| `X-RateLimit-Limit` | Requests allowed per window. |
| `X-RateLimit-Remaining` | Requests left in the current window, never below `0`. |
| `X-RateLimit-Reset` | When the current window ends, as an absolute Unix time in seconds, not a duration. |
| `Retry-After` | On every `429`, the seconds to wait. On a budget or spend-cap refusal, the seconds until `reset_at`. |
The `X-RateLimit-*` headers always describe the per-minute request window, so
a budget refusal can still show requests remaining.
The `X-RateLimit-*` headers are absent on a `401`, because authentication runs
before the limiter, and on a `403` from the key check (`key_scope_forbidden`,
`key_read_only`). They are also absent when the limiter cannot reach its own
storage; that request is served unmetered, or refused with `503`
`limits_unavailable` where the deployment fails closed.
```bash title="curl"
# -D - prints the response headers; look for X-RateLimit-* and, on a 429, Retry-After.
curl -s -D - -o /dev/null https://api.cruxy.ai/api/v1/limits \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
// These clear only at reset_at; every other 429 clears after Retry-After.
const ALLOWANCE_SPENT = new Set([
'budget_exhausted',
'key_spend_cap_exceeded',
'workspace_spend_cap_exceeded',
'credits_exhausted',
])
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'epos',
stream: false,
messages: [{ role: 'user', content: 'Hello' }],
}),
})
const remaining = res.headers.get('X-RateLimit-Remaining')
const resetAt = res.headers.get('X-RateLimit-Reset') // Unix seconds
if (res.status === 429) {
const { code, reset_at, request_id } = await res.json()
const waitSeconds = Number(res.headers.get('Retry-After'))
if (ALLOWANCE_SPENT.has(code)) {
// No retry succeeds before reset_at.
console.log(`allowance spent: ${code}, resets at ${reset_at} (request_id ${request_id})`)
} else {
// rate_limited, or a transient headless check: wait Retry-After, then retry.
console.log(`${code}; retry in ${waitSeconds}s (request_id ${request_id})`)
}
} else if (remaining !== null) {
console.log(`${remaining} requests left until ${new Date(Number(resetAt) * 1000).toISOString()}`)
}
```
Pace yourself before you reach `0`: when `X-RateLimit-Remaining` runs low,
slow down until `X-RateLimit-Reset` rather than waiting for a `429`.
## Read your limits
`GET /api/v1/limits` returns the limits that apply to your key, with live
headroom. Every value comes from the same limiter and budget state that chat
enforces, so `remaining` is exactly what your next request is checked against.
The call costs nothing, writes nothing, and is rate-limited like any other
route.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/limits \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/limits', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
if (!res.ok) throw new Error(`limits request failed: ${res.status}`)
const limits = await res.json()
const perKey = limits.rate_limits.chat.per_key
console.log(`chat: ${perKey.remaining} of ${perKey.limit} left this window`)
if (limits.token_pool) {
console.log(`token pool: ${limits.token_pool.state}`) // healthy, low, or blocked
}
```
Everything comes from the key you send; nothing is read from the request.
Which blocks appear depends on your billing `bucket`:
| Field | Meaning |
| --- | --- |
| `object` | `limits`. |
| `bucket` | Which kind of key this is: `subscription` (a signed-in credential), `apikey` (a developer API key, which draws on its owner's plan and needs a paid one), or `headless` (billed from prepaid `credits`). Both `subscription` and `apikey` carry `token_pool`. Memory and standing rules are on by default only for `subscription`. |
| `tier` | Your account's plan: `free`, `core`, `advanced`, `pro`, or `max`. It sizes `token_pool` and picks the `plan_limits` row; see [Plan limits](/docs/en/build/plan-limits). A Pro subscription from the beta reads `pro_legacy` and has Pro's limits. The field can also read `ultra` or `enterprise`. This is not a model tier. |
| `models` | Subscription and developer-key buckets. The chat tiers your plan includes, by public id: `saga` on Free; `epos` and `saga` on Core; all three from Advanced. A request naming another is `403` `model_not_in_plan`, and `auto` routes only among these. |
| `context_window` | Subscription and developer-key buckets. `plan` is your plan's window in tokens (256,000 on Free, 1,000,000 on a paid plan), and `models` maps each included tier to its effective window, the smaller of the tier's own and the plan's. A turn past it is `400` `context_overflow`. |
| `api_share` | On every plan with API access. The share of the month's `token_pool` that developer API keys together may use: `share` (`0.25`), `cap`, `used`, `remaining`, `state`, `window` (`monthly`), `starts_at`, and `resets_at`, in `units`. Past it a developer-key request is `429` `api_share_exhausted`; a signed-in credential is never counted or refused by it. |
| `rate_limits.chat.max_concurrent_per_key` | Developer keys only. How many chat requests this key may have in flight at once, streaming or not, within the account's `max_concurrent_requests`. One more is `429` `rate_limited` with `reason: concurrency` and `Retry-After: 1`. |
| `rate_limits.chat.per_key` | The chat request-rate window for this key: `limit`, `remaining`, `resets_at`, and `window_seconds`. `resets_at` is `null` until a request opens the window. |
| `rate_limits.chat.per_org` | The same window, for your whole organization. |
| `rate_limits.chat.max_concurrent_requests` | How many chat requests your account may have in flight at once, across all three chat endpoints. One more is refused at once, not queued: `429` `rate_limited` with `reason: concurrency` and `Retry-After: 1`. |
| `token_pool` | Subscription and developer-key buckets. The usage budget behind `budget_exhausted`, in `units`. |
| `plan_limits` | Subscription and developer-key buckets. Your plan's row of the limits table: `files_per_month`, `images_per_month`, `files_per_message`, `images_per_message`, `storage_bytes`, `max_file_bytes`, `projects`, `works`, and `context_window`. A count that is `null` has no fixed cap. The row never changes within a month, so read it once; see [Plan limits](/docs/en/build/plan-limits). |
| `files`, `images` | Subscription and developer-key buckets, for each kind of upload the plan caps (Max caps neither, so it has neither block). This month's stored uploads against the plan's allowance: `cap`, `used`, `remaining`, `state`, `window` (`monthly`), `starts_at`, and `resets_at`. An image is a file whose detected type is `image/*`; it never spends a file. |
| `storage` | What you hold against the plan's storage pool, in `unit: bytes`: `cap`, `used`, `remaining`, and `state`. |
| `credits` | Headless bucket only. The dollar-credit pool: `granted`, `adjustment`, `used`, `remaining`, and `resets_at`. |
| `key_spend_cap` | Present only when the key has a monthly spend cap: `cap`, `spent`, `remaining`, and `resets_at`, in USD. |
| `workspace_spend_cap` | Present only when the key's workspace has a monthly spend cap, in the same shape. |
`token_pool` has two windows, each with `cap`, `used`, `remaining`,
`percent_used`, `resets_at`, and a `state` of `healthy`, `low`, or `blocked`:
- `monthly`: the cap over your account's own month, with `starts_at` beside
`resets_at`. A paid plan's month is its billing period; a Free account's runs
from the day of the month it was created. No allowance runs on the calendar
month.
- `burst`: the trailing 12-hour sliding cap, on every plan.
Every monthly allowance on the response, `token_pool.monthly`, `api_share`,
`files`, and `images`, is measured over that same month and resets at the same
instant, the `resets_at` each one carries. There is no message allowance:
nothing counts messages on any plan. The spend caps are the exception: they
run on the UTC calendar month.
`token_pool` also carries an overall `state`, `blocked_models`, and the `unit`
(`units`). While an operator has the Epos request brake on, it carries `epos`,
a monthly request cap on the `epos` tier with its own `cap`, `used`,
`remaining`, `resets_at`, and `state`. Next to it,
`mira` carries the same cap under the field's former name, present whenever
`epos` is and with the same values, so existing code keeps working. Read
`epos`.
`percent_used` is `used` over `cap` as a percentage from 0 to 100, to one
decimal place, rounded down, so it reads `100` only once the cap is reached.
On a `token_pool` window it is `null` when there is no cap to measure
against. `api_share`, `files`, `images`, and `storage` carry it too. Draw a
usage bar from it instead of dividing `used` by `cap`.
There is no tokens-per-minute limit and no per-model daily cap. The gateway
enforces neither, so neither appears here.
## Read your month so far
`GET /api/v1/limits/month` returns the month in progress day by day: one entry
per UTC day from the day the month began to today, oldest first, each with a
`date` and a `percent_used`. A day's `percent_used` is the share of the
month's limit used from the start of the month through the end of that day, and
through now for today. It is the same measure as
`token_pool.monthly.percent_used` on `GET /api/v1/limits`, over the same
month, so today's entry equals that figure, and the response's `starts_at` and
`resets_at` are that window.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/limits/month \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/limits/month', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
if (!res.ok) throw new Error(`month request failed: ${res.status}`)
const month = await res.json()
if (!month.enforced) {
// A headless key has no unit pool, so there is no series to draw.
console.log('no monthly limit applies to this key')
} else {
for (const day of month.days) {
console.log(`${day.date}: ${day.percent_used}%`)
}
console.log(`resets ${month.resets_at}`)
}
```
```json
{
"object": "limits.month",
"enforced": true,
"starts_at": "2026-11-19T09:30:00Z",
"resets_at": "2026-12-19T09:30:00Z",
"days": [
{ "date": "2026-11-19", "percent_used": 40.1 },
{ "date": "2026-11-20", "percent_used": 42.3 }
]
}
```
The series has no gaps and never falls: a day with no usage repeats the day
before. The response carries no unit count, cap, or remainder; read
`GET /api/v1/limits` for those. Like that call, it reads everything from the
key you send, writes nothing, and costs nothing, and it is rate-limited on its
own bucket. A headless key, which draws on dollar credit and has no unit pool,
gets `enforced: false` and an empty `days`.
## Budgets and auto
A request with `model: "auto"` may be budget-downgraded to `epos`. Read
`cruxy.routed_tier` on the response to see the tier that served it, rather than
assuming the router's first choice; see
[Auto routing](/docs/en/build/auto-routing).
## Next steps
- [Plan limits](/docs/en/build/plan-limits): what each plan allows, every plan's entitlements from `GET /api/v1/limits/plans`, and when each allowance resets.
- [Errors and retries](/docs/en/build/errors-and-retries): every `429` code, and a retry loop that honours `Retry-After`.
- [Features overview](/docs/en/get-started/features): the tiers and `auto`.
- [Get your API key](/docs/en/get-started/api-keys): key restrictions, including spend caps.
================================================================================
Page: Plan limits
URL: https://learn.cruxyai.com/docs/en/build/plan-limits
Markdown: https://learn.cruxyai.com/docs/en/build/plan-limits.md
================================================================================
# Plan limits
What each Cruxy plan allows: models, context window, usage share for the API, uploads, storage, and projects, and when each allowance resets.
Your Cruxy plan sets what the gateway allows a key to do: which tiers it can
call, how much context a turn can carry, how much usage the API may draw, and
how many files it can upload and store. `GET /api/v1/limits` reports your plan
and the live headroom against each allowance; this page is the table it reads
from. Prices are on [cruxyai.com/pricing](https://cruxyai.com/pricing).
## The table
| | Free | Core | Advanced | Pro | Max |
| --- | --- | --- | --- | --- | --- |
| Tiers | `saga` | `epos`, `saga`, and `auto` | `epos`, `saga`, `canto`, and `auto` | `epos`, `saga`, `canto`, and `auto` | `epos`, `saga`, `canto`, and `auto` |
| Context window | 256K | 1M | 1M | 1M | 1M |
| Usage | Limited | 1/4 of Advanced | The baseline | 4x Advanced | 8x Advanced |
| API | Not included | Up to 25% of the plan's usage | Up to 25% of the plan's usage | Up to 25% of the plan's usage | Up to 25% of the plan's usage |
| File uploads a month | 100 | 2,000 | 5,000 | 20,000 | Unlimited, within fair use |
| Image uploads a month | 50 | 1,000 | 2,500 | 10,000 | Unlimited, within fair use |
| Files and images per message | 10 of each | 20 of each | 20 of each | 50 of each | Unlimited |
| Storage | 1 GB | 10 GB | 25 GB | 100 GB | 500 GB |
| Largest file | 25 MB | 50 MB | 100 MB | 100 MB | 100 MB |
| Projects | Up to 3 | Unlimited | Unlimited | Unlimited | Unlimited |
| Cruxy Code | `saga` | `saga` | `canto` | `canto` | `canto` |
Sizes are decimal: 25 MB is 25,000,000 bytes. A Pro subscription from the beta
reads `pro_legacy` on `GET /api/v1/limits` and has Pro's row. An enterprise
account has no row and is held to the deployment's ceilings alone.
## Tiers and context
A Free account calls `saga`. Core adds `epos` and `auto`; Advanced, Pro, and
Max add `canto`. A chat request that names a tier outside the plan is `403`
`model_not_in_plan`, carrying `model` and `plan`, before anything is billed;
waiting does not help, so do not retry it. `auto` routes only among the plan's
tiers, so on Free it is always served `saga`. `GET /api/v1/limits` lists the
plan's tiers as `models`. [Choosing a tier](/docs/en/build/choosing-a-tier)
compares the three, and [Auto routing](/docs/en/build/auto-routing) explains
`auto`.
The context window is the plan's: a turn on Free carries up to 256,000 tokens,
and a turn on a paid plan up to 1,000,000, the smaller of the tier's own
window and the plan's. Input past it is `400` `context_overflow`, whose `limit`
is that number; see [Errors and retries](/docs/en/build/errors-and-retries).
`GET /api/v1/limits` reports both as `context_window`.
## Usage and the API's share
Every plan has a usage pool, `token_pool` on `GET /api/v1/limits`. Core is a
quarter of Advanced, Pro four times it, and Max eight times it. Everything a
plan does draws on the one pool: the web app, Cruxy Code, and the API.
The API is included on every paid plan. Developer API keys together may use
25% of the plan's monthly units, in the account's month: `api_share` on
`GET /api/v1/limits` reports the share with `cap`, `used`, `remaining`, and
`resets_at`. Past it, a developer-key request on chat, embeddings, or rerank is
`429` `api_share_exhausted`, in the shape of `budget_exhausted`, until
`reset_at`. A signed-in credential is never counted toward the share and never
refused by it. A developer key may also have 4 chat requests in flight at once,
reported as `rate_limits.chat.max_concurrent_per_key`; one more is `429`
`rate_limited` with `reason: concurrency`.
A developer API key on a Free account is refused with `403` `plan_required`; a
signed-in credential works on every plan. See
[Get your API key](/docs/en/get-started/api-keys#two-kinds-of-key).
## When an allowance resets
- **Every 12 hours.** Every plan has a trailing 12-hour window beside its
month, `token_pool.burst`, which trickles back continuously.
- **On your own monthly date.** A paid plan's month is its billing period. A
Free account's month runs from the day of the month it was created, at the
time it was created, in UTC; a day a month does not have is that month's last
day. No allowance runs on the calendar month.
Every monthly figure on `GET /api/v1/limits`, `token_pool.monthly`,
`api_share`, `files`, and `images`, carries the `starts_at` and `resets_at` of
that month, and every
`429` that refuses for a spent allowance carries the same instant as
`reset_at`. An upgrade applies to the month in progress; a downgrade applies at
the next one.
## Uploads and storage
`POST /api/v1/files` holds every upload to the plan's row:
- **Files and images a month** are counted apart, by the file's detected media
type: `image/*` is an image, anything else a file. One stored upload is one;
a deleted or expired file stays counted. One past the allowance is `429`
`file_limit_reached` or `image_limit_reached`, refused before a byte is
stored, with `limit`, `used`, and `reset_at`. A retry under the same
`Idempotency-Key` and content counts once.
- **The largest file** is `413` `payload_too_large` past the plan's size, with
`limit` in bytes.
- **Storage** is `413` `storage_quota_exceeded` past the plan's pool, with
`limit` and `used`, counted as files plus their extracted text per
organization and user. A pool lowered under what an account holds refuses
new uploads and deletes nothing.
One chat request may name up to the plan's count of files and of images in
`file_ids`. A Free account may create up to 3 projects. A cap only ever refuses
creating more: nothing you have is deleted or hidden when you move to a smaller
plan.
## Read every plan
`GET /api/v1/limits/plans` returns this table for every plan on sale, in the
order the pricing page lists them, the same for every caller and read from the
tables the gateway enforces on, so a client that offers an upgrade holds no
copy of it. Each entry carries `plan`, `models` (the tiers it includes, by the
ids your `Cruxy-Version` names them), `context_window`, `usage_multiple` (the
plan's monthly allowance over Advanced's: `0.25` on Core, `1` on Advanced, `4`
on Pro, `8` on Max, `null` on Free), and `plan_limits` in the shape below. It
says nothing about your own plan or usage; read `GET /api/v1/limits` for that.
It is authenticated, rate-limited on its own bucket, and not billed.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/limits/plans \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/limits/plans', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
if (!res.ok) throw new Error(`plans request failed: ${res.status}`)
const { plans } = await res.json()
for (const p of plans) {
// usage_multiple is null on free; a null count in plan_limits is unlimited.
console.log(
`${p.plan}: ${p.models.join(', ')}, ${p.context_window} tokens,`,
`${p.usage_multiple ?? 'limited'}x Advanced, ${p.plan_limits.files_per_month ?? 'unlimited'} files a month`,
)
}
```
## Read your plan's row
`plan_limits` on `GET /api/v1/limits` is your plan's row, so your code never
holds a copy of this table. A count that is `null` has no fixed cap.
```bash title="curl"
curl https://api.cruxy.ai/api/v1/limits \
-H "Authorization: Bearer $CRUXY_API_KEY"
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/limits', {
headers: { Authorization: `Bearer ${process.env.CRUXY_API_KEY}` },
})
if (!res.ok) throw new Error(`limits request failed: ${res.status}`)
const limits = await res.json()
console.log(`plan ${limits.tier}: ${limits.models.join(', ')}`)
console.log(`context window: ${limits.context_window.plan} tokens`)
const row = limits.plan_limits
if (row) {
// null means no fixed cap on this plan.
console.log(`files a month: ${row.files_per_month ?? 'unlimited'}`)
console.log(`largest file: ${row.max_file_bytes} bytes`)
}
if (limits.files) {
console.log(`files: ${limits.files.used} of ${limits.files.cap}, resets ${limits.files.resets_at}`)
}
if (limits.storage) {
console.log(`storage: ${limits.storage.remaining} bytes left`)
}
if (limits.api_share) {
// Developer keys together: 25% of the plan's month.
console.log(`API share: ${limits.api_share.remaining} of ${limits.api_share.cap} units left`)
}
```
## Next steps
- [Rate limits and budgets](/docs/en/build/rate-limits): every block on `GET /api/v1/limits`, the month so far from `GET /api/v1/limits/month`, and the `429` codes.
- [Get your API key](/docs/en/get-started/api-keys): the two kinds of key, and which plans the API needs.
- [Errors and retries](/docs/en/build/errors-and-retries): which refusals to retry, and when.
================================================================================
Page: Usage and cost
URL: https://learn.cruxyai.com/docs/en/build/usage-and-cost
Markdown: https://learn.cruxyai.com/docs/en/build/usage-and-cost.md
================================================================================
# Usage and cost
Read the usage object on each response: token counts, billable and cached tokens, the shape of the cost object, and how a stream reports usage.
Every chat response reports what the call consumed in a `usage` object: its
token counts and a `cost` object. This page documents the shape of both. Cost
amounts and rates are specific to your account and shown in your Cruxy
Console, so the examples here use placeholders where those numbers go. Read
the real values from your own responses.
```bash title="curl"
# usage, with its cost object, is near the end of the response body.
curl https://api.cruxy.ai/api/v1/chat \
-H "Authorization: Bearer $CRUXY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "saga",
"messages": [
{ "role": "user", "content": "Name three monsoon fruits." }
]
}'
```
```ts title="TypeScript"
const res = await fetch('https://api.cruxy.ai/api/v1/chat', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CRUXY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'saga',
messages: [{ role: 'user', content: 'Name three monsoon fruits.' }],
}),
})
if (!res.ok) throw new Error(`chat failed: ${res.status} ${await res.text()}`)
const { usage, cruxy } = await res.json()
console.log(`request ${cruxy.request_id} on ${usage.cost.tier}`)
console.log(`input ${usage.input_tokens} (billable ${usage.billable_input_tokens})`)
console.log(`output ${usage.output_tokens}`)
// cost is an object, not a number: amount is the one figure, in cost.currency.
console.log(`cost ${usage.cost.amount} ${usage.cost.currency}`)
```
## The usage object
A non-streaming `POST /api/v1/chat` response carries `usage` next to `message`.
`input_tokens`, `output_tokens`, `billable_input_tokens`, and `cost` are always
present.
| Field | Meaning |
| --- | --- |
| `input_tokens` | The call's input tokens. |
| `output_tokens` | The tokens the model generated. |
| `billable_input_tokens` | The input tokens the call is billed for. |
| `cache_read_tokens` | Input tokens read from the prompt cache. |
| `cache_write_tokens` | Input tokens written to the prompt cache. |
| `prompt_tokens_details` | The same cache split in OpenAI's shape, `cached_tokens` and `cache_write_tokens`. Present only when the prompt cache was used. |
| `cost` | What the call cost, as an object; see [The cost object](#the-cost-object). |
The token fields have different names on different endpoints. Each set belongs
to a wire contract that has already shipped, so read the one for the endpoint
and mode you called:
| Endpoint and mode | Token fields | Cost |
| --- | --- | --- |
| `POST /api/v1/chat`, `stream: false` | `input_tokens`, `output_tokens`, `billable_input_tokens`, `cache_read_tokens`, `cache_write_tokens` | `usage.cost` |
| `POST /api/v1/chat`, `stream: true` | `prompt_tokens`, `completion_tokens`, `total_tokens`, plus the three Cruxy counts when they are not zero | `usage.cost`, on the usage frame |
| `POST /api/v2/chat`, either mode | `input_tokens`, `output_tokens`, `billable_input_tokens`, `cache_read_tokens`, `cache_write_tokens` | `usage.cost` |
| `POST /api/v1/embeddings` and `POST /api/v1/rerank` | `prompt_tokens`, `total_tokens` | `cruxy.cost` |
| `POST /v1/chat/completions` | `prompt_tokens`, `completion_tokens`, `total_tokens`, and `prompt_tokens_details.cached_tokens` when the cache was used | Not in the response |
| `POST /api/v1/moderations` | Not in the response | Not in the response |
## The cost object
`cost` is an object, not a number. Read `cost.amount` when you want one figure.
```json
{
"tier": "saga",
"amount": "",
"currency": "",
"input_unit": "",
"output_unit": "