Portkey Docs
HomeAPIIntegrationsChangelog
  • Introduction
    • What is Portkey?
    • Make Your First Request
    • Feature Overview
  • Integrations
    • LLMs
      • OpenAI
        • Structured Outputs
        • Prompt Caching
      • Anthropic
        • Prompt Caching
      • Google Gemini
      • Groq
      • Azure OpenAI
      • AWS Bedrock
      • Google Vertex AI
      • Bring Your Own LLM
      • AI21
      • Anyscale
      • Cerebras
      • Cohere
      • Fireworks
      • Deepbricks
      • Deepgram
      • Deepinfra
      • Deepseek
      • Google Palm
      • Huggingface
      • Inference.net
      • Jina AI
      • Lingyi (01.ai)
      • LocalAI
      • Mistral AI
      • Monster API
      • Moonshot
      • Nomic
      • Novita AI
      • Ollama
      • OpenRouter
      • Perplexity AI
      • Predibase
      • Reka AI
      • SambaNova
      • Segmind
      • SiliconFlow
      • Stability AI
      • Together AI
      • Voyage AI
      • Workers AI
      • ZhipuAI / ChatGLM / BigModel
      • Suggest a new integration!
    • Agents
      • Autogen
      • Control Flow
      • CrewAI
      • Langchain Agents
      • LlamaIndex
      • Phidata
      • Bring Your own Agents
    • Libraries
      • Autogen
      • DSPy
      • Instructor
      • Langchain (Python)
      • Langchain (JS/TS)
      • LlamaIndex (Python)
      • LibreChat
      • Promptfoo
      • Vercel
        • Vercel [Depricated]
  • Product
    • Observability (OpenTelemetry)
      • Logs
      • Tracing
      • Analytics
      • Feedback
      • Metadata
      • Filters
      • Logs Export
      • Budget Limits
    • AI Gateway
      • Universal API
      • Configs
      • Multimodal Capabilities
        • Image Generation
        • Function Calling
        • Vision
        • Speech-to-Text
        • Text-to-Speech
      • Cache (Simple & Semantic)
      • Fallbacks
      • Automatic Retries
      • Load Balancing
      • Conditional Routing
      • Request Timeouts
      • Canary Testing
      • Virtual Keys
        • Budget Limits
    • Prompt Library
      • Prompt Templates
      • Prompt Partials
      • Retrieve Prompts
      • Advanced Prompting with JSON Mode
    • Guardrails
      • List of Guardrail Checks
        • Patronus AI
        • Aporia
        • Pillar
        • Bring Your Own Guardrails
      • Creating Raw Guardrails (in JSON)
    • Autonomous Fine-tuning
    • Enterprise Offering
      • Org Management
        • Organizations
        • Workspaces
        • User Roles & Permissions
        • API Keys (AuthN and AuthZ)
      • Access Control Management
      • Budget Limits
      • Security @ Portkey
      • Logs Export
      • Private Cloud Deployments
        • Architecture
        • AWS
        • GCP
        • Azure
        • Cloudflare Workers
        • F5 App Stack
      • Components
        • Log Store
          • MongoDB
    • Open Source
    • Portkey Pro & Enterprise Plans
  • API Reference
    • Introduction
    • Authentication
    • OpenAPI Specification
    • Headers
    • Response Schema
    • Gateway Config Object
    • SDK
  • Provider Endpoints
    • Supported Providers
    • Chat
    • Embeddings
    • Images
      • Create Image
      • Create Image Edit
      • Create Image Variation
    • Audio
      • Create Speech
      • Create Transcription
      • Create Translation
    • Fine-tuning
      • Create Fine-tuning Job
      • List Fine-tuning Jobs
      • Retrieve Fine-tuning Job
      • List Fine-tuning Events
      • List Fine-tuning Checkpoints
      • Cancel Fine-tuning
    • Batch
      • Create Batch
      • List Batch
      • Retrieve Batch
      • Cancel Batch
    • Files
      • Upload File
      • List Files
      • Retrieve File
      • Retrieve File Content
      • Delete File
    • Moderations
    • Assistants API
      • Assistants
        • Create Assistant
        • List Assistants
        • Retrieve Assistant
        • Modify Assistant
        • Delete Assistant
      • Threads
        • Create Thread
        • Retrieve Thread
        • Modify Thread
        • Delete Thread
      • Messages
        • Create Message
        • List Messages
        • Retrieve Message
        • Modify Message
        • Delete Message
      • Runs
        • Create Run
        • Create Thread and Run
        • List Runs
        • Retrieve Run
        • Modify Run
        • Submit Tool Outputs to Run
        • Cancel Run
      • Run Steps
        • List Run Steps
        • Retrieve Run Steps
    • Completions
    • Gateway for Other API Endpoints
  • Portkey Endpoints
    • Configs
      • Create Config
      • List Configs
      • Retrieve Config
      • Update Config
    • Feedback
      • Create Feedback
      • Update Feedback
    • Guardrails
    • Logs
      • Insert a Log
      • Log Exports [BETA]
        • Retrieve a Log Export
        • Update a Log Export
        • List Log Exports
        • Create a Log Export
        • Start a Log Export
        • Cancel a Log Export
        • Download a Log Export
    • Prompts
      • Prompt Completion
      • Render
    • Virtual Keys
      • Create Virtual Key
      • List Virtual Keys
      • Retrieve Virtual Key
      • Update Virtual Key
      • Delete Virtual Key
    • Analytics
      • Graphs - Time Series Data
        • Get Requests Data
        • Get Cost Data
        • Get Latency Data
        • Get Tokens Data
        • Get Users Data
        • Get Requests per User
        • Get Errors Data
        • Get Error Rate Data
        • Get Status Code Data
        • Get Unique Status Code Data
        • Get Rescued Requests Data
        • Get Cache Hit Rate Data
        • Get Cache Hit Latency Data
        • Get Feedback Data
        • Get Feedback Score Distribution Data
        • Get Weighted Feeback Data
        • Get Feedback Per AI Models
      • Summary
        • Get All Cache Data
      • Groups - Paginated Data
        • Get User Grouped Data
        • Get Model Grouped Data
        • Get Metadata Grouped Data
    • API Keys [BETA]
      • Update API Key
      • Create API Key
      • Delete an API Key
      • Retrieve an API Key
      • List API Keys
    • Admin
      • Users
        • Retrieve a User
        • Retrieve All Users
        • Update a User
        • Remove a User
      • User Invites
        • Invite a User
        • Retrieve an Invite
        • Retrieve All User Invites
        • Delete a User Invite
      • Workspaces
        • Create Workspace
        • Retrieve All Workspaces
        • Retrieve a Workspace
        • Update Workspace
        • Delete a Workspace
      • Workspace Members
        • Add a Workspace Member
        • Retrieve All Workspace Members
        • Retrieve a Workspace Member
        • Update Workspace Member
        • Remove Workspace Member
  • Guides
    • Getting Started
      • A/B Test Prompts and Models
      • Tackling Rate Limiting
      • Function Calling
      • Image Generation
      • Getting started with AI Gateway
      • Llama 3 on Groq
      • Return Repeat Requests from Cache
      • Trigger Automatic Retries on LLM Failures
      • 101 on Portkey's Gateway Configs
    • Integrations
      • Llama 3 on Portkey + Together AI
      • Introduction to GPT-4o
      • Anyscale
      • Mistral
      • Vercel AI
      • Deepinfra
      • Groq
      • Langchain
      • Mixtral 8x22b
      • Segmind
    • Use Cases
      • Few-Shot Prompting
      • Enforcing JSON Schema with Anyscale & Together
      • Detecting Emotions with GPT-4o
      • Build an article suggestion app with Supabase pgvector, and Portkey
      • Setting up resilient Load balancers with failure-mitigating Fallbacks
      • Run Portkey on Prompts from Langchain Hub
      • Smart Fallback with Model-Optimized Prompts
      • How to use OpenAI SDK with Portkey Prompt Templates
      • Setup OpenAI -> Azure OpenAI Fallback
      • Fallback from SDXL to Dall-e-3
      • Comparing Top10 LMSYS Models with Portkey
      • Build a chatbot using Portkey's Prompt Templates
  • Support
    • Contact Us
    • Developer Forum
    • Common Errors & Resolutions
    • December '23 Migration
    • Changelog
