Available on Enterprise Self Hosting plan only.Requires 1.17.0 or higher version of the Gateway.
Policies
- Policies allow organisations to apply flexible budget and rate limit controls based on dynamic conditions (API keys, metadata, workspace, etc.).
-
There are two types of policies:
- Usage Limits Policies: Control the total usage (cost or tokens) over a period
- Rate Limits Policies: Control the rate of requests or tokens per minute/hour/day
Policy Structure
Both policy types share common concepts:Conditions
Conditions define which requests the policy applies to. Each condition has akey and value:
Valid keys:
api_key- Apply to a specific API keyworkspace_id- Apply to a workspacemetadata.*- Apply based on custom metadata fields (e.g.,metadata._user,metadata.team)
Group By
Group by defines how usage is aggregated. Each group entry has akey:
Valid keys:
api_key- Group by API keyworkspace_id- Group by workspacemetadata.*- Group by custom metadata fields
Authentication
All policy endpoints require authentication. You can authenticate using:- API Key: Include in
x-portkey-api-keyheader
Permissions
Policies require the following RBAC permissions:policies:create- Create policiespolicies:read- Read policiespolicies:update- Update policiespolicies:delete- Delete policiespolicies:list- List policies
Base URL
All policy endpoints are under:Usage Limits Policies
Usage limits policies allow you to set maximum usage (cost or tokens) that can be consumed over a period. When the limit is reached, requests will be blocked until the limit resets.Policy Types
Usage limits policies support two types:cost: Limit based on total cost (in dollars)tokens: Limit based on total tokens consumed
Usage Limits Policies
Usage limits policies allow you to set maximum usage (cost or tokens) that can be consumed over a period. When the limit is reached, requests will be blocked until the limit resets.Policy Types
Usage limits policies support two types:cost: Limit based on total cost (in dollars)tokens: Limit based on total tokens consumed
Parameters
Usage limits policies support the following parameters:credit_limit (required)
The maximum usage limit that can be consumed before requests are blocked.
- Type: Number (integer or float)
- Minimum value:
- For
costtype:1(represents $1.00) - For
tokenstype:100tokens
- For
- Units:
- For
costtype: Value is in USD (dollars) - For
tokenstype: Value is in tokens
- For
- Behavior: When the credit limit is reached, all matching requests will be blocked until the limit resets (if
periodic_resetis configured) or the policy is updated
alert_threshold (optional)
An optional threshold that triggers notifications before the credit limit is reached.
- Type: Number (integer or float)
- Minimum value:
1 - Units:
- For
costtype: Value is in USD (dollars) - For
tokenstype: Value is in tokens
- For
- Validation: Must be less than
credit_limitif provided - Behavior:
- When usage reaches this threshold, email notifications are sent to configured recipients
- The API key continues to function normally until the full
credit_limitis reached - Useful for proactive monitoring and budget management
periodic_reset (optional)
Configures automatic reset of the usage limit at regular intervals.
- Type: String (enum)
- Valid values:
"weekly"- Budget limits automatically reset every week"monthly"- Budget limits automatically reset every month- Omitted/not provided - No periodic reset (limit applies until exhausted)
- Reset timing:
- Weekly: Resets occur every Monday at 12:00 AM UTC
- Monthly: Resets occur on the 1st calendar day of each month at 12:00 AM UTC, regardless of when the policy was created
- Behavior: When a reset occurs, the usage counter resets to zero and the limit becomes available again
Validation Rules
- Conditions: Must be a non-empty array. Each condition must have
keyandvaluefields. - Group By: Must be a non-empty array. Each group must have a
keyfield. - Valid Keys: For both
conditionsandgroup_by, valid keys are:api_keyworkspace_id- Any key starting with
metadata.(e.g.,metadata._user)
- Alert Threshold: Must be less than
credit_limitif provided. - Workspace: Workspace ID is required (can be provided via API key or request body).
Examples
1: Monthly Cost Limit per API Key
Limit each API key to $1000 per month:2: Token Limit by User
Limit tokens per user (via metadata):Rate Limits Policies
Rate limits policies allow you to control the rate of requests or tokens consumed per minute, hour, or day. When the rate limit is exceeded, requests will be throttled.Policy Types
Rate limits policies support two types:requests: Limit based on number of requeststokens: Limit based on number of tokens
Rate Units
Rate limits can be specified in three units:rpm: Requests/Tokens per minuterph: Requests/Tokens per hourrpd: Requests/Tokens per day
Parameters
Rate limits policies support the following parameters:type (required)
The type of rate limit to enforce.
- Type: String (enum)
- Valid values:
"requests"- Limit based on number of API requests"tokens"- Limit based on number of tokens consumed
- Behavior: Determines what metric is being rate-limited
unit (required)
The time interval unit for the rate limit.
- Type: String (enum)
- Valid values:
"rpm"- Requests/Tokens per minute"rph"- Requests/Tokens per hour"rpd"- Requests/Tokens per day
- Behavior:
- Defines the time window over which the rate limit is calculated
- Limits reset automatically at the start of each time period
- Per Minute: Limits reset every minute, ideal for fine-grained control
- Per Hour: Limits reset hourly, providing balanced usage control
- Per Day: Limits reset daily, suitable for broader usage patterns
value (required)
The maximum number of requests or tokens allowed within the specified time unit.
- Type: Number (integer)
- Minimum value:
1(setting to0effectively disables the policy) - Units:
- For
requeststype: Value represents the number of API requests - For
tokenstype: Value represents the number of tokens
- For
- Behavior:
- When the rate limit is exceeded, subsequent requests are throttled/rejected until the time period resets
- The limit applies immediately after the policy is created
- Rate limits are enforced on a rolling window basis within the specified time unit
Validation Rules
- Conditions: Must be a non-empty array. Each condition must have
keyandvaluefields. - Group By: Must be a non-empty array. Each group must have a
keyfield. - Valid Keys: For both
conditionsandgroup_by, valid keys are:api_keyworkspace_id- Any key starting with
metadata.(e.g.,metadata._user)
- Value: Must be a numeric value.
- Workspace: Workspace ID is required (can be provided via API key or request body).

