Skip to main content

Moderation

/v1/moderations classifies a piece of text against content policy categories such as hate, harassment, self-harm, sexual content and violence, and answers whether the text is flagged and how strongly each category applies. It is a cheap check to run before you store user content, show it to other users, or send it on to a model. The gateway serves the OpenAI moderations format for every moderation model it knows, so one request shape reaches OpenAI, Mistral, Azure AI Content Safety, Amazon Bedrock Guardrails and Llama Guard, and the same routing, fallbacks and spend tracking apply as for chat calls.

A real-world scenario​

A community platform lets members post comments that other members read. Before a comment is published, the platform's backend sends it to /v1/moderations through the gateway. A flagged comment goes to a human review queue instead of the page, and the category scores tell the reviewer why. The platform team registered two moderation models, one as the primary and one as a fallback, so a provider outage never leaves comments unchecked, and the Usage page shows what moderation costs per month next to the chat spend.

Set it up​

  1. Register a moderation model. In the Admin UI open Models + Endpoints under AI Gateway, open the Add Model tab, pick the provider, enter omni-moderation-latest as the model name and the credentials, set Mode to Moderation - /moderations, and click the add button. In config.yaml the same deployment carries mode: moderation so health checks use the moderations endpoint:

    model_list:
    - model_name: omni-moderation-latest
    spanway_params:
    model: openai/omni-moderation-latest
    api_key: os.environ/OPENAI_API_KEY
    model_info:
    mode: moderation

    Expected result: the model appears on the Deployed Models tab and GET /v1/models lists it.

  2. Send a moderation request:

    curl -X POST http://localhost:4000/v1/moderations \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"model": "omni-moderation-latest", "input": "Text to classify"}'

    Expected result:

    {
    "id": "modr-6a1f...",
    "model": "omni-moderation-latest",
    "results": [
    {
    "flagged": false,
    "categories": {"hate": false, "harassment": false, "self-harm": false, "sexual": false, "violence": false},
    "category_scores": {"hate": 0.0001, "harassment": 0.0003, "self-harm": 0.0, "sexual": 0.0001, "violence": 0.0002}
    }
    ]
    }

    input is a string, a list of strings, or a list of content parts with text and images for multimodal moderation models. The native providers below accept text only. When model is omitted, the gateway uses the provider's default moderation model.

  3. Read the log. Open Logs and click the request. It records the model, the provider and the spend, and the Usage page adds moderation spend into the key's and team's totals and budgets.

A model that matches a model group in your config is routed through the router, so load balancing, retries, fallbacks and cooldowns apply to moderation calls the same way they apply to chat calls. On the gateway, a native provider model (one of the providers below) must be a configured model group. A request that names such a model directly, like mistral/mistral-moderation-latest, is rejected with a 400 so callers cannot reach a provider with the gateway's own credentials, unless pass_through_all_models is turned on. A moderation request that carries a data policy is also rejected unless it names a configured model group.

Native moderation providers​

Besides OpenAI-compatible moderation APIs, the gateway calls these providers natively and maps their answer into the OpenAI response shape. Every response carries all thirteen OpenAI categories. A vendor category with no OpenAI equivalent is returned under its own name in categories and category_scores, and it still counts toward flagged. When two vendor categories map to the same OpenAI category, the OpenAI category is flagged if either is, and its score is the higher of the two.

ProvidermodelCredentials
Mistralmistral/mistral-moderation-latestapi_key (or MISTRAL_API_KEY), optional api_base.
Azure AI Content Safetyazure_ai/content-safety (any name under azure_ai/)api_key and api_base of the Content Safety resource (or AZURE_CONTENT_SAFETY_API_KEY and AZURE_CONTENT_SAFETY_API_BASE), optional api_version. The Azure AI Foundry variables AZURE_AI_API_KEY and AZURE_AI_API_BASE are never used.
Amazon Bedrock Guardrailsbedrock/guardrail/<guardrail id or ARN>The usual aws_* params or a Bedrock API key, guardrail_version (default DRAFT).
Llama GuardAny chat model whose name contains llama-guard, such as deepinfra/meta-llama/Llama-Guard-4-12BThe chat provider's usual credentials.
model_list:
- model_name: moderation
spanway_params:
model: mistral/mistral-moderation-latest
api_key: os.environ/MISTRAL_API_KEY
- model_name: moderation-backup
spanway_params:
model: azure_ai/content-safety
api_key: os.environ/AZURE_CONTENT_SAFETY_API_KEY
api_base: https://my-resource.cognitiveservices.azure.com
- model_name: bedrock-moderation
spanway_params:
model: bedrock/guardrail/gr-abc123
guardrail_version: "1"
aws_region_name: us-east-1

router_settings:
fallbacks: [{"moderation": ["moderation-backup"]}]

guardrail_version and price_per_1000_text_records belong to the deployment. The gateway rejects them in a request body unless configurable_clientside_auth_params or allow_client_side_credentials allows it, so a caller cannot switch a pinned guardrail version or zero out the price.

Mistral​

One request carries every input. flagged is true when Mistral flags any category, and the scores are Mistral's own.

