AI Tool Integration: Open WebUI

Minimum Version
AI Gateway - 2.0
Incompatible with
on-prem

Overview

Put Kong AI Gateway between Open WebUI and OpenAI, so that the organization’s OpenAI key stays in Kong and each Open WebUI deployment authenticates with its own key that you can revoke. You’ll set up:

  • An AI Consumer key that Open WebUI sends as a standard Bearer token
  • A global Pre-Function Policy that lets the Key Auth AI Auth Strategy read that Bearer token
  • An AI Model that accepts one model ID, which you list in the Open WebUI connection settings

Prerequisites

This tutorial uses Kong Konnect. The AI Gateway quickstart script provisions a recipe-scoped AI Gateway and local data plane.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
  2. Export your token. You reuse the same token later for kongctl commands:

    export KONNECT_TOKEN='YOUR_KONNECT_PAT'
  3. Set the recipe-scoped AI Gateway name and run the quickstart script:

    export KONNECT_CONTROL_PLANE_NAME='coding-agents-open-webui-recipe'
    curl -Ls https://get.konghq.com/ai | bash -s -- -k $KONNECT_TOKEN

    This provisions an AI Gateway named coding-agents-open-webui-recipe, a local data plane connected to it, and prints export lines for the rest of the session vars, including AI_GATEWAY_ID. Paste those into your shell when prompted.

This tutorial uses kongctl to manage AI Gateway configuration.

  1. Install kongctl from the kongctl documentation.
  2. Verify it’s installed:

    kongctl version

This tutorial runs Open WebUI in a container with Docker Compose.

  1. Install Docker with the Compose plugin.
  2. Verify that Compose is installed:

    docker compose version

Pick the provider you want to route to and export its credentials. The provider’s tab in Apply the Kong configuration reads these variables.

What this cookbook solves

Most teams connect Open WebUI directly to OpenAI with the organization’s API key in each deployment, which causes the following problems:

Problem

Solution

Open WebUI saves the connection’s API key in its own database (OPENAI_API_KEYS is a persistent setting), so each instance, volume, and backup holds a credential that bills the organization’s account. The AI Model Provider keeps the OpenAI key in Kong. Open WebUI holds only an AI Consumer key, and Kong sends the OpenAI key upstream.
Each deployment uses the same key, so the provider bill is one line item, and you can’t revoke one deployment without rotating the key everywhere. The Key Auth AI Auth Strategy authenticates each deployment as an AI Consumer. Kong rejects a missing or unknown key with 401 before any upstream call, and deleting one credential revokes one deployment.
OpenAI-compatible clients send the API key only as Authorization: Bearer <key>, so a gateway in front of them has to accept that header without client changes. A global Pre-Function Policy copies the Bearer token into the apikey header before authentication, so Open WebUI keeps its standard connection settings.
Open WebUI fills its model selector from the backend’s /models endpoint, and some OpenAI-compatible backends don’t serve it. The AI Model accepts one model ID, CHAT_MODEL. You list the same ID in the Open WebUI connection, so the selector shows exactly what Kong serves.

Try it out

Apply the configuration, connect Open WebUI to Kong, then send requests with and without the AI Consumer key and chat from the browser.

Apply the Kong configuration

The configuration creates an AI Model Provider with the organization’s OpenAI key, a Key Auth AI Auth Strategy, an AI Consumer with an API key, a global Pre-Function Policy that copies the Bearer token into the apikey header, and an AI Model on /coding-agents. A kongctl namespace scopes every resource, so you can remove this configuration without affecting anything else on the same AI Gateway. See the kongctl documentation for more on namespaces.

Adopt the quickstart AI Gateway into a kongctl namespace so that kongctl can manage it. Adoption adds the KONGCTL-namespace label to the AI Gateway:

kongctl adopt ai-gateway "${KONNECT_CONTROL_PLANE_NAME}" \
  --namespace "${KONNECT_CONTROL_PLANE_NAME}" \
  --pat "${KONNECT_TOKEN}"

Generate the AI Consumer’s API key. Open WebUI sends this key to Kong instead of an OpenAI key. The credential’s api_key field is write-only, so its value must come from an environment variable:

export CONSUMER_API_KEY="coding-agents-demo-$(openssl rand -hex 8)"

Select your provider. The tab exports CHAT_MODEL, the only model ID that the AI Model accepts and the model that Kong requests from OpenAI, then applies the configuration. Provider credentials come from the prerequisites.

Connect Open WebUI to Kong

Run Open WebUI in Docker with an OpenAI-compatible connection to the AI Model. The following Open WebUI environment variables configure the connection:

  • OPENAI_API_BASE_URLS and OPENAI_API_KEYS set the connection URL and the AI Consumer key. host.docker.internal lets the container reach the data plane on port 8000 of your host.
  • OPENAI_API_CONFIGS sets the options of each connection, keyed by the connection’s index. model_ids lists the model IDs of the connection, because the AI Model doesn’t serve a model list.
  • DEFAULT_MODELS selects the model on a new chat.
  • ENABLE_OLLAMA_API set to False turns off the default Ollama connection.
  • WEBUI_AUTH set to False turns off the Open WebUI sign-in for this local test. Open WebUI allows this only on a new installation with no users.

