Advanced API Usage

Custom Moderation Controls

Add a guidelines array to a request to set how Grok further handles specific content categories. For each category you include, you choose a further enhanced behavior. Today the only behavior is decline. Categories you leave out continue to keep the model's default moderation behavior.

Grok follows the guidelines as it writes its response, in its own words. The response has the same shape as a response without guidelines.


Categories and behaviors

Each entry in guidelines has three fields: type, which is always "policy", a category, and the behavior you want for that category.

CategoryBehaviorWhat Grok does
nsfwdeclineAvoids sexual innuendo or sexualized conversations. Still answers educational and clinical questions about sexual topics.
violencedeclineAvoids depictions of graphic violence. Engages with history and news without graphic detail.
profanitydeclineWrites without using profanity. Declines when the request is for profanity alone, or when the request is highly offensive.

The table describes each behavior at a high level. SpaceXSI may refine the details over time.

A category you leave out keeps the model's default behavior, so list only the categories you want to change.


Set guidelines on a request

This example request sets violence and nsfw to decline.

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("XAI_API_KEY"),
    base_url="https://api.x.ai/v1",
)

response = client.responses.create(
    model="grok-4.7",
    input="Write a short story about a knight.",
    extra_body={
        "guidelines": [
            {"type": "policy", "category": "violence", "behavior": "decline"},
            {"type": "policy", "category": "nsfw", "behavior": "decline"},
        ],
    },
)

print(response.output_text)

The OpenAI SDKs do not define guidelines. With the OpenAI Python SDK, pass it in extra_body. The OpenAI JavaScript SDK sends it with the other request parameters. On Chat Completions, add the same guidelines array as a top-level field of the request body, at the same level as messages.

Guidelines apply only to the request that carries them. To apply them to a whole conversation, send them on every request in it. In the xAI Python SDK, chat.create stores them on the chat, so every sample call sends them.


Requests that cannot use guidelines

Guidelines currently do not work with multi-agent models, such as grok-4.20-multi-agent, or with deferred completions. A request with no server-side tools and no file attachments also cannot use guidelines if it sets stop or sets n greater than 1. The API rejects these requests with 400 before the model runs. The error message names the model or the option.


Errors

The API checks guidelines before it runs the model. If it cannot apply them, it returns an error and does not run the model.

StatusCause
422The category or behavior is not a known value. The error lists the valid values.
400The same category appears more than once.
400The type is not "policy".
400The request cannot use guidelines, as described in Requests that cannot use guidelines. The message names the model or the option.

In the xAI Python SDK, an unknown category or behavior raises ValueError before the SDK sends the request. The API returns the other errors as a gRPC INVALID_ARGUMENT status.


Supported APIs

guidelines is accepted on Chat Completions and the Responses API, and on chat.create in the xAI Python SDK.


Last updated: October 27, 2026