Powered by GitBook
On this page
  • Using Guardrails
  • 1. Create a New Guardrail & Add Checks
  • 2. Add Guardrail Actions
  • There are 6 Types of Guardrail Actions
  • 3. "Enable" the Guardrails through Configs
  • Add Guardrail ID before the request OR after the request
  • Example Config with Guardrails
  • Guardrail Behaviour on the Gateway
  • Example Config Using the New 246 & 446 Status Codes
  • 4. Final Step - Attach Config to Request
  • Viewing Guardrail Results in Portkey Logs
  • On the Feedback & Guardrails tab on the log drawer, you can see
  • Defining Guardrails Directly in JSON
  • Bring Your Own Guardrails
  • Examples of When to Deny Requests with Guardrails

Was this helpful?

Edit on GitHub
  1. Product

Guardrails

Ship to production more confidently with Portkey Guardrails on your requests & responses

PreviousAdvanced Prompting with JSON ModeNextList of Guardrail Checks

Last updated 9 months ago

Was this helpful?

This feature is in beta and available to select users. To enable it for your org, ping us on the .

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.

Using Portkey's Guardrail platform, you can now verify your LLM inputs AND outputs to be adhering to your specifed checks; and since Guardrails are built on top of our , you can orchestrate your request exactly the way you want - 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.

