Ship to production confidently with Portkey Guardrails on your requests & responses
This feature is available on all plans.
BASIC
GuardrailsBASIC
, PARTNER
, PRO
Guardrails.custom
Guardrails.LLMs are brittle - not just in API uptimes or their inexplicable 400
/500
errors, but also in their core behavior. You can get a response with a 200
status code that completely errors out for your app’s pipeline due to mismatched output. With Portkey’s Guardrails, we now help you enforce LLM behavior in real-time with our Guardrails on the Gateway pattern.
Use Portkey’s Guardrails to verify your LLM inputs AND outputs, adhering to your specifed checks. Since Guardrails are built on top of our Gateway, you can orchestrate your request - with actions ranging from denying the request, logging the guardrail result, creating an evals dataset, falling back to another LLM or prompt, retrying the request, and more.
Portkey currently offers 20+ deterministic guardrails like the ones described above as well as LLM-based guardrails like Detect Gibberish
, Scan for prompt injection
, and more. These guardrails serve as protective barriers that help mitigate risks associated with Gen AI, ensuring its responsible and ethical deployment within organizations.
Portkey also integrates with your favourite Guardrail platforms like Aporia, SydeLabs, Pillar Security and more. Just add their API keys to Portkey and you can enable their guardrails policies on your Portkey calls! More details on Guardrail Partners here.
Putting Portkey Guardrails in production is just a 4-step process:
This flowchart shows how Portkey processes a Guardrails request:
Let’s see in detail below:
Portkey only evaluates the last message in the request body when running guardrails checks.
On the Guardrails
page, click on Create
and add your preferred Guardrail checks from the right sidebar.
On Portkey, you can configure Guardrails to be run on either the INPUT
(i.e. PROMPT
) or the OUTPUT
. Hence, for the Guardrail you create, make sure your Guardrail is only validating ONLY ONE OF the Input or the Output.
Each Guardrail Check has a custom input field based on its usecase — just add the relevant details to the form and save your check.
Yes
/No
) verdict.Define a basic orchestration logic for your Guardrail here.
Guardrail is created to validate ONLY ONE OF the Input
or the Output
. The Actions set here will also apply only to either the request
or the response
.
Action | State | Description | Impact |
---|---|---|---|
Async | TRUE This is the default state | Run the Guardrail checks asynchronously along with the LLM request. | Will add no latency to your requestUseful when you only want to log guardrail checks without affecting the request |
Async | FALSE | On Request Run the Guardrail check BEFORE sending the request to the LLM On Response Run the Guardrail check BEFORE sending the response to the user | Will add latency to the requestUseful when your Guardrail critical and you want more orchestration over your request based on the Guardrail result |
Deny | TRUE | On Request & Response If any of the Guardrail checks FAIL, the request will be killed with a 446 status code. If all of the Guardrail checks SUCCEED, the request/response will be sent further with a 200 status code. | This is useful when your Guardrails are critical and upon them failing, you can not run the requestWe would advice running this action on a subset of your requests to first see the impact |
Deny | FALSE This is the default state | On Request & Response If any of the Guardrail checks FAIL, the request will STILL be sent, but with a 246 status code. If all of the Guardrail checks SUCCEED, the request/response will be sent further with a 200 status code. | This is useful when you want to log the Guardrail result but do not want it to affect your result |
On Success | Send Feedback | If all of the Guardrail checks PASS, append your custom defined feedback to the request | We recommend setting up this actionThis will help you build an “Evals dataset” of Guardrail results on your requests over time |
On Failure | Send Feedback | If any of the Guardrail checks FAIL, append your custom feedback to the request | We recommend setting up this actionThis will help you build an “Evals dataset” of Guardrail results on your requests over time |
Set the relevant actions you want with your checks, name your Guardrail and save it! When you save the Guardrail, you will get an associated $Guardrail_ID
that you can then add to your request.
This is where Portkey’s magic comes into play. The Guardrail you created above is yet not an Active
guardrail because it is not attached to any request.
Configs is one of Portkey’s most powerful features and is used to define all kinds of request orchestration - everything from caching, retries, fallbacks, timeouts, to load balancing.
Now, you can use Configs to add Guardrail checks & actions to your request.
Portkey now offers a more intuitive way to add guardrails to your configurations:
Config Key | Value | Description |
---|---|---|
input_guardrails | ["guardrails-id-xxx", "guardrails-id-yyy"] | Apply these guardrails to the INPUT before sending to LLM |
output_guardrails | ["guardrails-id-xxx", "guardrails-id-yyy"] | Apply these guardrails to the OUTPUT from the LLM |
You can also continue to use the original hook-based approach:
Type | Config Key | Value | Description |
---|---|---|---|
Before Request Hook | before_request_hooks | [{"id":"$guardrail_id"}] | These hooks run on the INPUT before sending to the LLM |
After Request Hook | after_request_hooks | [{"id":"$guardrail_id"}] | These hooks run on the OUTPUT after receiving from the LLM |
Both configuration approaches work identically - choose whichever is more intuitive for your team. The simplified input_guardrails
and output_guardrails
fields are recommended for better readability.
For asynchronous guardrails (async= TRUE
), Portkey returns the standard, default status codes from the LLM providers — this is because the Guardrails verdict is not affecting how you orchestrate your requests. Portkey will only log the Guardrail result for you.
But for synchronous requests (async= FALSE
), Portkey can orchestrate your requests based on the Guardrail verdict. The behaviour is dependent on the following:
PASS
or FAIL
) ANDTRUE
or FALSE
)Portkey sends different request status codes
corresponding to your set Guardrail behaviour.
For requests where async= FALSE
:
Guardrail Verdict | DENY Setting | Returned Status Code | Description |
---|---|---|---|
PASS | FALSE | 200 | Guardrails have passed, request will be processed regardless |
PASS | TRUE | 200 | Guardrails have passed, request will be processed regardless |
FAIL | FALSE | 246 | Guardrails have failed, but the request should still **be processed.**Portkey introduces a new Status code to indicate this state. |
FAIL | TRUE | 446 | Guardrails have failed, and the request should not **be processed.**Portkey introduces a new Status code to indicate this state. |
246
& 446
Status CodesCreate these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. More here.
Now, while instantiating your Portkey client or while sending headers, just pass the Config ID.
For more, refer to the Config documentation.
Portkey Logs will show you detailed information about Guardrail results for each request.
Feedback & Guardrails
tab on the log drawer, you can seepassed
and how many failed
Portkey will also show the feedback object logged for each request
Value
:** The numerical feedback value you passedWeight
: The numerical feedback weightMetadata Key & Value
:** Any custom metadata sent with the feedbacksuccessfulChecks
: Which checks associated with this request passed
failedChecks
: Which checks associated with this request failed
erroredChecks
: If there were any checks that errored out along the wayWhen Guardrails are enabled and configured to run synchronously (async=false
), Portkey adds a hook_results
object to your API responses. This object provides detailed information about the guardrail checks that were performed and their outcomes.
The hook_results
object contains two main sections:
Each section contains an array of guardrail execution results, structured as follows:
Here’s a simplified example of what the hook_results
might look like in a response:
This example corresponds to a config
like:
async=true
), the hook_results
will not appear in the API response. The results will only be available in the Portkey logs.data
field varies based on the type of guardrail check being performed. Each guardrail type returns different information relevant to its function.verdict
for a guardrail is true
only if all individual checks pass. If any check fails, the verdict will be false
.transformed
is true
, it indicates that the guardrail has modified the content (such as redacting PII).deny
is true
and the verdict is false
, the request will be denied with a 446 status code.data
field. For example, a sentence count check provides information about the number of sentences, while a PII check might provide information about detected PII entities.metadata
object within feedback
keeps track of which checks were successful, failed, or encountered errors.On Portkey, you can also create the Guardrails in code and add them to your Configs. Read more about this here:
If you already have a custom guardrail pipeline where you send your inputs/outputs for evaluation, you can integrate it with Portkey using a modular, custom webhook! Read more here:
By appropriately configuring Guardrail Actions, you can maintain the integrity and reliability of your AI app, ensuring that only safe and compliant requests are processed.
To enable Guardrails for your org, ping us on the Portkey Discord