Mistral categoryOpenAI category
sexualsexual
hate_and_discriminationhate
violence_and_threatsviolence
dangerous_and_criminal_contentillicit
selfharmself-harm
health, financial, law, pii and any newer categoryKept under the Mistral name.

Azure AI Content Safety​

Each input is sent to text:analyze with the FourSeverityLevels output, and inputs longer than 10,000 characters are split at word boundaries with the worst severity per category kept. A category is flagged at severity 2 or higher, the same default the Azure Content Safety guardrail uses, and its score is the severity divided by 6.

Azure categoryOpenAI category
Hatehate
Sexualsexual
SelfHarmself-harm
Violenceviolence

Amazon Bedrock Guardrails​

Each input is sent to ApplyGuardrail with source: INPUT. When AWS rejects an input as larger than the guardrail's text unit limit, the input is split at whitespace and each part is checked on its own, with the results merged the same way as two categories that share an OpenAI name. An input that cannot be split further returns a 400 rather than a rate limit error. flagged follows the guardrail's own decision (GUARDRAIL_INTERVENED). A content filter is flagged when the guardrail acted on it, and its score comes from the filter confidence (NONE 0, LOW 0.33, MEDIUM 0.67, HIGH 1).

Bedrock policyOpenAI category
Content filter HATEhate
Content filter INSULTSharassment
Content filter SEXUALsexual
Content filter VIOLENCEviolence
Content filter MISCONDUCTillicit
Content filter PROMPT_ATTACKprompt_attack
Denied topicsdenied_topics
Word filtersword_filter
Sensitive information (PII and regexes)sensitive_information

Llama Guard​

Each input is sent as one user message to the chat model, and the safe or unsafe verdict with its hazard codes is mapped as below. Llama Guard returns no scores, so a flagged category scores 1 and the rest score 0. The chat call's token cost is recorded as the moderation call's cost when the model has pricing. An answer that is not a Llama Guard verdict returns a 502 so the router can fall back.

Hazard codeOpenAI category
S1 violent crimesviolence
S2 non-violent crimesillicit
S3 sex-related crimessexual
S4 child sexual exploitationsexual/minors
S5 defamationdefamation
S6 specialized advicespecialized_advice
S7 privacyprivacy
S8 intellectual propertyintellectual_property
S9 indiscriminate weaponsillicit/violent
S10 hatehate
S11 suicide and self-harmself-harm
S12 sexual contentsexual
S13 electionselections
S14 code interpreter abusecode_interpreter_abuse

Cost tracking​

ProviderHow the cost is computed
MistralPrompt tokens (Mistral's reported usage, or a local token count when the response has none) times input_cost_per_token, taken from the deployment, its model_info, or the model cost map. A model with no price records no cost.
Azure AI Content SafetyText records (one per started 1,000 characters of each request sent) times price_per_1000_text_records / 1000. Set price_per_1000_text_records on the deployment to match your pricing tier. Without it no cost is recorded.
Amazon Bedrock GuardrailsThe usage units ApplyGuardrail reports for each policy, priced from the bedrock/guardrails entry of the model cost map (or a regional bedrock/<region>/guardrails entry).
Llama GuardThe token cost of the underlying chat calls, when the chat model has pricing.

Moderation as a guardrail​

To moderate every request automatically rather than calling the endpoint yourself, configure the OpenAI Moderation guardrail or any other content guardrail with mode: pre_call, which blocks flagged requests before the model is called. The apply guardrail endpoint runs a configured guardrail on arbitrary text, which covers providers that have no moderations endpoint. See guardrails.

Errors​

StatusMeaningFix
400 model names a native provider directlyThe request used a provider model name such as mistral/mistral-moderation-latest instead of a configured model group.Register the model in the config or the Admin UI and send its model_name.
400 guardrail_version or price_per_1000_text_records in the bodyThese belong to the deployment and client overrides are off.Remove them from the request, or allow them with configurable_clientside_auth_params.
400 from Bedrock about the input sizeA single word is longer than the guardrail's text unit limit, so it cannot be split.Shorten the input.
401The provider credentials are missing on the deployment.Set api_key or the provider's environment variable on the deployment.
429The provider rate limited the call. The router retries and falls back when fallbacks are configured.Add a fallback moderation model under router_settings.fallbacks.
502 from a Llama Guard modelThe chat model answered something that is not a Llama Guard verdict.The router falls back when a fallback is configured. Check the model name contains llama-guard and points at a Llama Guard model.
No cost recordedThe deployment has no price.Set input_cost_per_token, price_per_1000_text_records, or add the model to the cost map.
  • Guardrails to moderate every request automatically with a pre_call guardrail.
  • Apply guardrail to run a configured guardrail on text.
  • Reliability for retries, fallbacks and cooldowns that moderation calls share with chat calls.
  • Cost tracking for how moderation spend reaches budgets and the Usage page.
  • Models + Endpoints page for registering a moderation model in the Admin UI.

Providers​

The gateway's endpoint support matrix marks 10 provider integrations as supporting this endpoint:

AIHubMix (aihubmix), Azure, Azure AI, Azure Text, AWS - Bedrock, Custom OpenAI (custom_openai), Mistral AI API, Oobabooga (oobabooga), OpenAI, Text Completion OpenAI.