Cloudflare AI Gateway Guardrails are set to Block a hazard category in responses, yet a streamed chat reply with that content reached users; why, and what do you change?
answer
- streaming is the exception
- evaluated and logged, not enforced
- which endpoint the client calls
- stream false where Block must hold
- 2016 for prompts, 2017 for responses
basics
~20 sGuardrails do not support streaming. On the REST API a streamed response is evaluated and logged but still delivered in full, so Block is not enforced. Send stream: false wherever a response block must hold, and handle errors 2016 and 2017.
solid answer
~40 sGuardrails do not support `stream: true` requests. Prompts are still evaluated and enforced, so a blocked prompt fails with error `2016`. The response side depends on the API surface: on the REST API at `api.cloudflare.com`, Guardrails evaluate the streamed response and **log** the result but do not enforce it, so the client receives the whole stream. On the `gateway.ai.cloudflare.com` endpoints, Guardrails buffer the full response, evaluate it, and return one non-streamed payload. A chat UI on the REST API with streaming on therefore shows content Block should have stopped. For flows where response blocking must hold, send `stream: false` and handle error `2017`, or accept flag-and-review for streamed flows. Then confirm in the logs that evaluations ran, and check that no API update dropped the `guardrails` property.
go deeper
Recall that Guardrails check prompts and responses per category with Flag, Ignore or Block, and that blocked requests return errors 2016 and 2017.
Explain that Guardrails run on Workers AI models, add latency and usage, and that streaming changes how responses are handled.
Diagnose the unenforced stream from the API surface and the stream flag, and check the other silent causes: Flag under failure, model type and a PUT that dropped guardrails.
Decide which flows must enforce response blocking and give up streaming, and which accept detection with review, and own that statement to compliance.
## What Guardrails do **Guardrails** are an AI Gateway feature that checks **prompts** and **model responses** against hazard categories and, per category and direction, applies **Flag**, **Ignore** or **Block**. They run on Workers AI: categories `S1` to `S13` (violent crimes, privacy, hate, elections and the rest) are evaluated with `@cf/meta/llama-guard-3-8b`, and `P1`, prompt injection, with `@cf/meta/prompt-guard-2-86m`. Enabling them therefore adds Workers AI usage to the bill and latency to each request: typically about 500 milliseconds, more for large requests, which are split into chunks and evaluated separately. A blocked request fails with a structured error: - `2016`, "Prompt blocked due to security configurations"; - `2017`, "Response blocked due to security configurations". ## Why the streamed reply got through Guardrails **do not support streaming** (`stream: true`). What happens to a streamed request depends on where it is sent: | Surface | Prompt | Streamed response | | --- | --- | --- | | REST API, `api.cloudflare.com/.../ai/...` | Evaluated and enforced | Evaluated and **logged, not enforced**: the client receives the full stream | | Gateway endpoints, `gateway.ai.cloudflare.com/v1/...` | Evaluated and enforced | Buffered, evaluated, then returned as **one non-streamed payload** | | Either, with `stream: false` | Evaluated and enforced | Evaluated and enforced | The support chat sends `stream: true` to the REST API, so a response in a Block category was evaluated, recorded in the logs, and delivered anyway. Nothing was misconfigured in the category table; the request shape put it outside enforcement. ## What to change 1. **Decide per flow whether blocking must hold.** Where it must, for example a public-facing assistant, send `stream: false` and render the reply when it arrives. The user waits longer for the first word, but Block is enforced. 2. **Where streaming matters more**, keep streaming and treat Guardrails as **detection**: review flagged and would-be-blocked responses in the logs and tighten prompts or models. Do not tell compliance that Block covers this path. 3. **Know the gateway-endpoint behaviour**: moving the same call to a `gateway.ai.cloudflare.com` URL enforces the check by buffering, which silently ends streaming. A client written for chunks must cope with one payload. 4. **Handle the errors**: catch `2016` and `2017` and show a neutral message rather than a raw error. ## Other reasons a block does not fire - **Flag versus Block under failure.** If at least one category is set to Block and AI Gateway cannot get an answer from Workers AI, the request is **blocked**. If categories are only Flag, the request **proceeds unevaluated**. Guardrails favour availability unless you choose Block. - **Model types.** For embedding models only the prompt is evaluated. When AI Gateway cannot tell a model's type, it evaluates the prompt and skips the response. - **Configuration overwritten.** The gateway update endpoint is a `PUT`. A script that sends the gateway's settings without the `guardrails` property **disables Guardrails and removes their configuration**. Read the current gateway first and send its settings back. The API accepts `FLAG` and `BLOCK`; a category left out behaves as Ignore. - **Language coverage.** The evaluation model supports a fixed list of languages, English, French, German, Hindi, Italian, Portuguese, Spanish and Thai; content in other languages falls outside that list. ## What enforcement costs Turning streaming off is not free, and the trade-off belongs in the decision: - **Time to first word** grows to the full generation time plus the evaluation, because nothing is shown until the whole reply has been checked. - **Evaluation latency** adds roughly half a second per request at typical sizes, and long replies are split into chunks that are each evaluated. - **Workers AI usage** grows with every evaluated prompt and response, and shows up in the Workers AI dashboard rather than as a provider charge. A common split is non-streamed, enforced replies for anonymous public users and streamed, flagged replies for internal staff tools, with the flagged log entries reviewed on a schedule. ## Verifying it works - In the gateway's **Logs**, Guardrails evaluations appear as their own entries marked with a shield icon, linked to the request by an event ID. Logs are written for passing requests too, so a missing evaluation means Guardrails did not run. - Send a known test prompt with `stream: false` and confirm a `2017` or `2016`; repeat with `stream: true` on the REST API and confirm the stream arrives with an evaluation logged. That pair documents the behaviour for reviewers. - Recheck after every change to the gateway made through the API.
- Why might a team set some Guardrails categories to Block even where availability matters most?Block changes failure behaviour. With any category on Block, a request is refused when Guardrails cannot reach Workers AI to evaluate it; with only Flag, it proceeds unevaluated. A team that must never return unchecked content in a regulated flow chooses Block and accepts the outage risk; a team that prizes uptime keeps Flag and reviews logs.
- An automation updated a gateway's rate limit through the API and Guardrails stopped logging evaluations; what happened?The gateway update is a `PUT`. Omitting the `guardrails` property disables Guardrails and removes their configuration. Restore the categories, then change the automation to read the current gateway and send every existing setting back with the one it changes.
- Block on a response category stops streamed replies mid-stream on every surface.
- Guardrails skip prompts as well when a request streams.
- A Flag-only gateway refuses requests when the evaluation model is down.
- Guardrails run on the model provider's own moderation, so they cost nothing extra.
- Leaving a property out of the gateway PUT keeps its current value.
Assumes Cloudflare's platform as of 2026-10-10: the docs list full Guardrails support for streaming as planned, and the REST API (since May 2026) and the gateway endpoints treat streamed responses differently.