# Route Traffic to OpenAI

[OpenAI](https://openai.com/) provides access to models such as GPT-4o and o1 for chat completions, embeddings, and more. This guide shows how to route traffic to OpenAI through API7 Gateway using the [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md) plugin.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

* Install [Docker](https://docs.docker.com/get-docker/).

* Install [cURL](https://curl.se/) to send requests to the services for validation.

* Have a running API7 Gateway instance.

* Create a [token from the Dashboard](https://docs.api7.ai/api7-gateway/reference/obtain-dashboard-token.md) and save it to an environment variable:

  ```
  export API_KEY=your-dashboard-token   # replace with your Dashboard token
  ```

* Replace `{gateway_group_id}` with your gateway group ID. Use `default` if you are following the quickstart.

* If you are following the Admin API examples, create or reuse a service in API7 Gateway. If you do not have one yet, follow [Create or Reuse a Service](https://docs.api7.ai/api7-gateway/ai-gateway/get-started.md#create-or-reuse-a-service), then save its ID to an environment variable:

  ```
  export SERVICE_ID=your-service-id         # replace with your service ID
  ```

## Obtain an OpenAI API Key[​](#obtain-an-openai-api-key "Direct link to Obtain an OpenAI API Key")

Create an [OpenAI account](https://openai.com) and generate an [API key](https://platform.openai.com/api-keys). Save the key to an environment variable:

```
export OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxxxxxx   # replace with your OpenAI API key
```

## Configure OpenAI as a Provider[​](#configure-openai-as-a-provider "Direct link to Configure OpenAI as a Provider")

Create a route with the [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md) plugin:

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/openai-route?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  --data-binary @- <<EOF

  {

    "name": "openai-route",

    "service_id": "$SERVICE_ID",

    "paths": ["/openai"],

    "plugins": {

      "ai-proxy": {

        "provider": "openai",

        "auth": {

          "header": {

            "Authorization": "Bearer $OPENAI_API_KEY"

          }

        },

        "options": {

          "model": "gpt-4o"

        }

      }

    }

  }

EOF
```

❶ Set the provider to `openai`.

❷ Attach the OpenAI API key in the `Authorization` header.

❸ Set the default model to `gpt-4o`.

adc.yaml

```
services:

  - name: OpenAI Service

    routes:

      - uris:

          - /openai

        name: openai-route

        plugins:

          ai-proxy:

            provider: openai

            auth:

              header:

                Authorization: "Bearer ${OPENAI_API_KEY}"

            options:

              model: gpt-4o

    labels:

      docs-example: openai-service
```

❶ Set the provider to `openai`.

❷ Attach the OpenAI API key in the `Authorization` header.

❸ Set the default model to `gpt-4o`.

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=openai-service
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=openai-service
```

## Multi-Model Routing with OpenAI[​](#multi-model-routing-with-openai "Direct link to Multi-Model Routing with OpenAI")

Use the [`ai-proxy-multi`](https://docs.api7.ai/hub/ai-proxy-multi.md) plugin to distribute traffic across multiple OpenAI models. This example routes 80% of traffic to `gpt-4o-mini` (cost-effective) and 20% to `gpt-4o` (premium), with automatic failover on HTTP 429 and 5xx errors:

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/openai-multi-route?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  --data-binary @- <<EOF

  {

    "service_id": "$SERVICE_ID",

    "name": "openai-multi-route",

    "paths": ["/openai-multi"],

    "plugins": {

      "ai-proxy-multi": {

        "fallback_strategy": ["http_429", "http_5xx"],

        "instances": [

          {

            "name": "gpt-4o-mini",

            "provider": "openai",

            "auth": {

              "header": {

                "Authorization": "Bearer $OPENAI_API_KEY"

              }

            },

            "options": {

              "model": "gpt-4o-mini"

            },

            "weight": 8

          },

          {

            "name": "gpt-4o",

            "provider": "openai",

            "auth": {

              "header": {

                "Authorization": "Bearer $OPENAI_API_KEY"

              }

            },

            "options": {

              "model": "gpt-4o"

            },

            "weight": 2

          }

        ]

      }

    }

  }

EOF
```

❶ `fallback_strategy` enables automatic failover. When an instance returns HTTP 429 (rate limited) or 5xx (server error), the gateway retries on another instance.

❷ Assign a weight of `8` so that roughly 80% of traffic goes to `gpt-4o-mini`.

❸ Assign a weight of `2` to the premium `gpt-4o` instance for the remaining 20%.

adc.yaml

```
services:

  - name: OpenAI Multi-Model Service

    routes:

      - uris:

          - /openai-multi

        name: openai-multi-route

        plugins:

          ai-proxy-multi:

            fallback_strategy:

              - http_429

              - http_5xx

            instances:

              - name: gpt-4o-mini

                provider: openai

                auth:

                  header:

                    Authorization: "Bearer ${OPENAI_API_KEY}"

                options:

                  model: gpt-4o-mini

                weight: 8

              - name: gpt-4o

                provider: openai

                auth:

                  header:

                    Authorization: "Bearer ${OPENAI_API_KEY}"

                options:

                  model: gpt-4o

                weight: 2

    labels:

      docs-example: openai-multi-model-service
```

❶ `fallback_strategy` enables automatic failover. When an instance returns HTTP 429 (rate limited) or 5xx (server error), the gateway retries on another instance.

❷ Assign a weight of `8` so that roughly 80% of traffic goes to `gpt-4o-mini`.

❸ Assign a weight of `2` to the premium `gpt-4o` instance for the remaining 20%.

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=openai-multi-model-service
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=openai-multi-model-service
```

For more routing strategies including failover and priority-based routing, see [Multi-LLM Routing and Fallback](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/multi-llm-routing-and-fallback.md).

## Validate the Configuration[​](#validate-the-configuration "Direct link to Validate the Configuration")

Send this request to the multi-model route. To test the single-model route, use `/openai` instead of `/openai-multi`.

```
curl "http://127.0.0.1:9080/openai-multi" -X POST \

  -H "Content-Type: application/json" \

  -d '{

    "messages": [

      { "role": "system", "content": "You are a computer scientist." },

      { "role": "user", "content": "Explain in one sentence what a Turing machine is." }

    ]

  }'
```

You should receive a response similar to the following:

```
{

  "id": "chatcmpl-abc123",

  "object": "chat.completion",

  "model": "gpt-4o-2024-08-06",

  "choices": [

    {

      "index": 0,

      "message": {

        "role": "assistant",

        "content": "A Turing machine is an abstract mathematical model of computation that defines an idealized machine capable of simulating any algorithm."

      },

      "finish_reason": "stop"

    }

  ],

  "usage": {

    "prompt_tokens": 22,

    "completion_tokens": 24,

    "total_tokens": 46

  }

}
```

To enable streaming responses, set `"stream": true` in the request body. Use the [`proxy-buffering`](https://docs.api7.ai/hub/proxy-buffering.md) plugin to disable NGINX `proxy_buffering` to avoid server-sent events (SSE) being buffered.

## Next Steps[​](#next-steps "Direct link to Next Steps")

You have learned how to route traffic to OpenAI through API7 Gateway. See the [OpenAI API reference](https://platform.openai.com/docs/api-reference) to learn more about available models and endpoints.

* [Multi-LLM Routing and Fallback](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/multi-llm-routing-and-fallback.md) — Load balance across models and providers.
* [Token Rate Limiting](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/token-rate-limiting-and-quota-management.md) — Control costs with token budgets.
* [AI Observability](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/ai-observability-and-cost-tracking.md) — Monitor token usage and latency.
