Skip to main content

ai-request-rewrite

Rewrites the request body with an LLM before proxying.

Category AIAdd it to Route, Service, Consumer, Consumer group, Global rule, Plugin configEngine plugin ai-request-rewritePriority 1073

What it does​

ai-request-rewrite sends the client's request body to a model together with the instruction under Prompt, and the model's answer replaces that body before the request goes on to the route's backend. The model is the one Provider names, reached with the credential under Auth.

The backend receives what the model wrote, not what the client sent, under the content type the client gave the original body.

When to use it​

  • Partners that send orders in their own language to the orders API, while the backend reads English only.
  • Request bodies that carry e-mail addresses the backend must never store, removed by a model before the request reaches it.
  • Free text from a mobile app that the backend should receive as one short paragraph.
  • A model your company runs itself behind an address that speaks the OpenAI protocol, named under Endpoint with the provider openai-compatible.

Not for changing the path, the Host header or the headers the backend receives — see proxy-rewrite (Rewrites the request URI, method, host and headers before proxying), sending the client's prompt to a model and the answer back to the client — see ai-proxy (Proxies requests to an LLM provider with a unified API) or adding documents from a search index to a prompt — see ai-rag (Adds retrieved documents to the prompt from a vector search).

Set it up​

Translate each request body into English before it reaches the backend​

The Authorization value is sent to the provider as written, so write it as Bearer, a space and the key.

Under Basics set Provider to openai, fill Auth as in the JSON below, set Prompt to Translate the request body into English and keep its JSON shape., fill Options as in the JSON below and fill Override as in the JSON below.

ai-request-rewrite · as the console saves it
{
"provider": "openai",
"auth": {
"header": {
"Authorization": "<secret>"
}
},
"prompt": "Translate the request body into English and keep its JSON shape.",
"options": {
"model": "gpt-4o-mini"
},
"override": {
"endpoint": "https://api.openai.com/v1/chat/completions"
}
}

Things to know​

  • Prompt — The rewritten body keeps the client's original content type. A model that answers in plain text on a request sent as application/json gives the backend a body labelled JSON, which the backend can refuse.
  • Provider — Bedrock and Vertex AI need AWS or GCP signing, which this plugin cannot do, because Auth takes only a header or a query key.
  • Auth — Auth takes a header or a query parameter only, and a key pair for signing, the aws shape, is refused when you save.
  • Endpoint — Without an endpoint, openai-compatible is refused at save, while azure-openai, bedrock and vertex-ai are saved but answer every request with 500.
  • Endpoint — An empty endpoint is refused when you save.
  • Timeout — The whole call to the model may last at most 60000 milliseconds, and 60001 is refused when you save.
  • Prompt — The backend receives the model's answer in place of the client's body: in the example a Turkish note arrives as Deliver before noon.
  • Endpoint — The request to the model goes to this address when it is set, the provider's own otherwise.
  • AdviceWrite the prompt so that it names the shape the backend reads, for example "keep its JSON shape" for a backend that parses JSON.

Settings​

The fields as the console shows them, section by section.

Basics​

ProviderproviderstringRequired

The AI service that rewrites the request.

one of aimlapi, anthropic, azure-openai, bedrock, deepseek, gemini, openai, openai-compatible, openrouter, vertex-ai
AuthauthobjectRequired

The provider credential, usually a header such as Authorization with the value Bearer and your key.

Headerauth.headerobject

Stored on the engine and shown only to roles that manage this object. An object whose keys are letters, digits, dots, underscores and hyphens, each with a string as its value.

Queryauth.queryobject

Stored on the engine and shown only to roles that manage this object. An object whose keys are letters, digits, dots, underscores and hyphens, each with a string as its value.

PromptpromptstringRequired

The instruction sent to the model with the client's request body, whose answer then replaces that body. The body keeps its original Content-Type, so a plain-text answer sent on as application/json can be refused by the upstream.

Optionsoptionsobject

Settings passed to the model, such as the model name or the temperature. Other keys are stored as written.

example {"model":"gpt-4o","temperature":0.2}
modeloptions.modelstring
OverrideoverrideobjectRequired when Provider is openai-compatible, or when Provider is azure-openai, or when Provider is bedrock, or when Provider is vertex-ai
Endpointoverride.endpointstringRequired when Provider is openai-compatible, or when Provider is azure-openai, or when Provider is bedrock, or when Provider is vertex-ai

The address requests go to instead of the provider's own, including the full path for bedrock and vertex-ai.

at least 1 character

Behavior​

Timeouttimeoutintegerdefault 30000

How long the whole call to the model may take, connect, send and read together.

1 to 60000 · in milliseconds
SSL verifyssl_verifybooleandefault true

Advanced​

Request body limitmax_req_body_sizeintegerdefault 67108864

The largest request body in bytes that is read, where a larger one is refused with 400.

at least 1
Keepalivekeepalivebooleandefault true
Keepalive poolkeepalive_poolintegerdefault 30
at least 1

Every plugin also has Enabled, Priority, Filter, Error response and Pre-function: see Plugin execution settings.

G8 dev · Apache APISIX® 3.19.0
Apache, Apache APISIX and APISIX are trademarks of The Apache Software Foundation.
Facts
Category
AI
Add it to
Route, Service, Consumer, Consumer group, Global rule, Plugin config
Engine plugin
ai-request-rewrite
Priority
1073