sequenceDiagram
autonumber
participant client as Client
participant kong as AI Gateway
participant backend as Upstream LLM
participant ai as AI service
activate client
activate kong
client->>kong: Sends a prompt
deactivate client
activate backend
kong->>backend: Forwards the prompt
backend->>kong: Returns the response to AI Gateway
deactivate backend
activate ai
kong->>ai: Sends the response for transformation
ai->>kong: Returns the transformed response
deactivate ai
activate client
kong->>client: Returns the transformed response to the client
deactivate kong
deactivate client
Figure 1: The diagram shows the journey of a consumer’s prompt through AI Gateway to an Upstream LLM that sends a response, the response is transformed by the AI Response Transformer Policy using an AI service.
The AI Gateway admin sets up an llm configuration block and a prompt for the LLM service used to transform requests.
The prompt becomes the system message in the LLM chat request, and provides transformation
instructions to the LLM for the returning upstream response body.
- The client makes an HTTP(S) call.
- The AI Gateway sends the transformed request to the upstream LLM provider or uses the AI Request Transformer Policy.
- The the upstream LLM provider returns a response to AI Gateway.
- The AI Response Transformer policy sets the entire response body as the
user message in an LLM chat request, then sends it to the configured LLM service.
- The LLM service returns a response
assistant message, which is subsequently set as the upstream response body.
- The Policy returns early (
kong.response.exit) and can handle gzip or chunked requests, similar to the Forward Proxy policy.
You can additionally instruct the LLM service to respond in the following format, which lets you adjust the response headers, response status code, and response body:
{
"headers":
{
"new-header": "new-value"
}
}
If the parse_llm_response_json_instructions parameter is set to true, AI Gateway will parse these instructions and set the specified response headers, response status code, and replacement response body.
This lets you change specific headers such as Content-Type, or throw errors from the LLM.