# Rules: what a call may do, under which law

Rules are checked at the gateway before the model is called. A refused call costs no model call, and its refusal is sealed like any other decision.

## Policy rules

- A rule matches a call — provider, model, data class, mandate, cost — and blocks it, sends it to another model or asks a person to approve it. Start from a template in **Rules › Rules**; each condition is explained in plain language.
- Simulate a rule on past traffic before publishing it: what it would have blocked, what it would have let through.
- Rule sets are versioned: every decision seals the rule set in force that day.

## Budgets

A budget refuses before the call: `402 budget_exceeded`, with the budget’s name and where to raise it. It applies to the workspace or to one mandate, and a call bound to a mandate must pass both. Enforcement is fail-closed: a check that cannot run never lets spend through.

```
HTTP/1.1 402 Payment Required

{
  "error": "Budget \"Support — monthly\" is used up for this month. …",
  "code": "budget_exceeded",
  "budget_id": "…",
  "manage_url": "/dashboard/regles?tab=budgets"
}
```

## Human approval

A rule can ask a person to approve a call. The gateway answers `202 human_review_required` with an `approval_request_id`; once a distinct reviewer approves it in **Rules › More › Approvals**, `POST /api/v1/infer/resume` runs exactly the call they approved. A streamed call cannot be held, so it is refused instead (`human_review_not_resumable`).

```
HTTP/1.1 202 Accepted
{ "code": "human_review_required", "approval_request_id": "3b9d…", "matched_rules": [ … ] }

# once a distinct reviewer approved it:
curl -X POST https://www.azothos.ai/api/v1/infer/resume \
  -H "Authorization: Bearer $AZOTH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "approval_request_id": "3b9d…" }'
```

## Refusals that explain themselves

A refusal keeps its status and its `code`, and gains a `refusal` block in the caller’s language (`X-Azoth-Locale`, else `Accept-Language`): what was blocked, why, which rule and who owns it, and the compliant options. An option is offered only when every check would pass; taking it sends the call again, governed in full.

```
{
  "error": "Blocked by rule \"No US models for HR data\"",
  "code": "policy_blocked",
  "refusal": {
    "what": "…", "why": "…", "next": "…",
    "rule": { "name": "No US models for HR data" },
    "owner": { "name": "…" },
    "alternatives": [{ "type": "eu_model", "provider": "mistral", "model": "mistral-large-latest", "headers": { … } }],
    "exception": { "available": true, "max_days": 30, "request_url": "/api/v1/exceptions" },
    "trace_id": "…"
  }
}
```

## Exceptions, four eyes

Someone refused by a policy rule can ask for a time-limited exception. The rule’s owner — or an owner or admin, never the requester — decides it. It lifts that rule for exactly the identity that was refused; never a budget, the emergency stop or a legal requirement. Every step is sealed.

## The applicable law

- At each call Azoth collects the factors — where the people concerned are (`X-Azoth-Subject-Zone`, else the mandate’s zone, else the workspace’s), where the model runs, the sector, the data, the AI Act level — selects the law packs that cover them and applies the strictest rule: block, then approval, then reroute, then allow.
- Packs cover the EU (GDPR, AI Act, DORA), the United Kingdom and several US laws. A pack is decision support, never legal advice: it cites its sources and says whether a lawyer of its zone has reviewed it.
- Only an applicable rule acts, and only under enforce: a reviewed pack, a draft the workspace adopted, or the mandate’s own allowed zones. A reroute goes only to a target the workspace declared.
- The reasoning is sealed with every decision of the call, and the offline verifier decides it again from the seal.

> Declare your entities, sectors, provider regions and transfer mechanisms in **Rules › Jurisdictions**. Azoth reads your declarations, never your contracts.

## Systems exposed to the public

- A system your site’s visitors reach — a chatbot — is protected against cost exhaustion as soon as it is marked public, with nothing else to configure.
- Send `X-Azoth-End-User`, an opaque id of the visitor, never an e-mail. A visitor who calls too often is slowed (`429 exhaustion_slow`), then challenged (`428 exhaustion_challenge_required`, with a one-time nonce), then paused (`429 exhaustion_blocked`). An unusual volume on the whole system caps its answers, then challenges every visitor, then pauses the system.
- On a 428, run your captcha, then send the call again with `X-Azoth-Challenge-Passed: <nonce>`.
- These refusals are counted, not sealed one by one: each change of tier and a five-minute aggregate are sealed. The cost avoided is an estimate, never added to realized savings. A public system’s calls have a request limit of their own, at your plan’s figure, counted only once its tier let them through: a flood on it never refuses the workspace’s other keys.

```
import { azothGatewayHeaders, exhaustionChallengeOf } from '@azothos/sdk';

try {
  return await openai.chat.completions.create(body, {
    headers: azothGatewayHeaders({ endUser: session.id }),
  });
} catch (err) {
  const challenge = exhaustionChallengeOf(err);   // a 428: run your captcha first
  if (!challenge) throw err;
  await runCaptcha();
  return openai.chat.completions.create(body, {
    headers: azothGatewayHeaders({ endUser: session.id, challengePassed: challenge.nonce }),
  });
}
```

## Sovereignty and egress

Pin a workspace’s data to providers and regions, with a SecNumCloud strict mode. An air-gapped deployment reaches only the hosts its operator allowed: a model call to any other is refused, and the refusal sealed.

## Governed MCP tools

Agents reach third-party MCP servers through one Azoth endpoint: only approved tools, each call checked against the agent’s mandate, held for a person when it cannot be undone. See [MCP](https://www.azothos.ai/docs/mcp).

## Red-teaming before approval

A campaign runs Azoth’s versioned probes against a system through the real gateway, judged by deterministic oracles. A high-risk system is approved only once its latest campaign meets the workspace’s threshold, or with a waiver decided four eyes.

## Loops

Agent loops — repetition, cost runaway, high frequency, circular calls — are stopped at the runtime before their cost adds up.
