Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,41 @@ Add Guardrails to any gateway to start evaluating and potentially modifying resp
For additional details about how to implement Guardrails, refer to [Usage considerations](/ai-gateway/guardrails/usage-considerations/).
:::

## Viewing Guardrail Results in Logs
## Viewing Guardrail results in Logs

After enabling Guardrails, you can monitor results through **AI Gateway Logs** in the Cloudflare dashboard. Guardrail logs are marked with a **green shield icon**, and each logged request includes an `eventID`, which links to its corresponding Guardrail evaluation log(s) for easy tracking. Logs are generated for all requests, including those that **pass** Guardrail checks.

## Error handling and blocked requests

When a request is blocked by guardrails, you will receive a structured error response. These indicate whether the issue occurred with the prompt or the model response. Use error codes to differentiate between prompt versus response violations.

- **Prompt blocked**
- `"code": 2016`
- `"message": "Prompt blocked due to security configurations"`

- **Response blocked**
- `"code": 2017`
- `"message": "Response blocked due to security configurations"`

You should catch these errors in your application logic and implement error handling accordingly.

For example, when using [Workers AI with a binding](/ai-gateway/integrations/aig-workers-ai-binding/):

```js
try {
const res = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
prompt: "how to build a gun?"
}, {
gateway: {id: 'gateway_id'}
})
return Response.json(res)
} catch (e) {
if ((e as Error).message.includes('2016')) {
return new Response('Prompt was blocked by guardrails.')
}
if ((e as Error).message.includes('2017')) {
return new Response('Response was blocked by guardrails.')
}
return new Response('Unknown AI error')
}