Docker Compose replaces ${CONSUMER_API_KEY} and ${CHAT_MODEL} with the values from your shell (variable interpolation), so the model ID always matches the AI Model. Create the Compose file in an empty directory:

cat <<'EOF' > compose.yaml
services:
  open-webui:
    image: ghcr.io/open-webui/open-webui:main
    ports:
      - "3081:8080"
    environment:
      OPENAI_API_BASE_URLS: "http://host.docker.internal:8000/coding-agents"
      OPENAI_API_KEYS: "${CONSUMER_API_KEY}"
      OPENAI_API_CONFIGS: "{\"0\": {\"enable\": true, \"model_ids\": [\"${CHAT_MODEL}\"]}}"
      DEFAULT_MODELS: "${CHAT_MODEL}"
      ENABLE_OLLAMA_API: "False"
      WEBUI_AUTH: "False"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - open-webui:/app/backend/data
volumes:
  open-webui:
EOF

Start Open WebUI from the same directory, in the shell where CONSUMER_API_KEY and CHAT_MODEL are exported:

docker compose up -d

Open WebUI saves these connection values in its database at the first start, and uses the saved values instead of the environment variables after a restart. To apply changed values, run docker compose down -v and start Open WebUI again.

Send a request through the AI Model

Call Chat Completions with the AI Consumer key as a Bearer token, the same way Open WebUI does:

curl -sS -X POST "http://localhost:8000/coding-agents/chat/completions" \
  -H "Authorization: Bearer ${CONSUMER_API_KEY}" \
  -H "Content-Type: application/json" \
  -d "{
    \"model\": \"${CHAT_MODEL}\",
    \"messages\": [{\"role\": \"user\", \"content\": \"Reply with exactly: open-webui-ok\"}]
  }"

The response is an OpenAI Chat Completions object, with different IDs and token counts:

{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "model": "gpt-4o-mini-2024-07-18",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "open-webui-ok"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 5,
    "total_tokens": 20
  }
}

Send a request without the AI Consumer key

Send the same request without the Authorization header:

curl -sS -o /dev/null -w '%{http_code}\n' -X POST "http://localhost:8000/coding-agents/chat/completions" \
  -H "Content-Type: application/json" \
  -d "{\"model\": \"${CHAT_MODEL}\", \"messages\": [{\"role\": \"user\", \"content\": \"hi\"}]}"

Kong rejects the request before it reaches OpenAI:

401

Chat from Open WebUI

Open http://localhost:3081, select the model from CHAT_MODEL, and send Reply with exactly: open-webui-ok. Open WebUI shows the reply open-webui-ok.

The connection settings, under Settings > Connections in the admin section, have a Verify Connection button that calls /models. The AI Model doesn’t serve /models, so the verification fails, but chat still works (Open WebUI: connection verification).

Check the results

The requests and the Open WebUI chat confirm the following:

Behavior

Evidence

AI Consumer authentication with a Bearer token The request with Authorization: Bearer and the AI Consumer key returns a completion. The Pre-Function Policy copied the token into apikey, and the Key Auth AI Auth Strategy matched it to coding-agents-consumer.
Authentication before any upstream call The request without a key gets 401. Kong never contacted OpenAI.
One model ID Open WebUI lists only the model from model_ids, and the AI Model accepts it because it equals CHAT_MODEL.
Provider credentials stay in Kong Open WebUI only held the AI Consumer key. The AI Model Provider added the OpenAI key to each upstream request.

To review the created entities, and per-consumer usage, open the coding-agents-open-webui-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu.

How it works

Open WebUI sends each chat request to Kong with the AI Consumer key as a Bearer token. Kong copies the token into the apikey header, authenticates the AI Consumer, selects the AI Model that matches the model ID, and forwards the request to OpenAI with the organization’s key.

sequenceDiagram
    participant W as Open WebUI
    participant K as Kong AI Gateway
    participant O as OpenAI

    W->>K: POST /coding-agents/chat/completions (Authorization: Bearer AI Consumer key)
    activate K
    K->>K: Global Pre-Function Policy copies the Bearer token into apikey
    K->>K: Key Auth AI Auth Strategy validates apikey (else 401)
    K->>K: Select the AI Model whose model value matches the request
    K->>K: AI Model Provider adds the organization's OpenAI key
    K->>O: Chat Completions request
    activate O
    O-->>K: Response, streamed when requested
    deactivate O
    K-->>W: OpenAI-format response
    deactivate K

Pre-Function Policy

The Key Auth AI Auth Strategy reads the AI Consumer key from the apikey header and compares the whole header value, so it can’t read a key from Authorization: Bearer <key>. The Pre-Function Policy runs Lua code that copies the token into apikey before authentication. It’s global, because a policy attached to an AI Model runs only after authentication succeeds, while a global policy with a higher priority than the AI Auth Strategy runs before it (AI Policy scopes and authentication).

The following settings configure the Pre-Function Policy:

Setting

Purpose

