You can manage the lifecycle of a custom policy using any of the following:
- Konnect UI
- AI Gateway API with the
/v1/ai-gateways/{aiGatewayId}/custom-policies endpoint
- kongctl
A custom AI policy configuration is defined by the following fields:
|
Field
|
Type
|
Notes
|
name
|
string
|
Unique identifier. Immutable after creation. Creating a duplicate returns a 409 error.
|
type
|
installed or streaming
|
Discriminator. Determines whether handler is required.
|
display_name
|
string
|
1-256 characters.
|
schema
|
string
|
Lua schema, equivalent to a plugin’s schema.lua.
|
handler
|
string
|
Lua handler, equivalent to a plugin’s handler.lua. Required for streaming. Disallowed for installed.
|
id, created_at, updated_at
|
-
|
Server-assigned.
|
To get a paginated list of existing custom policies, use the /v1/ai-gateways/{aiGatewayId}/custom-policies endpoint:
curl -X GET "https://us.api.konghq.com/v1/ai-gateways/$AI_GATEWAY_ID/custom-policies" \
--no-progress-meter --fail-with-body \
-H "Authorization: Bearer $KONNECT_TOKEN"\
-H "Accept: application/json, application/problem+json"
To fetch a custom policy by name or id, use the /v1/ai-gateways/{aiGatewayId}/custom-policies/{name|id} endpoint:
curl -X GET "https://us.api.konghq.com/v1/ai-gateways/$AI_GATEWAY_ID/custom-policies/my-streaming-custom-policy" \
--no-progress-meter --fail-with-body \
-H "Authorization: Bearer $KONNECT_TOKEN"\
-H "Accept: application/json, application/problem+json"
To create a custom policy in streaming mode:
curl -X POST https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/custom-policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "my-streaming-custom-policy",
"type": "streaming",
"display_name": "Custom Policy - Streaming plugin",
"schema": "'$LUA_SCHEMA'",
"handler": "'$LUA_HANDLER'"
}
'
ai_gateway_custom_policies:
- ref: my-streaming-custom-policy
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-streaming-custom-policy
type: streaming
display_name: Custom Policy - Streaming plugin
schema: !env LUA_SCHEMA
handler: !env LUA_HANDLER
Make sure to replace the following placeholders with your own values:
-
AI_GATEWAY_ID: The id of your AI Gateway.
-
!env LUA_SCHEMA: Your Lua schema
-
!env LUA_HANDLER: Your plugin handler.
To create a custom policy in installed mode:
curl -X POST https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/custom-policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "my-installed-custom-policy",
"type": "installed",
"display_name": "Custom Policy - Installed plugin",
"schema": "'$LUA_SCHEMA'"
}
'
ai_gateway_custom_policies:
- ref: my-installed-custom-policy
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-installed-custom-policy
type: installed
display_name: Custom Policy - Installed plugin
schema: !env LUA_SCHEMA
Make sure to replace the following placeholders with your own values:
Including a handler in installed mode or omitting it in streaming mode results in an error.
To update an existing custom policy, redeploy the custom_policy entity with the same name:
curl -X POST https://{region}.api.konghq.com/v1/ai-gateways/{AIGatewayId}/custom-policies \
--header "accept: application/json" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $KONNECT_TOKEN" \
--data '
{
"name": "my-streaming-custom-policy",
"type": "streaming",
"display_name": "Custom Policy - Streaming plugin (updated)",
"schema": "'$LUA_SCHEMA'",
"handler": "'$LUA_HANDLER'"
}
'
ai_gateway_custom_policies:
- ref: my-streaming-custom-policy
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-streaming-custom-policy
type: streaming
display_name: Custom Policy - Streaming plugin (updated)
schema: !env LUA_SCHEMA
handler: !env LUA_HANDLER
Make sure to replace the following placeholders with your own values:
-
AI_GATEWAY_ID: The id of your AI Gateway.
-
!env LUA_SCHEMA: Your Lua schema
-
!env LUA_HANDLER: Your plugin handler.
The name field is immutable. You must include the current name when updating a custom policy.
To delete a custom policy by name or id, use the /v1/ai-gateways/{aiGatewayId}/custom-policies/{name|id} endpoint:
curl -X DELETE "https://us.api.konghq.com/v1/ai-gateways/$AI_GATEWAY_ID/custom-policies/my-streaming-custom-policy" \
--no-progress-meter --fail-with-body \
-H "Authorization: Bearer $KONNECT_TOKEN"\
-H "Accept: application/json, application/problem+json"