ai-request-rewrite
The ai-request-rewrite plugin processes client requests by forwarding them to LLM services for transformation before relaying them to upstream services. This enables LLM-powered modifications such as data redaction, content enrichment, or reformatting. The plugin supports the integration with OpenAI, DeepSeek, Gemini, Vertex AI, Anthropic, OpenRouter, and other OpenAI-compatible APIs.
The LLM call used for rewriting is separate from the client's request format. With the openai provider, the plugin builds a non-streaming Chat Completions request from the configured prompt and the original request body. It does not classify or proxy the client's request as an AI protocol. That internal request carries the plugin's configured provider credentials and does not forward client headers.
Examples
The examples below demonstrate how you can configure ai-request-rewrite for different scenarios.
The examples will use OpenAI as the LLM service. To follow along, obtain the OpenAI API key and save it to an environment variable:
export OPENAI_API_KEY=YOUR_LLM_API_KEY # replace with your API key
Redact Sensitive Information
The following example demonstrates how you can use the ai-request-rewrite plugin to redact sensitive information before the request reaches the upstream service.
- Admin API
- ADC
- Ingress Controller
Create a route and configure the ai-request-rewrite plugin as such:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-request-rewrite-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-request-rewrite": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer $OPENAI_API_KEY"
}
},
"options":{
"model": "gpt-4"
},
"prompt": "Given a JSON request body, identify and mask any sensitive information such as credit card numbers, social security numbers, and personal identification numbers (e.g., passport or driver's license numbers). Replace detected sensitive values with a masked format (e.g., \"*** **** **** 1234\") for credit card numbers. Ensure the JSON structure remains unchanged."
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF
❶ Specify the provider to be openai.
❷ Attach OpenAI API key in the Authorization header.
❸ Specify the name of the model.
❹ Specify what information to redact before the request reaches the upstream service.
Create a route with the ai-request-rewrite plugin configured as such:
services:
- name: ai-request-rewrite-service
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
routes:
- name: ai-request-rewrite-route
uris:
- /anything
methods:
- POST
plugins:
ai-request-rewrite:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4
prompt: "Given a JSON request body, identify and mask any sensitive information such as credit card numbers, social security numbers, and personal identification numbers (e.g., passport or driver's license numbers). Replace detected sensitive values with a masked format (e.g., \"*** **** **** 1234\") for credit card numbers. Ensure the JSON structure remains unchanged."
Preview the changes and confirm that they only affect resources for this guide:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=ai-request-rewrite
Synchronize the configuration to the gateway:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=ai-request-rewrite
❶ Specify the provider to be openai.
❷ Attach OpenAI API key in the Authorization header.
❸ Specify the name of the model.
❹ Specify what information to redact before the request reaches the upstream service.
- Gateway API
- APISIX CRD
Create a route with the ai-request-rewrite plugin configured as such:
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: ai-request-rewrite-plugin-config
spec:
plugins:
- name: ai-request-rewrite
config:
provider: openai
auth:
header:
Authorization: "Bearer YOUR_LLM_API_KEY"
options:
model: gpt-4
prompt: "Given a JSON request body, identify and mask any sensitive information such as credit card numbers, social security numbers, and personal identification numbers (e.g., passport or driver's license numbers). Replace detected sensitive values with a masked format (e.g., \"*** **** **** 1234\") for credit card numbers. Ensure the JSON structure remains unchanged."
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: ai-request-rewrite-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-request-rewrite-plugin-config
backendRefs:
- name: httpbin-external-domain
port: 80
Apply the configuration to your cluster:
kubectl apply -f ai-request-rewrite-ic.yaml
❶ Specify the provider to be openai.
❷ Attach OpenAI API key in the Authorization header.
❸ Specify the name of the model.
❹ Specify what information to redact before the request reaches the upstream service.
Create a route with the ai-request-rewrite plugin configured as such:
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: aic
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: ai-request-rewrite-route
spec:
ingressClassName: apisix
http:
- name: ai-request-rewrite-route
match:
paths:
- /anything
methods:
- POST
upstreams:
- name: httpbin-external-domain
plugins:
- name: ai-request-rewrite
enable: true
config:
provider: openai
auth:
header:
Authorization: "Bearer YOUR_LLM_API_KEY"
options:
model: gpt-4
prompt: "Given a JSON request body, identify and mask any sensitive information such as credit card numbers, social security numbers, and personal identification numbers (e.g., passport or driver's license numbers). Replace detected sensitive values with a masked format (e.g., \"*** **** **** 1234\") for credit card numbers. Ensure the JSON structure remains unchanged."
Apply the configuration to your cluster:
kubectl apply -f ai-request-rewrite-ic.yaml
❶ Specify the provider to be openai.
❷ Attach OpenAI API key in the Authorization header.
❸ Specify the name of the model.
❹ Specify what information to redact before the request reaches the upstream service.
Send a POST request to the route with some personally identifiable information:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"content": "John said his debit card number is 4111 1111 1111 1111 and SIN is 123-45-6789."
}'
You should receive a response similar to the following:
{
"args": {},
"data": "{\n \"content\": \"John said his debit card number is **** **** **** 1111 and SIN is ***-**-****.\"\n }",
"json": {
"content": "John said his debit card number is **** **** **** 1111 and SIN is ***-**-****."
},
"method": "POST",
"origin": "192.168.97.1, 103.97.2.170",
"url": "http://127.0.0.1/anything"
}
Reformat Data
The following example demonstrates how you can use the ai-request-rewrite plugin to reformat data before the request reaches the upstream service.
- Admin API
- ADC
- Ingress Controller
Create a route and configure the ai-request-rewrite plugin as such:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "ai-request-rewrite-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-request-rewrite": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer $OPENAI_API_KEY"
}
},
"options":{
"model": "gpt-4"
},
"prompt": "Convert natural language queries into structured JSON format with intent and extracted parameters."
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}
EOF
❶ Specify the provider to be openai.
❷ Attach OpenAI API key in the Authorization header.
❸ Specify the name of the model.
❹ Specify how to reformat before the request reaches the upstream service.
Create a route with the ai-request-rewrite plugin configured as such:
services:
- name: ai-request-rewrite-service
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
routes:
- name: ai-request-rewrite-route
uris:
- /anything
methods:
- POST
plugins:
ai-request-rewrite:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4
prompt: "Convert natural language queries into structured JSON format with intent and extracted parameters."
Preview the changes and confirm that they only affect resources for this guide:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=ai-request-rewrite
Synchronize the configuration to the gateway:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=ai-request-rewrite