global: true Applies the policy to all traffic on the AI Gateway, so it runs before the Key Auth AI Auth Strategy. Each AI Model on this AI Gateway that uses a Key Auth AI Auth Strategy with the apikey header also accepts Bearer tokens.
config.rewrite Runs the Lua code in the rewrite phase. The Pre-Function Policy reference lists the other phases.
Lua code Compares the first seven characters of Authorization with bearer , without regard to case, and sets apikey to the rest of the value. A request without a Bearer token passes unchanged, and the Key Auth AI Auth Strategy rejects it. kong.request.get_header and ngx.req.set_header are in the strict sandbox allowlist.

Key Auth AI Auth Strategy and AI Consumer

Authentication is an AI Auth Strategy, not an AI Policy. The Key Auth AI Auth Strategy reads the apikey header that the Pre-Function Policy set and matches the key against the AI Consumer credentials. On a match, Kong attaches the AI Consumer to the request. The AI Consumer is the identity that rate limits, ACLs, and analytics use.

The following settings configure the Key Auth AI Auth Strategy and the AI Consumer:

Setting

Purpose

key_names: [apikey] Kong reads the key from the apikey header, which the Pre-Function Policy fills from the Bearer token.
hide_credentials: true Kong removes the key before it forwards the request. OpenAI never sees the AI Consumer key.
credentials[].api_key The AI Consumer’s key. The field is write-only, so it takes a !secret reference to CONSUMER_API_KEY.

Kong adds X-Consumer-Username and related headers to the upstream request, not to the response. Per-consumer usage appears in Konnect analytics.

AI Model Provider and AI Model

The AI Model Provider holds the organization’s OpenAI key. The AI Model exposes /coding-agents, accepts OpenAI-format requests whose model field equals CHAT_MODEL, and has one target that uses the AI Model Provider.

The following settings configure the AI Model Provider and the AI Model:

Setting

Purpose

config.auth (AI Model Provider) The Authorization header with the OpenAI key, from OPENAI_TOKEN with its Bearer prefix. Kong adds it to each upstream request, and it takes precedence over the client’s Authorization header.
formats: [{type: openai}] The AI Model accepts OpenAI Chat Completions requests, the format that Open WebUI sends.
capabilities: [generate] The AI Model serves /chat/completions and /completions. No capability serves /models, so Open WebUI can’t discover the model, and you list it in model_ids.
config.route.model Kong reads the model field of the request body (body_param: model) and selects this AI Model only if the value equals CHAT_MODEL. An AI Model accepts one value.
targets[].name The model that Kong requests from OpenAI, also CHAT_MODEL. Set it to another model to change what OpenAI serves without changing Open WebUI.
config.route.paths and strip_path: true Open WebUI adds /chat/completions to the connection URL. Kong matches /coding-agents and removes it before it forwards the request.
config.route.methods Open WebUI sends chat requests with POST. The AI Model also accepts GET and OPTIONS on the same path.
config.route.protocols: [http, https] Accepts HTTP, so Open WebUI can call the local data plane at http://host.docker.internal:8000.
config.max_request_body_size: 16777216 Accepts request bodies up to 16 MB, instead of the 8 MB default. Long chats send the full conversation with each request.
config.response_streaming: allow Kong passes streamed responses to Open WebUI, which shows tokens as they arrive.
config.logging.payloads: true Kong logs request and response payloads. Chat messages can contain sensitive data, so set it to false if you only need usage metrics. The default is false.

Production credentials: This cookbook stores the AI Consumer’s API key and the OpenAI key in environment variables for simplicity. In production, use AI Vaults to reference them from a secret manager, for example AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager, or Azure Key Vault.

Variations and next steps

You can change or extend this configuration in the following ways:

Variation

How

Give each deployment its own key Add an AI Consumer and credential for each Open WebUI deployment or team, and set each deployment’s OPENAI_API_KEYS to its own key. Usage and revocation then work for each deployment. The AI Model and AI Model Provider don’t change.
Keep environment variables authoritative Set ENABLE_PERSISTENT_CONFIG to False. Open WebUI then reads the connection from the environment variables at each start, and changes in the admin UI last only until the next restart.
Offer more models Add one AI Model for each model ID on the same /coding-agents path, each with its own config.route.model.values, and add each ID to model_ids. See the Basic LLM routing cookbook for two AI Models on one path.
Add per-consumer token limits Attach the AI Rate Limiting Advanced Policy to give each AI Consumer its own token budget, so one deployment can’t use up the organization’s quota.
Connect another chat UI The LibreChat cookbook points another browser chat UI at the same kind of AI Model.

Cleanup

Stop Open WebUI, remove its volume, and delete the Compose file. Run these commands in the directory that holds compose.yaml:

docker compose down -v
rm -f compose.yaml

The recipe’s kongctl namespace scoped all resources, so this teardown removes only this recipe’s configuration. Tear down the local data plane and delete the AI Gateway from Konnect:

export KONNECT_CONTROL_PLANE_NAME='coding-agents-open-webui-recipe' && curl -Ls https://get.konghq.com/ai | bash -s -- -d -k $KONNECT_TOKEN

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!