apisix icon indicating copy to clipboard operation
apisix copied to clipboard

help request: How to select the corresponding AI proxy based on the model parameter in the user-requested body

Open RGZingYang opened this issue 10 months ago • 3 comments

Description

I need to use ai-rate-limiting, which must be used with ai-proxy-multi. However, ai-proxy-multi only supports the message parameter and does not support the model parameter. How can I select the corresponding AI proxy based on the model parameter like the OpenAI interface does? ai-proxy-multi as:

{
   "plugins": {
      "ai-proxy-multi": {
        "fallback_strategy: ["rate_limiting"],
        "instances": [
          {
            "name": "openai-instance",
            "provider": "openai",
            "priority": 1,
            "weight": 0,
            "auth": {
              "header": {
                "Authorization": "Bearer '"$OPENAI_API_KEY"'"
              }
            },
            "options": {
              "model": "gpt-4"
            }
          },
          {
            "name": "openai-instance",
            "provider": "openai",
            "priority": 1,
            "weight": 0,
            "auth": {
              "header": {
                "Authorization": "Bearer '"$OPENAI_API_KEY"'"
              }
            },
            "options": {
              "model": "gpt-5"
            }
          }
        ]
      },
      "ai-rate-limiting": {
        "instances": [
          {
            "name": "openai-instance",
            "limit": 10,
            "time_window": 60
          }
        ],
        "limit_strategy": "total_tokens"
      }
    }
}

select instances by model such as:

{
request :{
    "model": "gpt-5",
    "messages": [
        {
            "role": "user",
            "content": "hello"
        }
    ]
}

Environment

  • APISIX version (run apisix version): 3.14.1
  • Operating system (run uname -a):
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):

RGZingYang avatar Feb 03 '26 03:02 RGZingYang

Currently APISIX supports this by using the vars field in route, so what you could do is to configure multiple routes with different vars to match different models in request body, and with different ai provider in ai-proxy-multi plugin for each route. Here's an example that you can take a look at: https://github.com/apache/apisix/blob/f37c19013afe4d515c950dc28d0b388d29f32d85/docs/en/latest/router-radixtree.md?plain=1#L341-L415

janiussyafiq avatar Feb 06 '26 04:02 janiussyafiq

Here's an example of your use case @RGZingYang :

  1. Configure 2 routes. Each route has vars that match the model that you want. In this example I used Anthropic as my provider:
// first route for opus 4.6
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -H "X-API-KEY: XXXXX" -i -d '
{
    "id": "ai-proxy-claude-opus",
    "methods": ["POST"],
    "uri": "/ai",
    "vars": [
        ["post_arg.model", "==", "claude-opus"]
    ],
    "plugins": {
        "ai-proxy": {
            "name": "claude-opus",
            "provider": "anthropic",
            "auth": {
                "header": {
                    "Authorization": "Bearer '"$ANTHROPIC_API_KEY"'"
                }
            },
            "options": {
                "model": "claude-opus-4-6"
            }
        },
        "ai-rate-limiting": {
            "instances": [
                {
                    "name": "antropic-instance-opus",
                    "limit": 10,
                    "time_window": 60
                }
            ],
            "limit_strategy": "total_tokens"
        }
    }
}
'

// second route for sonnet 4.5
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -H "X-API-KEY: XXXXX" -i -d '
{
    "id": "ai-proxy-claude-sonnet",
    "methods": ["POST"],
    "uri": "/ai",
    "vars": [
        ["post_arg.model", "==", "claude-sonnet"]
    ],
    "plugins": {
        "ai-proxy": {
            "name": "claude-sonnet",
            "provider": "anthropic",
            "auth": {
                "header": {
                    "Authorization": "Bearer '"$ANTHROPIC_API_KEY"'"
                }
            },
            "options": {
                "model": "claude-sonnet-4-5"
            }
        },
        "ai-rate-limiting": {
            "instances": [
                {
                    "name": "antropic-instance-sonnet",
                    "limit": 10,
                    "time_window": 60
                }
            ],
            "limit_strategy": "total_tokens"
        }
    }
}
'
  1. Then you can test your routes are working by sending request to both models.
curl -i -X POST http://127.0.0.1:9080/ai -H "Content-Type: application/json" -d '{"model": "claude-opus", "messages": [{"role": "user", "content": "hi"}]}'

curl -i -X POST http://127.0.0.1:9080/ai -H "Content-Type: application/json" -d '{"model": "claude-sonnet", "messages": [{"role": "user", "content": "hi"}]}'
  1. You will get the following success response if all are configured correctly:
HTTP/1.1 200 OK
Date: Mon, 09 Feb 2026 03:00:57 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Server: APISIX/3.15.0

{"id":"msg_014vmk8dP33iGZDSWouQiiuZ","choices":[{"finish_reason":"stop","index":0,"message":{"role":"assistant","content":"Hi there! 👋 How are you doing today? Is there anything I can help you with?"}}],"created":1770606057,"model":"claude-opus-4-6","object":"chat.completion","usage":{"completion_tokens":25,"prompt_tokens":8,"total_tokens":33}}%   

janiussyafiq avatar Feb 09 '26 03:02 janiussyafiq

Duplicate of #12735

Baoyuantop avatar Feb 09 '26 03:02 Baoyuantop