Kong Gateway Plugin

Add cost-aware model routing to Kong Gateway and Kong AI Gateway with the Nadir plugin, on self-hosted Kong or Konnect, with no application code changes.

Cost-aware model routing for Kong Gateway and Kong AI Gateway, installed as a Kong plugin with no application code changes.

Your application changes one string. Everything else, auth, provider keys, streaming, rate limits, and logging, stays exactly as Kong already does it. This plugin only supplies the routing decision.

The whole application change
resp = client.chat.completions.create(
-   model="claude-opus-4-6",
+   model="nadir-auto",
    messages=[...],
)

nadir-auto is a virtual model. Inside Kong, the plugin reads the prompt, calls /v1/recommend to ask which real model fits, and rewrites the request body before it leaves the gateway. There is no Nadir service to run alongside it.

Install

Self-hosted Kong

Shell
# 1. Install the plugin from LuaRocks
luarocks install kong-plugin-nadir

# 2. Enable it in kong.conf, or via env
export KONG_PLUGINS=bundled,nadir

# 3. Point a route at Nadir
deck gateway sync kong.yaml

Konnect

The same plugin runs on Konnect as a custom plugin. Upload handler.lua and schema.lua, then enable it on a route from the UI. No LuaRocks and no kong.conf edit.

Configure a route

A declarative example, applied with deck gateway sync kong.yaml.

kong.yaml
_format_version: "3.0"

services:
  - name: llm
    # Any OpenAI-compatible chat completions endpoint.
    url: https://api.openai.com
    routes:
      - name: chat
        paths:
          - /v1/chat/completions
        strip_path: false
        plugins:
          - name: nadir
            config:
              api_key: "{vault://env/nadir-api-key}"
              virtual_model: nadir-auto
              default_model: claude-sonnet-4-6
              timeout_ms: 250
              # Optional: constrain the candidate set. When omitted, Nadir
              # routes among the models configured on the API key.
              # models:
              #   - claude-haiku-4-5
              #   - claude-sonnet-4-6
              #   - claude-opus-4-6

Then send a request. Nothing about the call changes except the model name.

Shell
curl -s localhost:8000/v1/chat/completions -i \
  -H 'content-type: application/json' \
  -H "authorization: Bearer $OPENAI_API_KEY" \
  -d '{"model":"nadir-auto","messages":[{"role":"user","content":"hi"}]}'

Warning

Store the API key in a Kong Vault rather than plaintext. The api_key field is referenceable, so {vault://env/nadir-api-key} reads it from the NADIR_API_KEY environment variable.

Info

Calls with a key are billed: the first 50 requests a month are free, then $0.002 each from a prepaid balance. When the balance is empty, Nadir answers HTTP 402 balance_empty and the plugin sends the request to default_model, so top up or turn on auto-recharge in Billing before you rely on it.

Configuration

FieldDefaultMeaning
api_urlhttps://api.getnadir.comNadir decision API base
recommend_path/v1/recommendDecision endpoint, no LLM call
api_keyrequiredX-API-Key for the decision call, referenceable
virtual_modelnadir-autoThe sentinel model that opts into routing
default_modelclaude-sonnet-4-6Fail-open target
timeout_ms250Per-request decision budget, 1 to 10000
modelsunsetOptional candidate set; defaults to the models on the API key
response_headerstrueStamp x-nadir-* headers on the response

Guarantees

  • Fail-open. Any decision error or timeout means the request proceeds on default_model. If the body is unreadable or not JSON, the plugin touches nothing. A router outage never takes down inference.
  • Opt-in. Only requests with model: nadir-auto are routed. A real model name is honored and passed through untouched, so you can adopt on 5 percent of traffic first.
  • Bounded. The plugin calls the decision-only endpoint, which never invokes an LLM, and the whole call is bounded by timeout_ms, 250ms by default. The budget is split across connect, send, and read rather than applied to each, so the worst case is the number you configured and not 3 times it.

Seeing the decision

The decision rides back on the response the client already gets, so there is no second place to look.

x-nadir-model: claude-haiku-4-5
x-nadir-tier: simple
x-nadir-confidence: 0.983
x-nadir-routed: true

Set response_headers to false to suppress them.

Plugin ordering

Nadir sits at priority 775 in the access phase, deliberately between two bounds. Auth plugins such as key-auth are 1250 and run before Nadir, so unauthenticated traffic never spends a decision call. Kong's ai-proxy is 770 and runs after Nadir, so it dispatches the model Nadir chose.

Pairing with ai-proxy

Nadir supplies the decision and ai-proxy owns provider auth and streaming. Leave model.name unset on ai-proxy so it takes the model from the rewritten body.

Warning

If streaming latency matters, prefer the plain upstream path. On Kong 3.7, ai-proxy buffers server-sent events by itself. Measured across a 5-frame stream: Nadir to a plain upstream spread the frames over about 0.90s, while ai-proxy alone and ai-proxy with Nadir both showed 0.00s spread. The buffering is identical with and without the plugin, so it is not ours, but it is real. On the plain upstream path frames reach the client incrementally and the x-nadir-* headers arrive with the response headers.

Info

Point api_url at a self-hosted NadirClaw or Nadir instance instead of api.getnadir.com to keep the decision entirely in-network. Both speak the same endpoint contract, so it is a drop-in swap. See Self-Host.

What has been validated

Verified against Kong 3.7, DB-less OSS, on a hermetic harness that mocks both the decision API and the LLM upstream: the plugin loads, the schema is accepted, the body rewrite reaches the upstream with a recalculated content-length, fail-open and opt-in behave as documented, the ai-proxy pairing dispatches the chosen model, and server-sent events stream incrementally with headers intact.

Not yet exercised against Konnect or Kong Enterprise, and the harness mocks the decision call rather than hitting api.getnadir.com.

Pages on this site

Machine-readable summaries of this site: llms.txt and llms-full.txt.