Framework
Decide how the application should respond to unsafe input and to an unavailable moderation provider. Those are separate outcomes. Configure the policy, identify the originating user where supported, and inspect the audit record when a request is blocked.
The AI assistant runtime ships two provider-aware content guardrails that protect the instance owner's LLM-provider organization from enforcement (warning → suspension → termination) triggered by abusive end users on untrusted surfaces (customer portals, public widgets):
Both are additive: existing tenants see no behavior change until an agent declares
untrustedInput or a tenant/env enables moderation.
For every chat turn the runtime computes:
endUserIdentifier = HMAC-SHA256(derivedSecret, `${tenantId}:${userId}`)
JWT_SECRET) under the purpose label ai-safety-identifier – no new secret to
provision, and no PII or reversible id ever leaves the platform.providerOptions
(keys are the AI SDK provider-option names in camelCase; the SDK translates
them to each provider's snake_case request-body field):
{ openai: { safetyIdentifier } } (sent as safety_identifier). Only the
native openai provider maps it – OpenAI-compatible/self-hosted backends (Groq, DeepInfra,
Azure, Ollama, …) leave it unset since the param is OpenAI-specific.{ anthropic: { metadata: { userId } } } (sent as metadata.user_id).Identifiers are advisory provider-side metadata, not an in-platform security control.
Note · Secret rotation
Rotating JWT_SECRET changes every derived identifier, so the provider sees "new" users and
loses per-user abuse-history continuity. This is accepted – rotation is rare and a
never-rotating dedicated secret would be worse hygiene.
Before the model call, the runtime runs a moderation check for the current user turn only (prior history was already screened on its own turns):
runAiAgentText
├─ resolve model + provider
├─ [gate] resolve moderation policy
│ ├─ skip when the resolved provider has no moderation endpoint
│ ├─ flagged ──► persist AiModerationFlag (categories + scores, no content)
│ │ emit ai_assistant.moderation_flag.created
│ │ throw AiModerationBlockedError → SSE { code: 'moderation_blocked' }
│ └─ not flagged ──► continue
└─ streamText(...)
Moderation runs only when the resolved chat provider supports it (initially the OpenAI
adapter, via OpenAI's free /v1/moderations endpoint). Tenants on Anthropic/Google rely on
those providers' own server-side filtering.
The effective policy for a turn is resolved first-match-wins:
agentDef.untrustedInput === true → enforced (tenant cannot disable).input_moderation on the agent-scoped settings row) → on / off.input_moderation on the tenant-wide row) → on / off.OM_AI_INPUT_MODERATION → on / off.untrustedInput) fail closed: if the moderation endpoint is
unavailable, the turn is rejected with moderation_unavailable rather than silently
bypassing the check.Each blocked input appends one immutable ai_moderation_flags row storing category flags
and scores only – never the prompt content or any PII. Review them at
/backend/config/ai-assistant/moderation-flags (requires ai_assistant.settings.manage) to
quantify false-positive rates and identify repeat abusers before widening enforcement.
| Variable | Default | Purpose |
|---|---|---|
OM_AI_INPUT_MODERATION | unset (off) | Env default for step 4 of the policy precedence (parsed with parseBooleanWithDefault). |
OM_AI_MODERATION_MODEL | omni-moderation-latest | Moderation model id passed to /v1/moderations. |
The safety-identifier HMAC secret is derived from JWT_SECRET – there is no dedicated env var,
and it is never logged.
A module developer shipping a portal-facing agent marks it as accepting untrusted input so moderation is enforced regardless of tenant settings:
import { defineAiAgent } from '@open-mercato/ai-assistant'
export const aiAgents = [
defineAiAgent({
id: 'support.portal_assistant',
moduleId: 'support',
label: 'Portal assistant',
description: 'Customer-facing support assistant.',
systemPrompt: '…',
allowedTools: ['search.hybrid_search'],
untrustedInput: true, // enforces input moderation where the provider supports it
}),
]
ai_assistant.errors.moderationBlocked message inline
in the chat (a soft warning that invites the user to rephrase). The raw category list is
never sent to the client – there is no oracle for probing the filter; categories live
only in the audit record.ai_assistant.errors.moderationUnavailable ("temporarily unavailable").See the spec .ai/specs/2026-06-04-ai-input-moderation-and-safety-identifiers.md for the full
design, risk register, and compliance report.
Previous: MCP Server (Claude Code & external clients) · Next: Mutation Approvals