Connecting an external agent to the Model Gateway
The external-developer handout now lives in the user manuals: Connecting your agent to the UpsQuad Model Gateway (#4522). Send that page to the developer of an agent that runs outside UpsQuad; it is self-contained and carries no internal references. This page is the internal operator and evidence companion: the onboarding writes, the edge and publishing state, the dated measurements and rulings below, and, in §10, the
file:linerow behind every claim the handout makes. A change to a fact the handout states changes the handout and its §10 row in the same PR. Its refusal table and gate-chain codes are held tomodelgateway.RefusalContract()by the same guard as this page's (test/lint/model_gateway_error_contract_doc_test.go).
Operator onboarding — MG-3.9. How an agent that runs outside UpsQuad obtains a platform token and completes a governed LLM call through the Model Gateway, what it will be refused for, and where the resulting spend shows up.
Read this before you read anything else
The hostname is LIVE. Production tenant LLM traffic is still gated. Those are two different statements and the difference is the whole of this box.
https://model-gw-beta.upsquad.airesolves, serves and is sealed, verified from the public internet on 2026-09-07 — the battery is in §2 and on the flip comment. What it is released for is E2E testing traffic under the scoped waiver recorded on #2912; production tenant LLM traffic remains gated — #3120 is the named open gate, and the standing list is the query in §9, not a copy on this page. Do not read "the endpoint answers" as "the platform is carrying tenant load" (LLD #2901 §0.1).The hostname took two renames to get here and the reason is worth one line, because it constrains the next one:
model-gateway.app.upsquad.ai→model-gw-beta.app.upsquad.ai(PR #3196) →model-gw-beta.upsquad.ai(PR #3198). A two-label name needs a per-hostname certificate — Cloudflare's universal cert is*.upsquad.ai+upsquad.ai, and a TLS wildcard matches exactly one label — so with Total TLS off the cert never issued. The one-label name was covered instantly. Any future MG hostname must stay one-label underupsquad.aiunless Total TLS is turned on first.So: §2 has a devbox / loopback half and a public hostname half, and both are now live measurements rather than one measurement and one plan. Every control below names the task that delivered it, so you can re-verify it rather than re-trust this page.
1. What you are connecting, and who does what
An external agent is a first-class agents.id — team, clearance, per-agent model
enablement, budget and audit all apply to it identically to a platform agent. The only
difference is the runtime = "external" provenance stamped on its token and carried onto
every ledger and audit row (internal/auth/oauth/token_endpoint.go, MG-3.3 / T5 #2907).
There is no second registration concept for LLM access. The same
external_agent_clients row (migration 155, #1956) that authenticates a BYO agent to the
governed MCP gateway authenticates it to the Model Gateway; only the requested audience
differs (MG-3.3).
Onboarding is five writes, and they are deliberately split across three authorities:
| # | Step | Surface | Who | Delivered by |
|---|---|---|---|---|
| 1 | Register the upstream endpoint | AIGatewayEndpointRegistryService.RegisterLLMEndpoint | L4 | slice 1 T9 #2685 |
| 2 | Activate it | SetLLMEndpointStatus → approved | L5 | slice 1 T9 #2685 |
| 3 | Write the provider credential | AIGatewayEndpointCredentialService.SetLLMEndpointCredential | L4/L5 per grain | slice 1 T10 #2686 |
| 4 | Bind the endpoint to a team, then approve the binding | CreateLLMBinding → ApproveLLMBinding | L4 + the unit's Manager | slice 2 T2 #2807 |
| 5 | Register the external agent + narrow its model access | AgentService.RegisterExternalAgent | L4 | #1956 + slice 2 T4 #2810 |
Why registration and activation are different clearances. base_url is an egress
destination. Authoring one is L4; turning it into a destination the platform will dial on
your behalf is an L5 decision. This split is normative and is not relaxable to
auto-approve (proto/upsquad/aigateway/v1/endpoint.proto).
Why binding approval is L4-and-Manager rather than L5. A binding grants an
already-approved endpoint to one unit, and the authority over a unit is its Manager
(org_units.manager_member_id). A unit with no Manager fails closed — there is no
authority to approve, so nobody may.
1.1 The endpoint URL fence — read this before you pick a base_url
base_url is tenant-supplied, so it goes through the full SSRF fence
(internal/modelgateway/registry/baseurl.go, LLD #2673 §8.3). Four rules, applied in
order; every refusal message begins base_url rule N: so you can tell which one rejected
the value.
| Rule | Requirement |
|---|---|
| 1 | Absolute http/https URL with a host |
| 2 | https only. Plaintext http is permitted solely for hosts in MODEL_GATEWAY_DEV_HTTP_HOSTS, which is empty in production |
| 3 | No userinfo component, no fragment |
| 4 | No private, loopback, link-local, CGNAT or ULA destination — unconditionally. Every A/AAAA record is tested; one blocked record refuses the URL. A host that cannot be resolved is refused, because unproven must not read as allowed |
Rule 4 has no escape hatch. MODEL_GATEWAY_DEV_HTTP_HOSTS relaxes the scheme check
and nothing else; it is currently inert and setting it changes nothing
(docker-compose.dev.yml, LLD §8.3 v1.8). This is the single most common surprise when
standing up a shim — see §7.
Not closed in v1, and stated rather than assumed away: DNS rebinding between validation and dial. A hostname that resolves public here can resolve to loopback when the gateway actually dials it. Closing it needs the pinned-resolver work the MCP egress ADR owns (LLD #2673 §13).
2. Reaching the gateway
Today — beta-dev / the devbox stack · verified 2026-09-07
The gateway process runs and serves, but only on loopback. There is no edge route.
| What | Where | Verified |
|---|---|---|
| Model Gateway | http://127.0.0.1:8091 on the devbox — loopback only, no host publish | yes |
| Token endpoint | http://127.0.0.1:8083/oauth/token (the context-engine container's public mux) | yes |
| AS metadata | http://127.0.0.1:8083/.well-known/oauth-authorization-server | yes |
| JWKS | http://127.0.0.1:8083/.well-known/jwks.json | yes |
| LLM audience in force | http://context-engine:8080/llm | yes — read off the process |
Get there over SSH to the devbox (docs/runbooks/devbox-ssh-warp.md). beta-dev.app.upsquad.ai
is a different vhost — the portal — and it sits behind Cloudflare Access; an unauthenticated
request to it returns 302, not the gateway.
The process states its whole posture in one startup line. This is the artefact to read before debugging anything, and it is the source of the numbers on this page:
$ docker logs upsquad-model-gateway | grep -m1 'controls wired'
… gates="7a/clearance_floor -> 7b/agent_enablement -> 7c/model_allowlist -> 7d/budget -> 7e/guardrail"
rate_limits="org 200/s burst 400 -> team 100/s burst 200 -> agent 20/s burst 40"
dialects_relayable="[anthropic openai_compatible]" credentials_enabled=false
org_token_cap_enforced=true production_routing_released=true
agent_token_auth=true agent_token_audience=http://context-engine:8080/llm
agent_scoped_keys=refused environment=development
Read it off the process you are talking to, not off this page. The excerpt above was
taken from the devbox container on 2026-09-07 and the fields quoted are stable across
builds, but the controls= and seam_types= lists grow with each task and the devbox
image lags main. A build's own line is authoritative for that build; this one is an
example of where to look.
Two lines of that are load-bearing for anyone testing here:
credentials_enabled=false— the devbox has no credential store bound, so a call that passes every gate is refused at step 8 withcredential_store_unbound(503). That is the stack, not your configuration.agent_scoped_keys=refused— theuq_key_*deprecation window is closed on this stack (T17 #2919, merged #3068). An agent-scoped API key is refused withagent_scoped_key_demoted(403). OAuth is the only agent path here.
The public hostname — LIVE, verified 2026-09-07
| What | Where | State |
|---|---|---|
| Model Gateway | https://model-gw-beta.upsquad.ai/v1/… | live, edge installed at 2ec6629e3 (#2912) |
| Sealed by | catch-all direct_response: 404 — only /v1/ is routed | live, probed |
| Released for | E2E testing traffic, scoped waiver on #2912 | production tenant traffic still gated (#3120) |
The vhost routes only /v1/ and seals everything else. That seal is the point:
cmd/model-gateway's mux serves GET /metrics, /readyz and /healthz with no
authentication on the same listener, so a prefix: "/" route would have published all
three. Under #2985's ruling — option (b), gateway-enforced, so there is no
Cloudflare Access application in front — that seal is not defence in depth. It is the only
thing between those three handlers and the internet.
So it was probed rather than asserted. From the public internet, 2026-09-07:
POST /v1/chat/completions (no auth) -> 401 {"error":{"message":"missing or malformed
POST /v1/chat/completions (Bearer nope) -> 401 Authorization header","type":"invalid_api_key"}}
GET /metrics -> 404 ┐
GET /readyz -> 404 │ the direct_response seal
GET /healthz -> 404 │
GET / -> 404 ┘
beta-dev.app.upsquad.ai -> 302 · gw-dev.upsquad.ai -> 302 neighbours unaffected
Two details make that battery mean something, and a bare status code would not.
- The 404s carry no
x-envoy-upstream-service-timeheader; the 401 does. So the 401 is the gateway refusing through Envoy, and the four 404s never reached an upstream at all — they are Envoy'sdirect_response. A status code alone cannot tell "the seal fired" from "the gateway happened to 404". - The same
/metricsis200on loopback (curl 127.0.0.1:8091/metrics). The seal is measurably converting a live 200 into a 404, which is the claim; four 404s on their own are equally consistent with a broken config.
The 401 body is the gateway's own JSON and there is no Access interstitial, no redirect — the
identity decision is being taken by T4 #2906's middleware (cmd/model-gateway/main.go),
which is the mechanism ruling (b) selected, observed working from outside.
Rollback is three independent one-minute levers (any one of them takes the hostname
down): delete the cloudflared ingress block and restart cloudflared-upsquad; delete the
CNAME; restore /etc/envoy/backups/envoy.yaml.<TS>.bak and restart envoy. The operational
checklist lives on #2912 and is not restated here — a restatement is a copy, and
the copy is the half that goes stale.
The trap: the audience is not derived from the gateway's hostname
LLMAudienceURI(publicBaseURL) = publicBaseURL + "/llm", wherepublicBaseURLis the platform base URL (MCP_GATEWAY_PUBLIC_BASE_URL) — the same sourceTeamAudienceURIandPlatformAudienceURIuse, and distinct from both so an MCP token can never satisfy the Model Gateway (LLD §3.5, T4 #2906).Requesting
resource=https://model-gw-beta.upsquad.ai/llmdoes not work — not before the flip, not now that the hostname is live, and the two renames it took to get to this name are the argument. Derivingaudfrom the gateway's hostname was considered and refused: the minter would then need to know that hostname, and a hostname change would silently invalidate every outstanding token with a 401 that names nothing. That hypothetical happened twice in one day (#3196, #3198) and cost this contract nothing, which is the design working.Derive it, never guess it: read
issuerfrom the authorization server's metadata and append/llm. That is the same derivation the validator performs.
3. Getting a token · verified 2026-09-07
grant_type=client_credentials only. No refresh tokens — the agent re-grants. TTL is
≈10 minutes. Two client-authentication methods, both advertised in the AS metadata:
private_key_jwt (RFC 7523, preferred — you hold the private key, we store only the public
JWK) and client_secret_post (fallback, bcrypt).
The mechanics of both, the hardening posture (uniform 401 invalid_client for every
failure, per-IP and per-client rate limits, the external_token_issued /
external_token_denied audit rows) and the MCP_GATEWAY_ENABLED / MCP_GATEWAY_TENANTS
enable gate are documented once, in
Platform Token Issuer. Everything there applies unchanged.
What follows is the one thing that differs for the Model Gateway.
The resource parameter is the whole difference
BASE=http://127.0.0.1:8083 # the PLATFORM origin, not the gateway's
# Derive the LLM audience from the issuer — never hand-write it.
LLM_AUD="$(curl -s "$BASE/.well-known/oauth-authorization-server" \
| python3 -c 'import json,sys; print(json.load(sys.stdin)["issuer"].rstrip("/")+"/llm")')"
curl -s -X POST "$BASE/oauth/token" \
-d grant_type=client_credentials \
-d "client_id=$CLIENT_ID" -d "client_secret=$CLIENT_SECRET" \
-d "resource=$LLM_AUD"
# => {"access_token":"<jwt>","token_type":"Bearer","expires_in":600}
RFC 8707 resource selects which audience is minted. It is matched exact, never by
prefix — a prefix match would let …/llm-evil select the LLM audience.
resource | Result | Verified |
|---|---|---|
| absent | MCP-gateway audience (the pre-#2906 shape; every existing client is unaffected) | yes — 401 invalid_client for a bogus client, i.e. the resource was accepted |
| the exact LLM audience | Model Gateway audience | yes — 401 invalid_client, i.e. accepted |
| anything else | 400 invalid_target — "the requested resource is not served by this authorization server" | yes |
An unrecognised resource is refused rather than ignored. Ignoring it would hand back an MCP-audience token to a client that asked for the LLM one, which then fails one hop away as a 401 that can only say "wrong audience".
What was NOT executed here. The devbox has zero rows in
external_agent_clientsand zero inllm_endpoints, so no successful mint and no successful relay were performed. Every refusal shape above is a real response from the running stack; the success shapes are read from the source and the Platform Token Issuer runbook, and are marked as such.No longer true as of 2026-09-30. #3674 executed a successful mint and a successful governed relay, on loopback and through the public gateway hostname, and wrote the first four
credential_runtime = 'external'ledger rows this stack has ever had. #3677 owns correcting this page's five stale claims, of which this box is one — it is left in place rather than half-fixed here, so the correction lands once.
3.1 Where you POST from outside the network — the sixth onboarding write
The five writes in §1 are what provisions an external agent. There is a sixth thing that has to be true before any of it is usable by a machine outside this network, and until #3675 no page named it: the token issuer has to be reachable.
It was not. #3674 measured the asymmetry: the Model Gateway data plane is
Access-free and answers from the public internet, while the issuer it depends on was
404 on the gateway hostname (that vhost's seal, working as designed) and 302-to-
Cloudflare-Access everywhere else. An interactive Access login cannot be completed by a
machine, so the one step an external operator must perform themselves was the one step they
could not perform. The founder ruled option (A) on 2026-10-02: publish the issuer on a
dedicated, Access-free hostname and let the issuer's own OAuth client authentication be the
gate.
| Issuer hostname | https://auth-beta.upsquad.ai — one label under upsquad.ai, because Cloudflare's universal cert is *.upsquad.ai + the apex and a TLS wildcard matches exactly one label (the #3196/#3198 history in §2) |
| What it serves | the issuer's three paths: POST /oauth/token, GET /.well-known/oauth-authorization-server, GET /.well-known/jwks.json; and, since #4273, the team MCP gateway (/mcp/team/{unit_id} and its RFC 9728 metadata under /.well-known/oauth-protected-resource/mcp/team/), which an external agent does not need for the Model Gateway. Everything else is a 404 naming issuer_path_not_published |
| Access posture | none, by decision. The gate is client_secret_post / private_key_jwt on the token endpoint itself |
| Rate limit | /oauth/token is capped at the edge — 30 requests per 60s, 429 with x-ratelimit-remaining: 0 over it. That is ONE bucket shared by every caller, not 30 per client: it is a box CPU ceiling (each client_secret_post attempt costs a bcrypt compare), so a single source sending 30/min starves everyone else for the rest of the window. Per-source fairness is the issuer's own allowIP (1 r/s, burst 10), allowClient (1 r/s, burst 5) and failure lockout — whose key is derived per #3697 from the hop the trusted edge wrote, never the first X-Forwarded-For hop. When the edge supplies no identifiable client address (its own cf-connecting-ip-absent sentinel), every such caller shares ONE allowIP bucket and the lockout is not armed on it: a five-minute backoff on a key held in common by every unidentifiable caller would be a denial of service against that whole population. Brute force against a credential is still bounded by allowClient and the per-client_id lockout, which need no address. Right order for the #2912 waiver population; not a GA limit — the GA shape is per-descriptor (remote_address) limiting, which needs Envoy ≥ 1.34 for wildcard descriptors (this edge runs 1.31.0) and a restored client address (xff_num_trusted_hops / CF-Connecting-IP original-IP detection), or it buckets whole Cloudflare PoPs together — with #2985's rate-limit arm, not a flag flip. The edge's other rate-limited routes are the Model Gateway's /v1/ (#3708) and the team gateway's /mcp/team/ (#4273), each on its own bucket |
| Precondition | NOT retired by #3697 — read this before treating the app-side fix as the answer. CF-Connecting-IP is unforgeable only for traffic that entered through Cloudflare, and both edge listeners bind 0.0.0.0 on a host with no local firewall — so the issuer's per-IP limiter and lockout hold only while tcp/10000 and tcp/10443 are closed to the internet at the Hetzner Cloud Firewall, a fact nothing in this repository records. #3697 does not change that, and the reason is the part that gets conflated: a caller that bypasses Cloudflare and hits the edge directly still has its forged CF-Connecting-IP written into X-Forwarded-For by the edge, so it reaches the issuer from the edge's own address, which is a trusted peer — indistinguishable from the honest case, and upstream of the only position the app can verify. The edge-side control for it is #3750. What #3697 does close is the other "direct" path — a caller reaching a process port directly, bypassing the edge: its socket is PUBLIC, so it is untrusted, every forwarding header it sends is ignored, and it is bucketed on its own address (internal/httpx/clientip, CLIENT_IP_TRUSTED_PROXY_CIDRS, defaulting to loopback + RFC 1918 + RFC 4193 for this process) — plus every route where the edge does not overwrite XFF at all. Verified by the off-box nc check in the publishing procedure; keep that step |
| Released for | external-agent onboarding and E2E verification under the same scoped waiver as #2912. Production tenant LLM traffic stays gated on #3120 — publishing a mint surface does not move that gate one inch |
So the BASE=http://127.0.0.1:8083 in the snippet above becomes, from outside:
BASE=https://auth-beta.upsquad.ai # the ISSUER hostname, not the gateway's
MG=https://model-gw-beta.upsquad.ai # the gateway hostname
# Derive the LLM audience from the issuer IDENTIFIER — never hand-write it, and never
# build it from the gateway hostname (that would be `invalid_target`).
LLM_AUD="$(curl -s "$BASE/.well-known/oauth-authorization-server" \
| python3 -c 'import json,sys; print(json.load(sys.stdin)["issuer"].rstrip("/")+"/llm")')"
Known limit, stated rather than smoothed over. The AS metadata's
issueris an RFC 8414 identifier, so deriving the audience from it is correct and needs no reachability — which is why the line above works. Buttoken_endpointandjwks_uriin that document are built fromMCP_GATEWAY_PUBLIC_BASE_URL, which on this stack is the internalhttp://context-engine:8080. A standards-following client that dereferencestoken_endpointwill therefore try to reach a host it cannot. POST the published hostname directly, as above. Repointing that base URL changes theaudandissof every MCP and LLM token on the stack at once, so it is a deploy-config decision, not an edge change.
What the issuer refuses, and whether the refusal names a reason
Every row measured live by #3674 on 2026-09-30 — cited rather than re-measured, because nothing on this path changed: #3675 published the surface, it did not touch the handler. "Uniform" means byte-identical, which is deliberate: there must be no client enumeration oracle, and the secret path runs a bcrypt compare against a fixed dummy hash for an unknown client so the timing does not distinguish either.
| You sent | Status | Body | Names the reason? |
|---|---|---|---|
Unknown client_id | 401 | {"error":"invalid_client","error_description":"client authentication failed"} | To you, no — on purpose. The true reason is on the external_token_denied audit row (reason=unknown_client) |
Correct client_id, wrong secret | 401 | byte-identical to the above | audit reason=bad_client_secret |
| Revoked credential | 401 | byte-identical | audit reason=revoked_client |
private_key_jwt against a secret-registered client | 401 | byte-identical | audit reason=wrong_credential_type |
| A tenant the enable gate does not cover | 401 | byte-identical — and checked only after successful authentication, so only a valid credential learns the feature is off | audit reason=tenant_not_enabled |
resource that is not an audience this AS serves (…/llm/capability, …/mcp/platform, …/llm-evil) | 400 | {"error":"invalid_target","error_description":"the requested resource is not served by this authorization server"} | Yes. Exact match, never prefix |
grant_type other than client_credentials | 400 | {"error":"unsupported_grant_type","error_description":"only client_credentials is supported"} | Yes |
| A malformed form body | 400 | {"error":"invalid_request","error_description":"malformed form body"} | Yes |
GET instead of POST | 405 | {"error":"invalid_request","error_description":"POST required"} + Allow: POST | Yes |
| Too many attempts | 429 | {"error":"slow_down","error_description":"rate limit exceeded"} (the issuer) or Envoy's local_rate_limited with x-ratelimit-* (the edge) | Yes |
| A valid credential | 200 | {"access_token":"<jwt>","token_type":"Bearer","expires_in":600}, Cache-Control: no-store | — |
Two things this table is not about, so they are not looked for here:
- #3676 — an expired token is indistinguishable from a forged one. That is a
property of the resulting token at the gateway (
"this bearer token could not be validated"covers expiry, bad signature, wrongkidand malformed JWT alike), not of the mint. It is the once-every-ten-minutes failure for every external agent and it is tracked separately; #3675 deliberately changed nothing about it. - The
401envelope'stype-vs-codewart, which §5.1 already flags.
Proving it from off the box
export UPSQUAD_EXTERNAL_CLIENT_ID=ext_...
read -rs UPSQUAD_EXTERNAL_CLIENT_SECRET && export UPSQUAD_EXTERNAL_CLIENT_SECRET
export UPSQUAD_LEDGER_DSN='postgres://…' # arm 6; over a tunnel, or run the SQL on the box
python3 scripts/edge/probe-external-agent-mint.py --model <a model the agent is enabled for>
Six arms, all required for exit 0: AS metadata · JWKS · the seal (an unpublished path
answers 404 issuer_path_not_published, which is what proves the hostname fronts the issuer's
three paths and the team gateway's two prefixes, and not the whole platform mux) · the mint (aud equals the derived audience, runtime
is external) · the governed call (200 + x-upsquad-usage-event-id) · the ledger
(credential_runtime = 'external' on that event id). Credentials come from the environment
only — there is no flag for them, because a flag lands in shell history and in ps. Missing
credentials, a missing DSN or an unreachable hostname each exit 2 naming what was
missing; a run that could not check has never verified anything.
Publishing the hostname is four human actions in order — an off-box nc check that
tcp/10000 and tcp/10443 are closed (the precondition above), a Cloudflare DNS record, a
tunnel ingress rule in ~/.cloudflared/upsquad/config.yml, and
sudo scripts/edge/install-envoy-config.sh. Merging the Envoy change does not apply it.
The exact steps, the probe's before/after counts and the rollback live with the config, in
infra/edge/README.md — one copy, next to the thing it changes.
4. Making the call
# PUBLIC (live, verified 2026-09-07): MG=https://model-gw-beta.upsquad.ai
# DEVBOX LOOPBACK (unsealed — serves /metrics, /readyz, /healthz):
# MG=http://127.0.0.1:8091
curl -s -X POST "$MG/v1/chat/completions" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H 'Content-Type: application/json' \
-d '{"model":"<a model id on an endpoint your team is bound to>",
"messages":[{"role":"user","content":"hello"}]}'
Things worth knowing before the first call:
- Everything under
/v1/is relayed. The gateway does not enumerate upstream routes — a registeredbase_urlmay carry an Azure-style deployment prefix and your path is joined onto it. So/v1/embeddings,/v1/rerankand provider-native paths reach the upstream; a fixed route table would break the endpoints the registry exists to support. - Your credentials are stripped.
Authorization,x-api-key,Api-Key,Cookieand everyX-Upsquad-*header are removed before forward, and the tenant's credential is injected:Authorization: Bearer …foropenai_compatible,x-api-key: …foranthropic(internal/modelgateway/transport/headers.go). - Dialect is inferred from the request shape, not from a route table (T6 #2908). A body
with
system/top_k/thinkingnamesanthropic; one withfrequency_penalty/response_format/seednamesopenai_compatible. A request naming two is refuseddialect_ambiguous(400). A request naming none is not refused — it falls to pass-through, which is what every earlier build did with it. - Cross-dialect translation is opt-in twice. The team's binding must carry
allow_translation, and the request must sendX-Upsquad-Translate: 1. Never inferred: PRD §11 forbids silent translation and inference is exactly that silence. See §4.1 for what happens when it is on. - The credential grain walk is most-specific-wins with no fallback where the binding
forbids one. If the binding sets
credential_mode = per_agentwithrequire_per_agent, there is deliberately no fallback to the team or org key (MG-2.2); the refusal isagent_credential_required. - HTTP/1.1 only.
:8091does not terminate h2c. An h2-terminating proxy in front is fine.
4.1 Translation: anthropic ⇄ openai_compatible
Both directions, request and response, shipped by T7 #2909 (merged 2026-09-07). Two dialects only — no third, no partial-fidelity mode, and no fallback to pass-through on a translation failure: a failure is a refusal.
The rule that makes it honest: a request feature with no cross-dialect analogue is
REFUSED, never dropped. A dropped tool_choice, thinking block or response_format
produces a successful call with different semantics, which is the worst failure shape
available because you are never told. The lossy set is derived from the dialect structs
rather than hand-listed, so a field a vendor ships tomorrow refuses by default.
| What happened | Wire |
|---|---|
| A field in your request has no analogue in the endpoint's dialect | 501 translation_unavailable |
| This build cannot rewrite this dialect pair's responses | 501 translation_unavailable — refused before the upstream is dialled, while a refusal is still free |
| Translation was granted and requested but your dialect was undetermined | 501 translation_unavailable |
| Your body does not parse as a request of your own dialect | 400 invalid_request |
The last row is the one to internalise. 501 and 400 are split deliberately: "fix your
body" and "wait for a deploy" are different remedies, and reporting the first as the
second sends you to ask for a release you do not need.
All four translation_unavailable arms are a governance allow. Every gate said yes and
the facade could not serve the request, so the ledger records an allow and no audit hop
fires — a hash-chained record of a governance denial that never happened is worse than no
record. 501 also carries no class, and that is a ruling rather than an omission:
unavailable would promise a retry may succeed, which is false until a different binary
ships.
501is not only translation, since 2026-09-08.unsupported_dialectis served there too — see §9. Both are "this build cannot serve it", which is what the status and the absentclasssay. They differ in the ledger: the four arms above are always an allow;unsupported_dialectis adenywhen the resolver's credential gate refuses it and anallowwhen the handler does. So do not read501as "never in the audit chain" — readerror.code.
Mid-stream is different, and you should know before you see it. Once the first frame is out the status line is committed, so a response frame with no analogue cannot be refused. It is reported in band — an error event in your own dialect — and the rewriter stops emitting. Dropping it or relaying it untranslated are both worse.
And know what you will not see: the ledger cannot tell you it happened. A mid-stream
translation_unavailable writes a usage row indistinguishable from a complete call on
every ledger-facing field — status_code, termination_reason, client_gone and
inspected are all identical to the clean stream (#3134, measured; open,
prod-enablement). You are also metered for the upstream's full token count, correctly,
because the observer sees upstream frames by design. So the in-band error event is the
only record that half your answer was rewritten and half was not — if you are
reconciling spend against completions, do not expect the ledger to flag these.
Translation is not a metering variable. The usage observer is fed the upstream frame while you are fed the rewritten one, from the same line, so a translated call meters byte-identically to the same upstream stream on the pass-through path.
5. Why you were refused — the vocabulary
Every refusal carries a stable machine code. Key on error.code; never on the message.
{"error":{"code":"budget_exceeded_team","message":"…","gate":"7d/budget","class":"quota"}}
code— the stable identity. It is also what lands inllm_usage_events.deciding_gate, so it is the string you group by in a dashboard.gate— the pipeline step that refused, e.g.7d/budget. Omitted when no pipeline step refused; absence is meaningful.class— what the status means for your retry logic. Omitted at a status this build has ruled unclassifiable (500,501); absence there is a declared ruling, not a gap. Both mean the same thing — this build cannot serve it, and no retry succeeds until a different binary is deployed — and there is deliberately no class that says so (#3245 asks whether there should be).
Four classes, and the difference is load-bearing:
| Class | Statuses | What it tells your client |
|---|---|---|
quota | 402, 429 | You exhausted a cap. Retrying now cannot succeed; retrying after the window rolls, or after the cap is raised, can |
policy | 401, 403 | A governance decision refused this call. Retrying cannot succeed at any time — someone has to change a configuration |
request | 400, 404, 413 | The request is the problem |
unavailable | 502, 503 | The control could not be evaluated. You did nothing wrong and a retry may well succeed |
403 and 503 must never be collapsed. A 403 sends an operator to the policy; a 503 sends
them to the dependency. Every gate's fail-closed arm therefore has its own code
(clearance_unavailable, not insufficient_clearance) — an operator grouping by the wrong
one looks at an agent's clearance, finds it correct, and has nowhere to go.
5.1 Authentication failures are a different envelope — and this is a real wart
A refusal from the auth middleware, i.e. before the resolver runs, does not carry
code, gate or class. It carries type:
$ curl -si -XPOST http://127.0.0.1:8091/v1/chat/completions \
-H 'Authorization: Bearer notatoken' -d '{}' # verified 2026-09-07
HTTP/1.1 401 Unauthorized
{"error":{"message":"this bearer token could not be validated","type":"invalid_token"}}
$ … -H 'Authorization: Bearer uq_key_nope' # verified 2026-09-07
{"error":{"message":"invalid or expired API key","type":"invalid_api_key"}}
$ … with no Authorization header at all # verified 2026-09-07
{"error":{"message":"missing or malformed Authorization header","type":"invalid_api_key"}}
The two 401 bodies share a shape deliberately — a client that must parse two envelopes will
parse one of them wrong — and type is the field you switch on. The contract's
unauthenticated code (401) is the resolver's arm: it is reached only when the
middleware admitted a request and no caller resolved, which is a wiring fault.
Note the dispatch rule: a bearer value not beginning uq_key_ goes to the OAuth
path; anything else — including no header at all — goes to the API-key path. That is why a
missing header reports invalid_api_key.
MG-3.6 asks that authentication failure be machine-distinguishable from the other families,
and it is (type vs code, and the two are never both present). But it is
distinguishable by a different field, which is a rougher edge than the rest of the
contract and is flagged rather than papered over.
5.2 The full refusal contract
One declaration (internal/modelgateway/errorcontract.go), one status per identity, keyed
by reason — denial and refuse do not take a status at all, they look it up. The table
below is generated from that declaration and pinned to it by
test/lint/model_gateway_error_contract_doc_test.go: adding a refusal without adding a row
here fails the build, and a row here naming a code the gateway cannot emit fails it too.
ledger is what llm_usage_events.governance_outcome records and, inseparably, whether
the hash-chained agent_audit_log hop fires — a deny row and an audit row are written
together or not at all. allow means governance permitted the call and something after
it failed; recording those as denials would put a decision that was never taken into a
tamper-evident chain.
| Code | Status | Class | Ledger | Retry-After |
|---|---|---|---|---|
agent_ambiguous | 403 | policy | deny | — |
agent_credential_required | 403 | policy | deny | — |
agent_enablement_malformed | 403 | policy | deny | — |
agent_enablement_unavailable | 503 | unavailable | deny | — |
agent_not_found | 403 | policy | deny | — |
agent_scoped_key_demoted | 403 | policy | deny | — |
agent_unresolved | 403 | policy | deny | — |
ambiguous_caller_identity | 401 | policy | deny | — |
auth_mode_unknown | 403 | policy | deny | — |
binding_kind_unknown | 403 | policy | deny | — |
binding_unit_corrupt | 403 | policy | deny | — |
binding_unit_unresolved | 403 | policy | deny | — |
binding_withheld | 403 | policy | deny | — |
budget_exceeded_agent | 402 | quota | deny | — |
budget_exceeded_member | 402 | quota | deny | — |
budget_exceeded_org | 402 | quota | deny | — |
budget_exceeded_team | 402 | quota | deny | — |
budget_unavailable | 503 | unavailable | deny | — |
capability_unconfigured | 403 | policy | deny | — |
clearance_unavailable | 503 | unavailable | deny | — |
content_filter | 400 | request | deny | — |
credential_missing | 403 | policy | deny | — |
credential_store_unbound | 503 | unavailable | deny | — |
dialect_ambiguous | 400 | request | deny | — |
dialect_mismatch | 400 | request | deny | — |
egress_destination_refused | 403 | policy | allow | — |
endpoint_disabled | 403 | policy | deny | — |
endpoint_not_enabled_for_agent | 403 | policy | deny | — |
endpoint_pending_approval | 403 | policy | deny | — |
endpoint_unmetered | 403 | policy | deny | — |
governance_backend_unavailable | 503 | unavailable | deny | — |
guardrail_input_unreadable | 400 | request | deny | — |
insufficient_clearance | 403 | policy | deny | — |
invalid_request | 400 | request | deny or allow | — |
model_allowlist_unavailable | 503 | unavailable | deny | — |
model_not_enabled_for_agent | 403 | policy | deny | — |
model_not_in_team_allowlist | 403 | policy | deny | — |
model_not_served_by_endpoint | 403 | policy | deny | — |
model_required | 400 | request | deny | — |
no_approved_binding | 403 | policy | deny | — |
no_matching_endpoint | 404 | request | deny | — |
rate_limit_unavailable | 503 | unavailable | deny | — |
rate_limited_agent | 429 | quota | deny | required |
rate_limited_member | 429 | quota | deny | required |
rate_limited_org | 429 | quota | deny | required |
rate_limited_team | 429 | quota | deny | required |
refusal_uncontracted | 503 | unavailable | deny | — |
request_body_too_large | 413 | request | deny or allow | — |
route_unresolved | 502 | unavailable | unassessed | — |
service_agent_budget_required | 403 | policy | deny | — |
service_governance_unavailable | 503 | unavailable | deny | — |
service_policy_required | 403 | policy | deny | — |
streaming_unsupported | 500 | (no class — declared omission) | allow | — |
subscriber_account_mismatch | 403 | policy | deny | — |
subscriber_account_unreadable | 400 | request | deny | — |
subscriber_consent_required | 403 | policy | deny | — |
subscriber_identity_expired | 401 | policy | deny | — |
subscriber_revoked | 403 | policy | deny | — |
subscriber_suspended | 403 | policy | deny | — |
subscriber_team_not_granted | 403 | policy | deny | — |
subscription_mode_disabled | 403 | policy | deny | — |
subscription_shape_invalid | 401 | policy | deny | — |
surface_not_allowed | 404 | request | deny | — |
translate_opt_in_malformed | 400 | request | deny | — |
translation_not_permitted | 403 | policy | deny | — |
translation_unavailable | 501 | (no class — declared omission) | allow | — |
unauthenticated | 401 | policy | deny | — |
unsupported_dialect | 501 | (no class — declared omission) | deny or allow | — |
upstream_unavailable | 502 | unavailable | allow | — |
5.3 The gate chain, in operator language
The order below is enforcement order, read back from the declaration at startup
(gates= on the wired line). Earlier gates win, so the code you get names the first
thing that was wrong, not the only one.
| Step | Refused because | The codes you will see | Where you fix it |
|---|---|---|---|
| 3 identity | No caller resolved, or an agent-scoped uq_key_* was presented after the window closed | unauthenticated, agent_scoped_key_demoted | Re-grant an OAuth token (§3) |
| 4 rate limit | Per-identity token bucket, before the body is read | rate_limited_{org,team,agent}, rate_limit_unavailable | §6.2 |
| 5 request / routing / binding | No model in the body; no approved endpoint serves it; the endpoint is not bound to your team, is pending, or is withheld | model_required, invalid_request, request_body_too_large, no_matching_endpoint, endpoint_disabled, endpoint_pending_approval, no_approved_binding, binding_withheld, binding_unit_unresolved, binding_kind_unknown | Steps 1–4 of §1 |
| 6 mode | The request names two dialects, or names one the endpoint does not speak, or opted into translation the binding does not grant | dialect_ambiguous, dialect_mismatch, translation_not_permitted, translate_opt_in_malformed | The request, or the binding's allow_translation |
| 6 metering | The endpoint has produced results the gateway could not meter, and a cap is in force | endpoint_unmetered | Fix metering, or take the L5 allow_unmetered acknowledgement — which turns off a spend control |
| 7a clearance floor | The agent's clearance is below max(endpoint, binding).min_clearance. Resolved server-side per call, never from a token claim, so an L5 demotion takes effect on the next call | insufficient_clearance, agent_not_found, clearance_unavailable | The agent's clearance, or the floor |
| 7b agent enablement | This agent's narrow-only selection does not include this endpoint or model | endpoint_not_enabled_for_agent, model_not_enabled_for_agent, agent_unresolved, agent_ambiguous, agent_enablement_malformed, agent_enablement_unavailable | The agent's Models selection (step 5) |
| 7c model allowlist | The model is not in the team's allowlist, or the endpoint does not serve it | model_not_in_team_allowlist, model_not_served_by_endpoint, binding_unit_corrupt, model_allowlist_unavailable | The binding's model list |
| 7d budget | A cap is exhausted at org, team or agent | budget_exceeded_{org,team,agent}, budget_unavailable | §6.1 |
| 7e guardrail | Content refused, or the body could not be read as a request | content_filter, guardrail_input_unreadable | The prompt |
| 8 credential | No credential at any grain this binding permits; an empty one; a store that could not be read tenant-bound | credential_missing, agent_credential_required, credential_store_unbound, unsupported_dialect | Step 3 of §1 |
agent_unresolved vs agent_ambiguous is worth a sentence, because it looks like an
internal detail and is not. member_api_keys.scoped_agent_id is TEXT with no foreign key
and its referent is genuinely ambiguous between agents(id) and agent_configurations(id).
The reader matches both and reports a value resolving two different rows as its own
refusal rather than picking one — because on a narrowing control, picking wrong misses in
the fail-open direction.
6. Quotas — what is actually enforced
6.1 Budget — 402
Three grains — org, team, agent — are all loaded and composed min-of: the
most restrictive wins, and the refusal names the deciding grain in its code
(budget_exceeded_team). An agent with a low cap cannot borrow team headroom; that is
the correct default, since a runaway agent is the failure mode caps exist to bound.
The check is pre-call. The call in flight when the cap was reached completes; the next one is refused. There is no mid-stream kill — that would bill you for a truncated answer to a question you did ask.
State the cap precisely. As of T14 (#2916, migration 223, closing #2861) and
the window unification that closed #2928 (#3126 merged 2026-09-07T10:24:59Z,
#3119 merged 13:55:22Z, #2928 closed 13:55:24Z, migration 235):
| Claim | True? |
|---|---|
| A monthly, per-period token cap whose window advances without human action, at all three grains | Yes. The window is derived — billing_period_bounds(billing_anchor_at, now()) inside the budget statement — so period_start <= now() < period_end holds by construction. Nothing has to advance it, and no background job's outage can disable it. Since #2928 there is one WITH period AS (…) CTE and org_period_start / team_period_start / agent_period_start all read it, so a min() across grains compares intervals that actually coincide |
| A money cap | No. The unit is tokens off llm_usage_events. cost_usd is deliberately NULLable (unknown is not $0), so a dollar cap is not computable from this ledger. The constant is named OrgTokenCapEnforced for exactly this reason (#3062 ruling (c)) |
| The same guarantee at team and agent grain | Yes, since 2026-09-07 — this row said "No" until that afternoon. Product ruled RECUR on 2026-09-03 and internal/modelgateway/budgetsource.go now opens the section with "THE RESIDUAL IS CLOSED: ALL THREE GRAINS NOW SHARE ONE DERIVED WINDOW (#2928)". The team and agent window predicates were deleted because a total derivation made them inert, and migration 235 marks the stored period_start / period_end columns vestigial and NULLABLE — so a surviving reader of the stored window now drops every cap, which is why they went rather than being left as a live-looking check |
The hazard this table used to name, now closed. It was worse than "no cap": under min-of composition a lapsed team row did not withdraw an opinion, it promoted the team to the org cap — the control widened, in the fail-open direction, on a timer, with nothing denying, so the refusal that names the deciding grain never fired precisely when a cap had stopped existing, and alert_pct / throttle_pct hung off the same row so the warning lapsed with the cap it was meant to warn about. Recorded rather than deleted: if you are reading a ledger from before 2026-09-07, this is what you are looking at | |
| "This tenant's spend is capped", as a composition | Still not writable — but settled, not open. #3062 closed 2026-09-07 under ruling (c): the constant was renamed to stop over-claiming, rather than the composition being completed. So the composed sentence is as false as it was; what changed is that it is now a recorded naming decision instead of an unresolved concern. Do not write it |
The AT LEAST wording on a 402 is not hedging. A window containing calls whose
metering_confidence is not complete has a used that is a floor, not a total, so
the refusal says so and counts the blind calls:
AT LEAST 812340 of 1000000 tokens used — 3 call(s) in this window carry no complete metering, so the true figure is higher
A precise number would send you looking for a billing bug. The same floor applies in the admitting direction, where it costs money rather than denying — that branch logs the incompleteness rather than staying silent (#3059).
Also enforced since #3118: all four token classes are summed (input, output,
cache_creation, cache_read). Two of them were invisible to the cap before.
6.2 Rate limits — 429
Per-identity token buckets in Redis, evaluated at step 4, before the body is read — a limiter running after a 32 MiB read would already have paid the cost it exists to bound.
| Class | Sustained | Burst | Applies to |
|---|---|---|---|
| org | 200/s | 400 | every caller — this class is total |
| team | 100/s | 200 | unit-scoped credentials |
| agent | 20/s | 40 | agent-scoped credentials |
Verified off the running process (rate_limits= on the wired line). There is no
environment variable that changes these — deliberately, so the running value and the
stated reason cannot drift. They are org × 1, × ½, × ⅒, with burst × 2, and the
ratios are containment statements: a team may not out-consume its org, an agent may not
out-consume its team.
The team class is deliberately oversubscribed — three teams at org/2 sum to 150% of the org rate. The per-team bucket is a blast-radius bound on one team, not a share of a fixed pie; the org bucket is what bounds the total. Partitioning instead would strand capacity whenever a team is idle and would silently change every team's limit the day a new team is created.
A caller the org class cannot bucket — no org_id — is refused, not admitted. A
non-empty bucket set is not the same as a bounded caller.
Two 429 properties your client can rely on:
Retry-Afteris always present on a 429 and never on any other refusal. That is the contract, not a convention:RetryAfterRequiredis declared per identity, and a header omitted at zero seconds would otherwise be indistinguishable from one never set.rate_limit_unavailable(503) is notrate_limited_*(429). A limit that could not be read is not a limit that was exceeded. There is a grace window (MODEL_GATEWAY_RATE_LIMIT_GRACE_SECONDS, default 5m) during which a per-replica fallback stands in for the shared bucket; after it, step 4 fails closed.
7. Bedrock — the interim workaround
Native AWS Bedrock is not supported and is not coming in v1 (PRD #2644 D3). This
is not a gap in the routing layer; SigV4 request signing is a different auth axis and does
not fit the registry's url + type + key model. Confirmed in the tree: git grep -i 'bedrock\|sigv4' returns no implementation — only ADR prose and one test fixture whose
model id happens to contain the word.
The gateway speaks exactly two dialects, openai_compatible and anthropic
(dialects_relayable on the wired line). So the interim path is to put something in front
of Bedrock that speaks one of them.
Two options. Both are the tenant's infrastructure, not UpsQuad's.
Option A — AWS's own OpenAI-compatible endpoint
If your account has an OpenAI-compatible chat-completions endpoint fronting Bedrock, register it directly:
dialect:openai_compatiblebase_url: the endpoint's publichttpsorigin- credential: the endpoint's bearer key, written through
SetLLMEndpointCredential
The gateway sends Authorization: Bearer <secret> and joins your request path onto
base_url, so a deployment prefix in the URL works.
Option B — a LiteLLM shim you host
Run LiteLLM configured with a Bedrock model, exposing /v1/chat/completions (or
/v1/messages for the anthropic dialect), and register the shim's URL as the
endpoint. LiteLLM holds the AWS credentials; UpsQuad holds a static bearer for the shim.
The constraint that catches everyone: the shim must be publicly resolvable over
https. SSRF fence rule 4 refuses any host resolving into the private/loopback/ link-local/CGNAT/ULA set unconditionally, andMODEL_GATEWAY_DEV_HTTP_HOSTS— the allowlist that would exempt one — relaxes only the scheme check and is currently inert. A shim on10.0.0.0/8, on a Kubernetes service name, or onlocalhostcannot be registered, and this is exactly why the devbox stack cannot relay to its ownupsquad-litellmcontainer.
infra/litellm/config.yaml is a working LiteLLM proxy config in this repo, but it is a
dev-only Anthropic facade over local Ollama for the Claude-SDK worker — it is not a
Bedrock configuration and it is not reachable by the Model Gateway for the reason above.
Read it as a syntax reference only.
What you give up either way, and it should be said before someone finds out from a
bill: metering fidelity is whatever the shim reports. If it does not return a usage block
the extractor recognises, calls land as metering_confidence not-complete, and every
budget number over that window becomes an AT LEAST floor (§6.1) —
including the one that decides whether to admit the next call.
8. Where the spend shows up
Tenant-facing, per org/team/agent/model — AIGatewayUsageService, read-only by
construction (T16 #2918):
| RPC | Returns |
|---|---|
QueryUsage | Usage at org / team / agent / model / endpoint / day / session grain, clearance-gated and paginated |
ExportUsage | The same as CSV |
QueryLatency | Percentiles that state the population they were computed over (p99_of_measured_ms) |
GetBudgetBurndown | The cap in force at one grain, and a projection that refuses or ranges whenever a cost is unknown |
unknown cost is a separate bucket, never summed into the total and never dropped
(OQ-3). A total that silently absorbed unpriced calls would be confidently wrong in the
flattering direction on exactly the largest ones.
The five dashboards (MG-3.7) are upsquad-client work and are all delivered as of
2026-09-07: Spend + CSV/API export (client#840, merged 2026-09-05T16:24:21Z); Agent
Economics, Reliability, Governance and Budget burndown (client#842, merged
2026-09-07T13:39:49Z). Both frontends carry every surface (two-frontend parity). This
paragraph said the second four were "open on client#842" until that PR merged — client#842
is a merged PR, not an open issue, and MG-3.7 is complete.
Platform-operator view — Grafana, Model Gateway — Overview
(deployments/observability/grafana/model-gateway-overview.json). Panels as of #3011/#3008:
concurrency and in-flight upstream requests (the HPA's own input), scrape health, peak
concurrency per replica, rate-limit refusals by identity class, rate-limit decisions by
arm, step-4 degradation, budget refusals by deciding grain, budget decisions by arm,
gate 7d failing closed, and unclassified gate-7d decisions.
Two panels exist because absence had to become visible: "gate 7d failing closed" separates a control that denied from one that could not be evaluated, and "unclassified" catches a future branch that classifies nothing rather than letting it go silent.
Denials vs allows are recorded differently, on purpose. A denial writes a
llm_usage_events deny row and a hash-chained agent_audit_log hop. An allowed call
writes a ledger row and no audit hop — chaining every served call would put an
advisory-lock round trip on the hot path (PRD v1.2 amendment to MG-2.11).
9. What this page does not claim
-
The hostname being live is not production traffic. The flip released E2E testing traffic under the scoped waiver on #2912; production tenant LLM traffic is still gated. #2912 itself stays open — its closure is the founder's call given the waiver scope, not a consequence of the flip.
Do not read a gate list off this page. The register moves faster than a runbook can. On the afternoon of 2026-09-07 three things this page asserted as open closed inside eighteen minutes —
client#842merged13:39:49Z, #2928 closed13:55:24Z, #3062 closed13:57:24Z. The pre-promotion checklist is a query:gh api "search/issues?q=repo:upsquad-ai/upsquad-core+label:prod-enablement+is:open&per_page=100" \--jq '.items[] | "#\(.number) \(.title)"'Filter it to the offering being shipped, per the Approval Protocol in
CLAUDE.md. At the time of writing, #3120 is the gate whose title names this switch directly.And re-sweep the whole page, not the row you came for. Every state claim here carries a date for exactly this reason; the ones that rot are never the ones you are looking at:
grep -oE '#[0-9]{3,5}' docs/runbooks/model-gateway-external-agent-onboarding.md | tr -d '#' | sort -un |while read -r n; dogh api repos/upsquad-ai/upsquad-core/issues/"$n" \--jq '"#\(.number) [\(.state)] closed=\(.closed_at // "-") \(.title[0:60])"'doneCross-repo references (
client#…) need the same check against their repo — and check whether the number is an issue or a merged PR, because "open on client#842" was wrong in both halves. -
The
:8091HA posture is not established. #2646 remains an openprod-enablementgate that slice 3 does not close. One devbox host edge, one process. -
GKE ingress for the Model Gateway is undesigned.
infra/edge/envoy.yamlgoverns the devbox host edge only; the cluster Service isClusterIPwith no Ingress, no HTTPRoute, no LoadBalancer and no ArgoCD Application, and an exposure check asserts that stays true. #2987 — deliberately notprod-enablement, because it gates no switch that exists.One thing that check no longer catches, recorded once so nobody rediscovers it as a surprise.
scripts/check-model-gateway-exposure.pyfinds edge references through threeEDGE_MATCHERSarms: the tokenmodel-gateway, the tokenmodel_gateway, and a digit-bounded8091. The public hostname used to satisfy the first arm by beingmodel-gateway.app.upsquad.ai; after the #3196 / #3198 renames it ismodel-gw-beta.upsquad.ai, which contains nomodel-gatewaytoken. Coverage is unchanged in practice — the vhost'sname:field is stillmodel-gateway, and anything that actually reaches the gateway must carrymodel_gateway(the cluster) or8091(the port), so both other arms still fire and the census is unchanged at 4 objects. But the hostname string is no longer load-bearing for that arm. Do not prune an arm on the reasoning that "the hostname covers it". -
The edge authn/authz/rate-limit baseline is #2985, ruled and re-scoped. Ruled option (b), gateway-enforced (founder, 2026-09-07) — that released the flip, and #2985 no longer gates it. It stays open scoped to first external-tenant onboarding. The consequence to carry: under (b) there is no Access application in front of the hostname, so the
direct_responseseal in §2 is the sole control on the unauthenticated ops surface. Any future edit to that vhost is a security change, not a routing change. -
unsupported_dialectmoved 502 → 501, and if you wrote a client before 2026-09-08 this is a breaking change to one branch. Founder ruling on the #2981 rider, 2026-09-08. It was served at 502, which the taxonomy maps tounavailable— "the control could not be evaluated; a retry may well succeed" — and for this refusal every control that ran said yes and no retry can succeed until a binary with a transport for the dialect ships. That is verbatim the sentence the 501 ruling declares false for the identical fact, so the code was telling clients to back off and retry a permanent condition.What changes on the wire: the status, and the
classfield, which is now absent — 501 is a declared-unclassified status, so the refusal carries no class rather than a wrong one. Nothing else. Thecodeis unchanged, so a client keying onerror.code(which is what §5 asks you to do) needs no change at all; a client branching on the status or onclass == "unavailable"does.Why now rather than never, or later. The only public-hostname traffic today is E2E waiver traffic, so this costs its minimum; after first external-tenant onboarding it would have needed deprecation ceremony for a code nobody could have been relying on correctly.
Its ledger outcome did not move and is still per-producer: refused pre-admission at gate
8/credentialit is adenyand is in the audit chain; refused post-admission by the handler it is anallowwith no hop. The class is the same at both and the disposition is not, which is not an inconsistency — the class answers "could a retry ever succeed" (no, either way) and the disposition answers "did a gate decide". Two questions.With that ruled, no contract disposition is contested any longer; the register that tracked them is deleted rather than left empty. One question it raises is open and tracked at #3245: the 501 ruling declines to invent a fifth
classand stated it would be revisited if a second refusal in this shape appeared. Three now have. That is a vocabulary question, not a defect — the current state is a declared omission and is honest on the wire — but it has a closing window for the same reason this change had one. -
The ledger half of #2981 is fixed, and it changes what you will find in the chain. Option B of the #2981 fork (founder ruling, 2026-09-07 — not to be confused with #2985's option (b) above, ruled the same day) moved the disposition from the refusal reason onto the producer. If you audited this gateway before that landed, two things are now different and both are corrections:
route_unresolvedused to writegovernance_outcome='deny', adeciding_gateand a hash-chained audit hop for a request where no gate ever decided. It now writes a row withgovernance_outcomeNULL — the ledger's spelling for "no gate chain evaluated this call" — and no audit hop.invalid_request,request_body_too_largeandunsupported_dialectare each emitted by two producers. Refused pre-admission by the pipeline they are denials and are in the chain, as before. Refused after every gate said yes they now record an allow and produce no hop, because governance permitted those calls. Thedeny or allowin the table above is that fact, per request.
What this costs you as an auditor, stated plainly: the refusal reason for a non-deny row is not in
deciding_gate— migration 207 permits that column only beside a deny — so it is counted onmodel_gateway_refusal_ledger_disposition_total{reason,disposition}and nowhere durable. That metric has a retention horizon and is not tamper-evident. The trade was made deliberately: what the chain now contains is only decisions that happened. Expect a one-off step down ingovernance_assessed_deny_eventsat the deploy and a matching step up ingovernance_unassessed_events; alert rules keyed on either need the note. That step is now observable rather than hypothetical: the flip released E2E testing traffic (first bullet), so these counters have a live population to move.The one series to query if you query nothing else here:
model_gateway_denial_audit_unrecorded_total{org_id}(#3430). Any non-zero value means a denial's chainedagent_audit_logappend failed: its audit record is not confirmed. This does not prove the separatellm_usage_eventswrite succeeded; inspect both write outcomes. It is initialised to zero for every org that reaches the audit hop. An absent series can mean the org never reached that hop, or that the counter is not wired; absence is not evidence of a complete chain. It is disjoint frommodel_gateway_denial_record_shed_total{org_id,family}, which counts records never attempted because the #3061 admission bound was saturated; summing the two is meaningless, alerting on either being non-zero is not. The gateway does not refuse traffic when this moves — the call it would refuse was already denied and its response already delivered — so inspect the corresponding ERROR log as well. This counter observes returned audit errors, not every possible missing row: a panicking batch leader unwinds past its own increment (followers receive an error), and a lost commit acknowledgement can leave an uncertain outcome. Neither zero nor absence proves chain completeness.No alert rule reads either series yet (#3505), which is why this says query rather than alert on: today it is a manual check an operator has to remember, and a counter nobody scrapes is the log line nobody greps. Replace this paragraph with the rule names when #3505 lands.
-
Nothing on this page was executed end-to-end through a successful governed call, for the reason in §3: the devbox has no registered external client and no registered endpoint. The flip did not change this — a live hostname that answers
401is still a refusal path, and a 401 from the internet is not evidence of a completed governed call. Refusal shapes, the token endpoint's resource contract, the wired-controls line, the seal battery and the DNS/TLS state are all live measurements taken 2026-09-07; success shapes are read from source.
10. Evidence for the external-agent handout
Every claim in the handout maps to a row
below. The handout itself carries no file:line (the manuals rule, enforced by
docs-site/scripts/check-manuals.py), so this table is where its claims are
re-verified. Lines were derived on main at 1ee9c97c9 and every pin re-checked at e3a240d78 (2026-11-17); a row whose line has
moved is re-derived, not trusted. Live rows were measured from the public internet on
2026-11-17 and are the only ones that are not read from source.
10.1 Claim → evidence
| Handout § | Claim | Evidence |
|---|---|---|
| 1, 3.2 | An external agent is a first-class agent (team, clearance, enablement, budget) and one credential binds one agent | proto/upsquad/agent/v1/agent.proto:163 (RegisterExternalAgent), :1293-1320 (request: team, clearance, public_key_jwk); internal/auth/oauth/token_endpoint.go:425-431 (mint from the stored org/agent/unit binding) |
| 1, 7 | Usage rows are marked as coming from an external agent | internal/auth/oauth/token_endpoint.go:429 (runtime: "external"); internal/modelgateway/agentauth/middleware.go:308-321 (carried onto the identity); internal/modelgateway/auditchain.go:181 (credential_runtime); internal/modelgateway/recorder.go:357 |
| 1, 5.2 | The provider never sees the agent's token; the tenant's key is attached | internal/modelgateway/transport/headers.go:30 (caller auth headers stripped), :203-216 (tenant credential injected) |
| 2 | Hostnames; gateway Access-free on the public internet | infra/edge/envoy.yaml:508 (model-gw-beta.upsquad.ai), :570 (/v1/ only), :618 (seal); :797 (auth-beta.upsquad.ai); live: unauthenticated POST /v1/chat/completions → 401 invalid_api_key with x-envoy-upstream-service-time |
| 2 | Only two dialects; no Bedrock/SigV4 | internal/modelgateway/transport/types.go:17, :21; §7 of this page (git grep -i 'bedrock|sigv4' finds no implementation) |
| 2, 5.1 | Everything under /v1/ is relayed; no route table | cmd/model-gateway/main.go:540; internal/modelgateway/passthrough/handler.go:611 (path := r.URL.Path); internal/modelgateway/transport/http.go:260 (JoinPath) |
| 2, 4.1, 4.3 | client_credentials only; private_key_jwt and client_secret_post | internal/auth/oauth/issuer_routes.go:121-123; internal/auth/oauth/token_endpoint.go:221 (unsupported grant), :252-261 (method dispatch) |
| 2, 6.5 | Edge limits: 120/min on /v1/, 30/min on /oauth/token, each one shared bucket | infra/edge/envoy.yaml:581-584 (model_gateway_v1, 120/60s), :874-881 (issuer_token, 30/60s) |
| 2, 5.6 | 150 s whole-call cap on this service | docker-compose.dev.yml:2315 (drain 180); internal/modelgateway/config.go:274 (margin 30), :801 (cap = drain − margin); internal/modelgateway/passthrough/handler.go:644 |
| 3.1 | Register L4, activate L5; bind = L4 + the unit's Manager | proto/upsquad/aigateway/v1/endpoint.proto:12-18, :58; proto/upsquad/aigateway/v1/endpoint_binding.proto:104-109 |
| 3.1, 4.6 | Tenant must be enabled; otherwise uniform 401 | internal/auth/oauth/token_endpoint.go:420-422 (checked after authentication), :498-503; cmd/context-engine/main.go:2558-2559 |
| 3.2 | client_id = ext_ + 32 hex; secret uxe_… shown once; private JWK refused | internal/agent/external_credential.go:34, :37, :90; proto/upsquad/agent/v1/agent.proto:1317, :1332 |
| 3.2 | Rotation keeps client_id; revocation exists | proto/upsquad/agent/v1/agent.proto:168, :173 |
| 4.1 | Paths; metadata; JWKS cache | internal/auth/oauth/issuer_routes.go:31-37, :115-125, :74; live: metadata returns issuer and token_endpoint on https://auth-beta.upsquad.ai |
| 4.1 | Body ≤ 64 KiB | internal/auth/oauth/token_endpoint.go:62, :208 |
| 4.1 | Other issuer-host paths → 404 issuer_path_not_published; the team gateway is also served | infra/edge/envoy.yaml:949 (/mcp/team/), :1000 |
| 4.2 | LLM resource = platform base + /llm, exact match; empty resource = MCP audience | internal/auth/oauth/agent_token.go:105; internal/auth/oauth/token_endpoint.go:277-293, :246 |
| 4.2 | On this service base == issuer, so the value is the issuer + /llm | docker-compose.dev.yml:946-953 (MCP_GATEWAY_PUBLIC_BASE_URL derives from PLATFORM_ISSUER); live: metadata issuer = token_endpoint origin = https://auth-beta.upsquad.ai; resource=https://model-gw-beta.upsquad.ai/llm → 400 invalid_target |
| 4.2, 6.2 | An MCP-audience token is refused at the gateway with "this token was not issued for the Model Gateway" | cmd/model-gateway/main.go:222; internal/modelgateway/agentauth/middleware.go:262 |
| 4.3 | Secret in the form; no HTTP Basic | internal/auth/oauth/token_endpoint.go:256, :378-379 (form only), :261 (nothing presented → deny) |
| 4.4 | Assertion: algorithms; iss=sub=client_id; aud = token endpoint URL; exp ≤ 5 min ±60 s; jti single-use | internal/auth/oauth/token_endpoint.go:71-75, :315-316, :355, :361-363, :58, :60, :367, :604-621 (in-process guard); cmd/context-engine/main.go:2551 (token endpoint URL) |
| 4.4 | Assertion wins over secret; client_id read from the assertion | internal/auth/oauth/token_endpoint.go:252-259, :315 |
| 4.5 | 200 body, no-store, Pragma, no refresh token, expires_in 600 | internal/auth/oauth/token_endpoint.go:165-170, :455-461, :65 |
| 4.5 | Token header and claims; no sub; 10 min TTL | internal/auth/oauth/agent_token.go:285-310, :287 (sub omitempty), :339, :362, :373, :384, :45; the external mint sets no subject: internal/auth/oauth/token_endpoint.go:425-431 |
| 4.5 | Clearance and enablement read per call; endpoint and budget caches ≤ 30 s / 15 s | cmd/model-gateway/main.go:214-223; internal/modelgateway/enablement.go:231; internal/modelgateway/cache.go:19; internal/modelgateway/budgetsource.go:958 |
| 4.5 | Gateway checks signature, issuer, audience, expiry; no skew past exp; no client-revocation check per call | internal/auth/oauth/jwks_validator.go:101, :140-145; internal/modelgateway/agentauth/middleware.go:223-264 |
| 4.5, 4.6 | Issuer limits 1 r/s burst 5 per client, burst 10 per source; lockout 5 failures, 1 s doubling to 5 min, success clears | internal/auth/oauth/token_endpoint_limiter.go:26-36, :112-124, :129; internal/auth/oauth/token_endpoint.go:203, :466-494 |
| 4.6 | Uniform 401 body for every authentication failure; true reason audited | internal/auth/oauth/token_endpoint.go:494, :488-493, :175-188 (reasons); :546-556 (writer) |
| 4.6 | 400 invalid_target / unsupported_grant_type / invalid_request; 405 with Allow; 429 slow_down | internal/auth/oauth/token_endpoint.go:246, :221, :210, :194-195, :204 |
| 4.6, 6.1 | Edge 429 is Envoy's plain local_rate_limited, x-ratelimit-*; /v1/ adds retry-after: 60 | infra/edge/envoy.yaml:596-611 |
| 5.1 | Authorization: Bearer exact; uq_key_ prefix routes elsewhere; no header → invalid_api_key | internal/modelgateway/agentauth/middleware.go:394, :405-416; internal/llmshared/auth/middleware.go:65, :203 |
| 5.1 | JSON body with model required; empty body → invalid_request (so no GET /v1/models) | internal/modelgateway/resolver.go:714, :732, :735; no /v1/models route under cmd/model-gateway or passthrough |
| 5.1 | Endpoint chosen by model; binding and enablement gate it | internal/modelgateway/resolver.go:520 (selectEndpoint), :541 (bindingGate); internal/modelgateway/gates.go:214-230 |
| 5.2 | Headers removed: caller auth, X-Upsquad-*, edge metadata, trace context, hop-by-hop | internal/modelgateway/transport/headers.go:11, :30, :49, :88-99, :163-200; internal/modelgateway/transport/tracecontext.go:70 |
| 5.2 | Added: tenant credential by dialect; Accept-Encoding: gzip | internal/modelgateway/transport/headers.go:203, :210, :215; internal/modelgateway/transport/types.go:54 |
| 5.2 | Body forwarded unchanged; query kept unless the registered URL has one | internal/modelgateway/dialect/openai.go:96-103 ("the bytes that were governed must be the bytes that were sent"); internal/modelgateway/passthrough/handler.go:667; internal/modelgateway/transport/http.go:265-267 |
| 5.2, 6.4 | X-UpsQuad-Subscriber selects the subscriber admission; an agent caller never enters it; subscription_mode_disabled is the one subscription code an agent can get | internal/modelgateway/subscriberauth/select.go:25, :179-182, :232; cmd/model-gateway/main.go:356; internal/modelgateway/resolver.go:1530 |
| 5.3 | Dialect from path suffix and top-level keys; null ignored; ambiguous / mismatch / unrecognised outcomes | internal/modelgateway/callerdialect.go:164-200, :222, :283; internal/modelgateway/mode.go:224, :261, :265, :283 |
| 5.4 | Response headers stripped: hop-by-hop, Set-Cookie, X-Upsquad-*; provider status and body relayed | internal/modelgateway/transport/headers.go:312-335; internal/modelgateway/passthrough/pipe.go:280-293 |
| 5.4, 6.1 | X-Upsquad-Usage-Event-Id on every handler response, refusals included, not on middleware 401s | internal/modelgateway/passthrough/receipt.go:31, :57; internal/modelgateway/passthrough/handler.go:485; cmd/model-gateway/main.go:540 (auth wraps the handler) |
| 5.5 | SSE relayed unbuffered; usage never injected | internal/modelgateway/passthrough/pipe.go:293; internal/modelgateway/dialect/openai.go:96-103 |
| 5.5, 6.5 | endpoint_unmetered: last call unmetered while capped; probe admitted per cooldown | internal/modelgateway/resolver.go:1376-1384 |
| 5.6 | 32 MiB body cap, refused not truncated | internal/modelgateway/resolver.go:175, :722-726; internal/modelgateway/passthrough/handler.go:31 |
| 5.6 | Request read within 60 s | cmd/model-gateway/main.go:563 |
| 5.6 | Head waits: streamed 30 s, non-streamed 90 s → 502 | internal/modelgateway/build.go:164, :1124-1133; internal/modelgateway/config.go:341 (not overridden in the model-gateway service, docker-compose.dev.yml:2245-2427); internal/modelgateway/passthrough/handler.go:731 |
| 5.6 | Cap cuts a running stream; edge idle 300 s | internal/modelgateway/passthrough/pipe.go:305; infra/edge/envoy.yaml:614 |
| 5.7 | Translation needs binding permission AND X-Upsquad-Translate: 1; 0 declines; other values 400; not-permitted 403 | internal/modelgateway/mode.go:90, :136-139, :283, :293, :314 |
| 5.7 | No-analogue feature → 501 before dialling; unreadable body → 400; mid-stream → in-band error event | internal/modelgateway/passthrough/handler.go:515-535, :572-590; internal/modelgateway/translate/response.go:45 |
| 6.1, 6.2 | Gateway 401 envelope with type; the messages | internal/modelgateway/agentauth/middleware.go:423-431, :219, :229, :262, :279, :299 |
| 6.3 | Envelope fields; gate from the step label, absent on post-admission fail; fleet: agent | internal/modelgateway/passthrough/handler.go:1238-1270, :930-934; internal/modelgateway/resolver.go:1695-1710; internal/modelgateway/passthrough/fleet.go:144 |
| 6.3 | Classes by status; 500/501 declared unclassified | internal/modelgateway/passthrough/wire.go:58-65, :78-86, :186 |
| 6.3 | Retry-After on 429 only | internal/modelgateway/passthrough/handler.go:1231-1233; internal/modelgateway/errorcontract.go:200, :629 |
| 6.4 | The complete code table | generated from modelgateway.RefusalContract() (internal/modelgateway/errorcontract.go:294), pinned in both pages by test/lint/model_gateway_error_contract_doc_test.go |
| 6.4 | Ledger values | internal/modelgateway/passthrough/ledger.go:78, :87, :108 |
| 6.5 | Step order | internal/modelgateway/gates.go:214-230; internal/modelgateway/resolver.go:496, :511, :520, :541, :591 |
| 6.5 | Rate classes 200/100/20 per second, burst ×2; org total; team skipped at org grain | internal/modelgateway/ratelimit.go:146, :168, :174, :183, :274-329, :391-407, :494-520 |
| 6.5 | Budgets: monthly derived period, all four token classes, min-of grains, pre-call, AT LEAST | internal/modelgateway/budgetsource.go:420, :514, :308; internal/modelgateway/gates.go:1074-1086; internal/modelgateway/budget.go:221 |
| 6.6 | Provider responses relayed unchanged; no retry | internal/modelgateway/passthrough/pipe.go:280-293; internal/modelgateway/passthrough/handler.go:662-733 (one Stream call, no retry loop) |
| 7 | One usage row per call, refusals included; rate-limit refusals can be shed under saturation | docs/architecture/model-gateway.md §0 and §5; internal/modelgateway/recordadmission.go:64-80, :154 |
| 7 | Tenant views: usage by grain, CSV export, burndown, L3; unknown cost separate | proto/upsquad/aigateway/v1/usage.proto:85-116, :139, :21; proto/upsquad/aigateway/v1/budget.proto:187 |
| 7 | Deny rows also hash-chained; token issuance and denial audited | internal/modelgateway/passthrough/ledger.go:78; internal/auth/oauth/token_endpoint.go:441, :488 |
| 8.6 | SDK header behaviour | Not UpsQuad code: the OpenAI SDK sends api_key as Authorization: Bearer; the Anthropic SDK sends api_key as x-api-key and auth_token as Authorization: Bearer (their public documentation). The gateway side is the §5.1 row. |
| 8.7 | Budget refusal message text | internal/modelgateway/gates.go:1074-1086; internal/modelgateway/budget.go:221; body order internal/modelgateway/passthrough/handler.go:1238-1270 |
10.2 Where this page and the code disagreed (found 2026-11-17, while deriving the handout)
Recorded here rather than silently rewritten above, because the dated sections are evidence of what was true when they were written. The handout follows the code.
- §3.1 "Known limit" is stale.
token_endpointandjwks_uriin the AS metadata are no longer the internalhttp://context-engine:8080:MCP_GATEWAY_PUBLIC_BASE_URLnow derives fromPLATFORM_ISSUER(docker-compose.dev.yml:946-953), and the live metadata on 2026-11-17 advertiseshttps://auth-beta.upsquad.ai/oauth/token. - "Derive the audience from
issuer" is a coincidence of configuration, not the validator's derivation. The validator and the minter derive it from the platform base URL (internal/auth/oauth/agent_token.go:105,cmd/model-gateway/main.go:222); it equalsissuer+/llmonly because this deployment sets both from one variable. - §6.2 lists three rate classes; the declaration has four. A
memberclass sits besideagent(internal/modelgateway/ratelimit.go:307-328). For an external agent the org, team and agent classes apply. - §4 says dialect is inferred "from the request shape, not from a route table" and lists
body keys only. The path suffix is evidence too (
internal/modelgateway/callerdialect.go:164-200):/v1/messagesnamesanthropicon its own, so a/v1/messagesbody carryingstopisdialect_ambiguous. - §2's
credentials_enabled=falseis a 2026-09-07 snapshot. The tracked compose now passesVAULT_PASSPHRASEtomodel-gateway(docker-compose.dev.yml:2291). Read the running process'scontrols wiredline, as §2 already says. - §3 "What was NOT executed" and §9's last bullet say no successful mint or relay was performed; both were overtaken by #3674 (2026-09-30), as the box in §3 already notes.
Related
- Connecting your agent to the UpsQuad Model Gateway — the external-developer handout this page is the evidence companion for
- Platform Token Issuer (MCP trust root) — the
/oauth/tokensurface, both client-authentication methods, the enable gate, the hardening posture - Completion-path provider posture, per deployment shape — a different seam (the platform's own completion path) with a different trust class; its "an RFC1918 address is fine" rule does not apply to the Model Gateway registry
- Landing a PR
- PRD #2644 · HLD #2649 · ADR-0034 #2643 · LLD #2901 · tracker #2648