Examples of Guardrails Portkey offers:

  • Regex match - Check if the request or response text matches a regex pattern

  • JSON Schema - Check if the response JSON matches a JSON schema

  • Contains Code - Checks if the content contains code of format SQL, Python, TypeScript, etc.

  • Custom guardrail - If you are running a custom guardrail currently, you can also integrate it with Portkey

  • ...and many 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.


Using Guardrails

Putting Portkey Guardrails in production is just a 4-step process:

  1. Create Guardrail Checks

  2. Create Guardrail Actions

  3. Enable Guardrail through Configs

  4. Attach the Config to a Request

This flowchart shows how Portkey processes a Guardrails request:

Let's see in detail below:


1. Create a New Guardrail & Add 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.

  • A check is an individual function that assesses the input prompt or output response against predefined conditions.

  • You can add as many checks as you want to a single Guardrail.

  • A check ONLY returns a boolean (Yes/No) verdict.


2. Add Guardrail Actions

This is where you will define a basic orchestration logic for your Guardrail.

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.

There are 6 Types of Guardrail Actions

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 request

  • Useful 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 request

  • Useful 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 request

  • We 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 action

  • This 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 action

  • This 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.


3. "Enable" the Guardrails through Configs

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.

Add Guardrail ID before the request OR after the request

Type
Config Key
Value
Description

Before Request Hook

before_request_hooks

[{"id":"$guardrail_id"}]

This key is used to run Guardrail checks & actions on the INPUT.

After Request Hook

after_request_hooks

[{"id":"$guardrail_id"}]

This key is used to run Guardrail checks & actions on the OUTPUT.

Example Config with Guardrails

{
	"retry": {
		"attempts": 3
	},
	"cache": {
		"mode": "simple"
	},
	"virtual_key":"openai-xxx",
	"before_request_hooks": [{
		"id": "input-guardrail-id-xx"
	}],
	"after_request_hooks": [{
		"id": "output-guardrail-id-xx"
	}]
}

