Skip to main content

Data Model: Centralise LLM Routing, Quotas and Budgets

One new Mongo collection and one Keycloak object per agent. Limits and usage stay in the gateway.

1. Deployment settingsโ€‹

SettingDefaultMeaning
LLM_GATEWAY_ENABLEDfalseGlobal routing switch. false uses direct-provider configuration and bypasses the gateway even when configured and reachable; true routes migrated paths through the gateway and fails closed on gateway failure.
OPENAI_COMPATIBLE_BASE_URLโ€”Gateway endpoint (from llm_wrapper).
LLM_GATEWAY_AUTHjwtjwt (gateway token) ยท key (per-agent gateway key). A gateway property, independent of the adapter.
LLM_GATEWAY_AUDIENCEllm-gatewayaud of gateway tokens. The gateway must expect the same value.
LLM_GATEWAY_ADAPTERnoneGateway management integration: litellm ยท agentgateway ยท kong ยท agentrouter ยท none. none disables management integration, not gateway routing (see adapter contract).
LLM_GATEWAY_DEFAULT_TEAMplatformTeam charged for agents without owner_team_slug. Provisioned at startup.
LLM_GATEWAY_BUDGET_SIGNALnamed adapter default; unset for noneGateway-limit recognition rule (research Decision 10). Required for budget-error conformance with adapter none.
Adapter admin credentialโ€”Through the existing secret strategies. Only needed by the UI backend for management integration; not required for none.

Direct-provider configuration and credentials must remain available for disabled mode; configuring a gateway endpoint alone does not enable routing.

2. llm_models (existing collection, additive fields)โ€‹

FieldExisting?Meaning
model_idโœ“Same value the gateway alias uses. Unchanged for existing rows.
name, provider, descriptionโœ“Unchanged.
sourcenewseed ยท admin ยท gateway
gateway_synced_atnewTime of the last list_models sync.
  • Persistence: model catalogue entries persist in MongoDB. Entries come from application configuration, the LLM Models UI, or gateway sync when the adapter declares models.list. Without that capability, administrators maintain entries through the existing configuration or UI and ensure model IDs match gateway aliases. Model sync is not required for inference.
  • Rule: when LLM_GATEWAY_ENABLED=true, build_chat_model uses the openai-compatible provider with model_id, whatever provider the caller passes.

3. Gateway token (JWT, not stored)โ€‹

ClaimExampleSource
isshttps://idp.example.com/realms/caipesigner
audLLM_GATEWAY_AUDIENCEsetting
subuser or task-owner subject (effective principal)caller token / trusted run binding (#2891)
act{"sub": "service-account-dynamic-agents"}acting service (#2885)
orgexample-orgexisting org claim (budget key namespace)
agent_idagent-exampleKeycloak scope (K2) or signer (C1)
team_idteam-exampleagent owner_team_slug, else LLM_GATEWAY_DEFAULT_TEAM
expโ‰ค 5 minsigner
  • Cache key: (sub, agent_id, owner_team_slug). The key alone does not ensure ownership consistency: CAIPE must check credential bindings against committed ownership before use or caching, block new calls during unresolved transfers, and invalidate cached credentials for the previous owner on completion.
  • Caller kind (user, service account, autonomous) is an OTel label set by the runtime, not a claim.

4. Agent scope (Keycloak, K2 only)โ€‹

FieldValue
namellm-agent-<agent_id>
mappershardcoded claims agent_id, team_id
assigned todynamic-agents exchanger client (optional scope)
lifecyclecreated with the agent; synchronized through a recoverable ownership-transfer operation; deleted with the agent
  • MongoDB ownership is authoritative. Transfer coordination must prevent calls from using inconsistent ownership and credential bindings, and support recovery after interruption. The recovery mechanism is an implementation decision; updating Keycloak before saving MongoDB is not sufficient on its own.

AI-assist system identityโ€‹

  • Provision a stable AI-assist system agent and its authorization relationships before enabling gateway routing for /assistant/suggest.
  • Grant its can_use permission to users authorized to use AI-assist through the existing authorization model; model access remains a separate CAS check.
  • Prepare its Keycloak scope (K2), signer mapping (C1), or per-agent gateway key according to the configured authentication mode, including any gateway principal required before first use.
  • Use the authenticated caller as sub, the system agent as agent_id, and its owning team as team_id (or LLM_GATEWAY_DEFAULT_TEAM when ownerless). The agent being drafted or edited supplies none of these identity bindings.

5. llm_quota_requests (new Mongo collection)โ€‹

FieldMeaning
scopeuser ยท agent ยท team
principal_idsubject, agent id or team slug
requested_bysubject
requested_limit, reasonthe ask
statuspending ยท approved ยท rejected
decided_by, decided_atapprover
created_atโ€”
  • On approval, the UI backend calls set_limit on the adapter. The gateway holds the result.

6. Adapter capabilities (not stored)โ€‹

Capabilities describe CAIPE's supported gateway management operations, including model discovery. Auth mode is LLM_GATEWAY_AUTH; inference routing is controlled by LLM_GATEWAY_ENABLED.

CapabilityMeaning
models.listcan fetch gateway models and persist them in llm_models; exposing /v1/models alone does not enable CAIPE sync
principals.ensuremust pre-create a user, agent or team before first use
limits.user / limits.agent / limits.teamcan set a limit per dimension
limits.budget_usd / limits.tokens / limits.rpmsupported limit types
usage.readcan return spend or usage per principal
keys.issuecan issue per-agent keys; required only for automated key provisioning through CAIPE
  • Key authentication requires a valid per-agent gateway key in CAIPE's credentials store. Operator-provisioned keys work without keys.issue, including with adapter none.