Create Cache Policy
POST /environments/{env_id}/cache_policies
POST /environments/{env_id}/cache_policiesInteractive request editor loads with JavaScript.
Authorization
BearerAdminToken Organization-scoped admin token. Read operations require a valid token, and write operations require a token with write permission.
In: header
Path Parameters
Environment ID that scopes the request.
uuidRequest Body
application/json
Create a cache policy. The name and backend are fixed after creation; omitted optional fields use the documented server defaults.
Response Body
application/json
application/json
application/json
application/json
application/json
application/json
curl -X POST "$AISIX_CP/environments/$ENV_ID/cache_policies" \ -H "Authorization: Bearer $AISIX_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "name": "string" }'Created
{
"cache_policy": {
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"env_id": "3f917d0d-4dff-42f3-97c4-6a5455dfe8c2",
"name": "string",
"enabled": true,
"backend": "memory",
"ttl_seconds": 0,
"applies_to": "string",
"scope": "api_key",
"purge_generation": 0,
"semantic": {
"embedding_model_id": "2959178f-981f-441b-a0b6-cedb1eb6201d",
"threshold": 0,
"max_entries": 1,
"embedding_timeout_ms": 0
},
"created_at": "2019-08-24T14:15:22Z",
"updated_at": "2019-08-24T14:15:22Z"
},
"warnings": [
{
"code": "field_ignored",
"field": "string",
"min_dp_version": "string",
"affected_dp_count": 0,
"affected_dp_versions": [
"string"
]
}
]
}Request body or parameters failed validation.
{
"error": {
"code": "string",
"message": "string"
}
}Missing or invalid bearer.
{
"error": {
"code": "UNAUTHENTICATED",
"message": "no org context"
}
}Authenticated request is not permitted.
{
"error": {
"code": "CUSTOM_KEY_DISABLED",
"message": "custom API key values are disabled for this organization"
}
}Resource not found.
{
"error": {
"code": "NOT_FOUND",
"message": "provider key not found"
}
}Request conflicts with current resource state.
{
"error": {
"code": "CREDENTIAL_IN_USE",
"message": "models still reference this provider_key"
}
}Complete operation details and schema variants
POST /environments/{env_id}/cache_policies
Create Cache Policy
Create a prompt-response cache rule in an environment. Policy names are unique within the environment, and the name and backend cannot be changed after creation.
Parameters
env_id(path, string, required): Environment ID that scopes the request.
Request body
Content type: application/json
-
name(string, required): Human-readable policy name, unique within the environment. -
enabled(boolean, optional): Policies are created enabled unless this isfalse. -
backend(string, optional): Where cached responses are stored.memorykeeps an in-process cache on each gateway instance.redisuses the shared Redis configured on the gateway (cache.redis); a gateway without one disables caching for the policy's requests and logs a warning. Fixed at creation. -
ttl_seconds(integer, optional): How long a cached response stays servable, in seconds. Defaults to3600(1 hour); the ceiling is 7 days. To stop serving cached responses, disable the policy rather than setting a zero TTL. -
applies_to(string, optional): Which traffic the policy covers:allfor every request in the environment,model:<model alias>for one model, orapi_key:<key id>for one caller API key. Defaults toall. -
scope(string, optional): Sharing boundary for entries created under a policy, applied to both exact and semantic matching.api_key(the default) keeps entries private to the API key that created them — one caller's answers are never replayed to another.envshares entries across every API key in the environment; pick it for shared-knowledge traffic (FAQ, documentation Q&A) where cross-caller reuse is the point. -
semantic(object, optional): Embedding-similarity matching for a cache policy. When configured, a request that misses exact matching is embedded and compared against stored entries; the nearest entry at or abovethresholdcosine similarity is served. Only fully textual requests participate — requests containing images, audio, or tool calls always take the exact path. On theredisbackend this additionally requires a Redis server with vector search (Redis 8 or later, or the search module); the gateway probes support at startup and keeps the policy exact-only when it is missing. -
semantic.embedding_model_id(string, required): ID of the embedding model used to embed requests. Must reference a model of kindembeddingin the same environment. -
semantic.threshold(number, required): Minimum cosine similarity for a stored entry to be served. Higher is stricter; values below0.9noticeably increase wrong-answer risk for most embedding models. -
semantic.max_entries(integer, optional): Upper bound on stored entries for this policy on thememorybackend (oldest evicted first). Defaults to1000. Theredisbackend bounds growth by TTL and ignores this value. -
semantic.embedding_timeout_ms(integer, optional): Per-call deadline for the embedding request in milliseconds.0or omitted disables the embedding-specific deadline. On timeout the request proceeds to the upstream uncached.
Responses
-
201: Created -
cache_policy(object, required): A prompt-response cache rule. When an enabled policy covers a request, the gateway serves an identical repeated request from cache instead of calling the upstream provider. -
cache_policy.id(string, required): Cache policy ID. -
cache_policy.env_id(string, required): Environment ID that owns this policy. -
cache_policy.name(string, required): Human-readable policy name, unique within the environment. Fixed at creation. -
cache_policy.enabled(boolean, required): Disabled policies are kept but the gateway no longer consults them. -
cache_policy.backend(string, required): Where cached responses are stored.memorykeeps an in-process cache on each gateway instance.redisuses the shared Redis configured on the gateway (cache.redis); a gateway without one disables caching for the policy's requests and logs a warning. Fixed at creation. -
cache_policy.ttl_seconds(integer, required): How long a cached response stays servable, in seconds. -
cache_policy.applies_to(string, required): Which traffic the policy covers:allfor every request in the environment,model:<model alias>for one model, orapi_key:<key id>for one caller API key. -
cache_policy.scope(string, required): Sharing boundary for entries created under a policy, applied to both exact and semantic matching.api_key(the default) keeps entries private to the API key that created them — one caller's answers are never replayed to another.envshares entries across every API key in the environment; pick it for shared-knowledge traffic (FAQ, documentation Q&A) where cross-caller reuse is the point. -
cache_policy.purge_generation(integer, required): Server-managed invalidation counter. Each purge increments it, making every entry created earlier unreachable at once. Read-only — see the purge operation. -
cache_policy.semantic(object, optional): Embedding-similarity matching for a cache policy. When configured, a request that misses exact matching is embedded and compared against stored entries; the nearest entry at or abovethresholdcosine similarity is served. Only fully textual requests participate — requests containing images, audio, or tool calls always take the exact path. On theredisbackend this additionally requires a Redis server with vector search (Redis 8 or later, or the search module); the gateway probes support at startup and keeps the policy exact-only when it is missing. -
cache_policy.semantic.embedding_model_id(string, required): ID of the embedding model used to embed requests. Must reference a model of kindembeddingin the same environment. -
cache_policy.semantic.threshold(number, required): Minimum cosine similarity for a stored entry to be served. Higher is stricter; values below0.9noticeably increase wrong-answer risk for most embedding models. -
cache_policy.semantic.max_entries(integer, optional): Upper bound on stored entries for this policy on thememorybackend (oldest evicted first). Defaults to1000. Theredisbackend bounds growth by TTL and ignores this value. -
cache_policy.semantic.embedding_timeout_ms(integer, optional): Per-call deadline for the embedding request in milliseconds.0or omitted disables the embedding-specific deadline. On timeout the request proceeds to the upstream uncached. -
cache_policy.created_at(string, required): Time when the policy was created. -
cache_policy.updated_at(string, required): Time when the policy was last updated. -
warnings(array, optional): Present on save and purge operations when some data plane nodes in the environment run a gateway version that will not execute part of the saved policy — its sharing scope, semantic block, or purge generation. Never present on reads. -
warnings[].code(string, required): How affected nodes treat the saved resource.field_ignored: the resource stays in service on those nodes but the named field has no effect there — beware when the field is restrictive, since the restriction is silently not enforced.row_rejected: those nodes refuse the whole resource, so it stops serving there entirely (or, if a previously accepted version exists, they keep serving the old values). -
warnings[].field(string, required): Dot-separated path of the field that triggered the warning, as delivered to gateway nodes. -
warnings[].min_dp_version(string, required): Oldest gateway version that fully executes the field. The literal valueunreleasedmeans no released gateway version executes it yet. -
warnings[].affected_dp_count(integer, required): Number of currently registered nodes in the environment running an older gateway version thanmin_dp_version. -
warnings[].affected_dp_versions(array, required): Distinct gateway versions reported by the affected nodes, capped at five entries. Nodes that report no version appear asunknown. -
400: Request body or parameters failed validation. -
error(object, required): Error details. -
error.code(string, required): Machine-readable identifier. Examples:UNAUTHENTICATED,INVALID_REQUEST,MASTER_KEY_UNSET,INTERNAL. -
error.message(string, required): Human-readable explanation; safe to surface to operators. -
401: Missing or invalid bearer. Response fields (application/json): identical toPOST /environments/{env_id}/cache_policies, response 400 above. -
403: Authenticated request is not permitted. Response fields (application/json): identical toPOST /environments/{env_id}/cache_policies, response 400 above. -
404: Resource not found. Response fields (application/json): identical toPOST /environments/{env_id}/cache_policies, response 400 above. -
409: Request conflicts with current resource state. Response fields (application/json): identical toPOST /environments/{env_id}/cache_policies, response 400 above.
cURL
curl -X POST '$AISIX_CP/environments/{env_id}/cache_policies' -H 'Authorization: Bearer $AISIX_TOKEN'