OpenAI SDK: Multi-provider chat routingv3.6+

Use a single OpenAI SDK client to route chat requests to different LLM providers by defining separate Routes per provider.

Configure multiple chat Routes in Kong Gateway, each targeting a different LLM provider such as Cohere or Mistral, while using the same OpenAI-compatible SDK client. The AI Proxy plugin extracts the model name from the request path, and injects provider-specific credentials automatically. This lets you arbitrate between providers without changing client logic, using only the base URL to select a provider.

Using this configuration, you can select your desired provider using the SDK:

client = OpenAI(
base_url="http://127.0.0.1:8000/cohere"
)

Or:

client = OpenAI(
base_url="http://127.0.0.1:8000/mistral"
)

For this configuration to work properly, you need two Routes with the following configuration:

routes:
 - name: cohere-chat
   paths:
     - "~/cohere/(?<model>[^#?/]+)/chat/completions$"
   methods:
     - POST

and:

routes:
 - name: mistral-chat
   paths:
     - "~/mistral(?<model>[^#?/]+)/chat/completions$"
   methods:
     - POST

Prerequisites

  • Cohere account

  • Mistral account

Environment variables

  • COHERE_API_KEY: The API key to authenticate requests to Cohere.

  • MISTRAL_API_KEY: The API key to authenticate requests to Mistral.

Set up the plugin

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!