Forward OpenAI SDK model selection to AI Proxy Advanced in Kong Gateway

Deployment Platform
Minimum Version
Kong Gateway - 3.6
TL;DR

Add a Pre-function plugin that extracts the model from the request body into a custom header, then use the $(headers.x-source-model) template variable in the AI Proxy Advanced config to reference it dynamically.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

     export KONNECT_TOKEN='YOUR_KONNECT_PAT'
    
  3. Run the quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
    

    This sets up a Konnect Control Plane named quickstart, provisions a local Data Plane, and prints out the following environment variable exports:

     export DECK_KONNECT_TOKEN=$KONNECT_TOKEN
     export DECK_KONNECT_CONTROL_PLANE_NAME=quickstart
     export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
     export KONNECT_PROXY_URL='http://localhost:8000'
    

    Copy and paste these into your terminal to configure your session.

This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

    curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    

decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial, install decK version 1.43 or later.

This guide uses deck gateway apply, which directly applies entity configuration to your Gateway instance. We recommend upgrading your decK installation to take advantage of this tool.

You can check your current decK version with deck version.

For this tutorial, you’ll need Kong Gateway entities, like Gateway Services and Routes, pre-configured. These entities are essential for Kong Gateway to function but installing them isn’t the focus of this guide. Follow these steps to pre-configure them:

  1. Run the following command:

    echo '
    _format_version: "3.0"
    services:
      - name: example-service
        url: http://httpbin.konghq.com/anything
    routes:
      - name: example-route
        paths:
        - "/anything"
        service:
          name: example-service
    ' | deck gateway apply -
    

To learn more about entities, you can read our entities documentation.

This tutorial uses OpenAI:

  1. Create an OpenAI account.
  2. Get an API key.
  3. Create a decK variable with the API key:

    export DECK_OPENAI_API_KEY='YOUR OPENAI API KEY'
    

OpenAI-compatible SDKs always set the model field in the request body. This is a required parameter and can’t be omitted.

AI Proxy Advanced validates the body model against the plugin-configured model. If they don’t match, the plugin rejects the request with 400 Bad Request: cannot use own model - must be: <configured-model>.

Instead of hardcoding a model in the plugin config, you can let the SDK’s model value drive the upstream selection. The Pre-function plugin extracts the model into a custom header, and AI Proxy Advanced reads it through a template variable. The validation passes because the resolved plugin model matches the body model.

Configure the Pre-function plugin

First, let’s configure the Pre-function plugin to extract the model field from the request body and write it into a custom x-source-model header:

echo '
_format_version: "3.0"
plugins:
  - name: pre-function
    config:
      access:
      - |-
        local req_body = kong.request.get_body()
        local model = req_body.model
        kong.service.request.set_header("x-source-model", model)
' | deck gateway apply -

Configure the AI Proxy Advanced plugin

Now, let’s configure AI Proxy Advanced to read the model name from the x-source-model header using the $(headers.x-source-model) template variable:

echo '
_format_version: "3.0"
plugins:
  - name: ai-proxy-advanced
    config:
      targets:
      - route_type: llm/v1/chat
        auth:
          header_name: Authorization
          header_value: Bearer ${{ env "DECK_OPENAI_API_KEY" }}
        model:
          provider: openai
          name: "$(headers.x-source-model)"
          options:
            max_tokens: 512
            temperature: 1.0
' | deck gateway apply -

The SDK sends "model": "gpt-4o" in the request body. Pre-function copies that value into the x-source-model header. AI Proxy Advanced resolves $(headers.x-source-model) to gpt-4o and uses it as the upstream model name. The validation passes because the body model and the resolved plugin model match.

Create a script

Now, let’s create a test script that sends requests with different model names. Each request reaches a different OpenAI model through the same route:

cat <<EOF > test_dynamic_model.py
from openai import OpenAI

kong_url = "http://localhost:8000"
kong_route = "anything"

client = OpenAI(
    api_key="test",
    base_url=f"{kong_url}/{kong_route}"
)

for model in ["gpt-4o", "gpt-4o-mini"]:
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": "What model are you? Reply with only your model name."}]
    )
    print(f"Requested: {model}, Got: {response.model}")
EOF
cat <<EOF > test_dynamic_model.py
from openai import OpenAI
import os

kong_url = os.environ['KONNECT_PROXY_URL']
kong_route = "anything"

client = OpenAI(
    api_key="test",
    base_url=f"{kong_url}/{kong_route}"
)

for model in ["gpt-4o", "gpt-4o-mini"]:
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": "What model are you? Reply with only your model name."}]
    )
    print(f"Requested: {model}, Got: {response.model}")
EOF

Validate the configuration

Now, we can run the script we created in the previous step:

python test_dynamic_model.py

You should see each request routed to the corresponding OpenAI model. The response.model value should match the model name the SDK sent.

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!