Guardrail Behaviour on the Gateway

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:

  • Guardrail Check Verdict (PASS or FAIL) AND

  • Guardrail Action — DENY Setting (TRUE 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.

Example Config Using the New 246 & 446 Status Codes

{
	"strategy": {
		"mode": "fallback",
		"on_status_codes": [246,446]
	},
	"targets": [
		{"virtual_key": "openai-key-xxx"},
		{"virtual_key": "anthropic-key-xxx"}
	],
	"before_request_hooks": [
		{"id": "guardrails-id-xxx"}
	]
}
{
	"retry": {
		"on_status_codes": [246],
		"attempts": 5
	},
	"after_request_hooks": [
		{"id": "guardrails-id"}
	]
}

4. Final Step - Attach Config to Request

Now, while instantiating your Portkey client or while sending headers, just pass the Config ID.

const portkey = new Portkey({
    apiKey: "PORTKEY_API_KEY",
    config: "pc-***" // Supports a string config id or a config object
});
const portkey = Portkey(
    api_key="PORTKEY_API_KEY",
    config="pc-***" # Supports a string config id or a config object
)
const openai = new OpenAI({
  apiKey: 'OPENAI_API_KEY',
  baseURL: PORTKEY_GATEWAY_URL,
  defaultHeaders: createHeaders({
    apiKey: "PORTKEY_API_KEY",
    config: "CONFIG_ID"
  })
});
client = OpenAI(
    api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY")
    base_url=PORTKEY_GATEWAY_URL,
    default_headers=createHeaders(
        provider="openai",
        api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY")
        config="CONFIG_ID"
    )
)
curl https://api.portkey.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "x-portkey-api-key: $PORTKEY_API_KEY" \
  -H "x-portkey-config: $CONFIG_ID" \ 
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{
        "role": "user",
        "content": "Hello!"
      }]
  }'

Viewing Guardrail Results in Portkey Logs

Portkey Logs will show you detailed information about Guardrail results for each request.

On the Feedback & Guardrails tab on the log drawer, you can see

Guardrail Details

  • Overview: How many checks passed and how many failed

  • Verdict: Guardrail verdict for each of the checks in your Guardrail

  • Latency: Round trip time for each check in your Guardrail

Feedback Details

Portkey will also show the feedback object logged for each request

  • Value: The numerical feedback value you passed

  • Weight: The numerical feedback weight

  • Metadata Key & Value: Any custom metadata sent with the feedback

  • successfulChecks: 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 way


Defining Guardrails Directly in JSON

On Portkey, you can also create the Guardrails in code and add them to your Configs. Read more about this here:


Bring Your Own Guardrails

If you already have a custom guardrail pipeline where you send your inputs/outputs for evaluation, you can also integrate it with Portkey using a modular, custom webhook! Read more here:


Examples of When to Deny Requests with Guardrails

  1. Prompt Injection Checks: Preventing inputs that could alter the behavior of the AI model or manipulate its responses.

  2. Moderation Checks: Ensuring responses do not contain offensive, harmful, or inappropriate content.

  3. Compliance Checks: Verifying that inputs and outputs comply with regulatory requirements or organizational policies.

  4. Security Checks: Blocking requests that contain potentially harmful content, such as SQL injection attempts or cross-site scripting (XSS) payloads.

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.


Portkey also integrates with your favourite Guardrail platforms like , , and more. Just add their API keys to Portkey and you can enable their guardrails policies on your Portkey calls!

You can create these Configs in Portkey UI, save them, and get an associated Config ID you can attach to your requests. .

For more, refer to the .

Browse the full list of supported Guardrail checks here.
Aporia
SydeLabs
Pillar Security
More details on Guardrail Partners here.
Here is a list of all the Guardrail checks available on Portkey and what they do.
More here
Config documentation here
Creating Raw Guardrails (in JSON)
Bring Your Own Guardrails
Portkey Discord
Gateway
Portkey Guardrails