Claude Code SSO

Minimum Version
AI Gateway - 2.0
Incompatible with
on-prem

Overview

Put Kong AI Gateway between Claude Code and your Claude provider, so that Okta SSO authenticates each request and the developer’s Okta group decides which models they can use and how many tokens they get. You’ll set up:

  • Okta JWT validation on each Claude Code request, with no provider credentials on developer machines
  • Two model tiers, where only the power tier can use Opus
  • A token budget for each tier
  • A Konnect dashboard for Claude Code usage

This cookbook covers Claude Code’s API authentication path, which apiKeyHelper uses. Claude subscription seats that sign in with claude /login go directly to Anthropic and aren’t covered.

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. The two -e settings raise the data plane’s request body buffer and limit to 16 MB, so the large request bodies that Claude Code sends (conversation history, tool definitions, file contents) stay in memory instead of being buffered to disk:

    export KONNECT_CONTROL_PLANE_NAME='claude-code-sso-recipe'
    curl -Ls https://get.konghq.com/ai | \
      bash -s -- -k $KONNECT_TOKEN \
        -e KONG_NGINX_HTTP_CLIENT_BODY_BUFFER_SIZE=16m \
        -e KONG_NGINX_HTTP_CLIENT_MAX_BODY_SIZE=16m

    This provisions an AI Gateway named claude-code-sso-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, and jq to edit the Claude Code settings file during cleanup.

  1. Install kongctl from the kongctl documentation.
  2. Install jq from jqlang.org.
  3. Verify that both are installed:

    kongctl version
    jq --version

This tutorial uses an Okta organization with admin access. You create a Native OIDC application for Claude Code, add a groups claim to the access token, and assign one test user to one group.

Create the Native application

The Native application is a public client that represents Claude Code. It uses Authorization Code with PKCE and no client secret (Okta: Create OIDC app integrations).

  1. In the Okta Admin Console, go to Applications and Resources > Applications, click Create App Integration, and select Classic experience.
  2. Select OIDC - OpenID Connect and Native Application, then click Next.
  3. Name the application Claude Code.
  4. Under Grant type, select Authorization Code and Refresh Token. Without the Refresh Token grant, Okta doesn’t issue refresh tokens and developers go through the browser sign-in each time a token expires.
  5. Set the sign-in redirect URI to http://localhost:9876/callback. The helper script that you install in Connect Claude Code to Kong listens on this address.
  6. Click Save.
  7. In Client Credentials, confirm that client authentication is None and that Proof Key for Code Exchange (PKCE) is required.
  8. Copy the Client ID.

Check the authorization server scopes

The helper script requests openid, profile, email, and offline_access. Okta’s default authorization server includes these scopes. Without offline_access, Okta doesn’t issue refresh tokens.

  1. Go to Security > API and select the default authorization server, or your custom server.
  2. Click the Scopes tab and confirm that the four scopes are listed. If offline_access is missing, add it.

Add the groups claim

The OpenID Connect AI Auth Strategy reads the groups claim from the access token and maps each value to the AI Consumer Group with the same name (Okta: Customize tokens with a groups claim).

  1. On the same authorization server, click the Claims tab, then click Add Claim.
  2. Set Name to groups, keep Include in token type as Access Token, and set Value type to Groups.
  3. Set Filter to Matches regex with the value .*, or restrict it to a prefix such as claude-.
  4. Click Create.

Create a group and a test user

The Kong configuration has two AI Consumer Groups, claude-standard-users and claude-power-users. Start with one Okta group and one user. Swap to the power tier moves the user to the other tier.

  1. Go to Directory > Groups and create claude-standard-users.
  2. Go to Directory > People and add a user, for example claude-test-user@example.com, or pick an existing user who can sign in.
  3. Open the user, click the Groups tab, and add the user to claude-standard-users.
  4. Open the Claude Code application, click the Assignments tab, and assign the same user. Without this assignment, Okta blocks the sign-in.

Export the Okta values

Export the authorization server’s issuer URL and audience. The audience is set on the authorization server, in its Settings tab. The default server uses api://default. OIDC_CACHE_TOKENS_SALT is a stable string that the OpenID Connect AI Auth Strategy uses to build its token cache keys. It isn’t a credential:

export OKTA_ISSUER='https://your-org.okta.com/oauth2/default'
export OKTA_AUDIENCE='api://default'
export OIDC_CACHE_TOKENS_SALT='claude-code-sso-dev-salt'

If you use a custom authorization server, set OKTA_ISSUER to its issuer URL and OKTA_AUDIENCE to its audience.

This tutorial uses Claude Code.

  1. Install Claude Code from the setup guide.
  2. Verify that it’s installed:

    claude --version
  3. Unset ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN in your shell. Claude Code uses either variable before it runs apiKeyHelper (authentication precedence), so the recipe’s helper script would never run:

    unset ANTHROPIC_API_KEY ANTHROPIC_AUTH_TOKEN

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 give Claude Code access through a shared provider API key on each developer machine, which causes the following problems:

Problem

Solution

A shared key in shell profiles, .env files, or CI secrets makes every developer’s usage look the same, and a leaked key means rotating it on every machine and pipeline. The AI Model Provider keeps the provider credentials in Kong. Developers authenticate with their own Okta token, and Kong attributes each request to an Okta user.
The API key has no link to the identity provider. A developer removed from an Okta group can keep calling the API, because nothing checks the key against Okta. Claude SSO covers the claude /login subscription path, not API keys. The OpenID Connect AI Auth Strategy validates the Okta JWT on each request. When the token expires, the next token carries the user’s current groups.
Each developer with a valid key gets the same models and the same budget. Cost control happens afterward, through billing alerts. AI Consumer Groups map Okta groups to tiers. Access control lists on the AI Models decide which tier can use Opus, and the AI Rate Limiting Advanced Policy gives each tier a token budget.

Try it out

Apply the configuration, connect Claude Code to Kong, then use Claude Code to test the tier boundary, the token budget, and a tier change.

Apply the Kong configuration

The configuration creates an AI Model Provider with your provider’s credentials, an OpenID Connect AI Auth Strategy for Okta, two AI Consumer Groups (claude-standard-users and claude-power-users) with an AI Rate Limiting Advanced Policy each, and two AI Models (a sonnet tier and an opus tier) that allow access by AI Consumer Group. 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}"

Select your provider. Each tab exports four values, then applies the configuration:

  • SONNET_ALIAS and OPUS_ALIAS: the model names that Claude Code sends for the standard and power tiers.
  • CHAT_MODEL_1 and CHAT_MODEL_2: the upstream models that Kong sends to the provider for each tier.

The Okta values and the provider credentials come from the prerequisites.

The two alias values must match ANTHROPIC_DEFAULT_SONNET_MODEL and ANTHROPIC_DEFAULT_OPUS_MODEL in each developer’s Claude Code settings, and the models must exist in your provider account. Claude Code chooses which features to send (effort levels, extended thinking, beta tool fields) from the model name. If the alias names a newer model than the upstream one, for example claude-sonnet-4-6 for a Sonnet 4.5 upstream, the provider rejects those fields with 400.

Create the Claude Code Usage dashboard

The dashboard shows cost, token usage, request volume, model mix, usage for each developer (AI Consumer), and latency for Claude Code traffic. It’s an organization-level resource, so the command reuses a dashboard with the claude-code-sso-recipe label if one exists.

Create the dashboard definition:

cat <<'EOF' > claude-code-usage-dashboard.json
{
  "name": "Claude Code Usage",
  "definition": {
    "tiles": [
      {
        "id": "e3f9588a-71fd-4996-818f-33c93afe6eae",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 2,
            "rows": 1
          },
          "position": {
            "col": 0,
            "row": 0
          }
        },
        "definition": {
          "chart": {
            "type": "single_value",
            "chart_title": "Total cost ($)"
          },
          "query": {
            "filters": [
              {
                "field": "ai_provider",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "cost"
            ],
            "datasource": "llm_usage",
            "dimensions": []
          }
        }
      },
      {
        "id": "9edab295-d705-403f-a612-59847420a195",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 2,
            "rows": 1
          },
          "position": {
            "col": 2,
            "row": 0
          }
        },
        "definition": {
          "chart": {
            "type": "single_value",
            "chart_title": "Total Tokens"
          },
          "query": {
            "filters": [
              {
                "field": "ai_provider",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "total_tokens"
            ],
            "datasource": "llm_usage",
            "dimensions": []
          }
        }
      },
      {
        "id": "e5cc504e-c505-4f0b-8633-638a0e7b5833",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 2,
            "rows": 1
          },
          "position": {
            "col": 4,
            "row": 0
          }
        },
        "definition": {
          "chart": {
            "type": "single_value",
            "chart_title": "Total Claude Code requests"
          },
          "query": {
            "filters": [
              {
                "field": "ai_provider",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "ai_request_count"
            ],
            "datasource": "llm_usage",
            "dimensions": []
          }
        }
      },
      {
        "id": "4ab1fb73-d60b-4f77-b915-a0c88d427263",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 3,
            "rows": 2
          },
          "position": {
            "col": 0,
            "row": 1
          }
        },
        "definition": {
          "chart": {
            "type": "top_n",
            "chart_title": "Top Claude Models by Usage"
          },
          "query": {
            "limit": 10,
            "filters": [],
            "metrics": [
              "total_tokens",
              "ai_request_count"
            ],
            "datasource": "llm_usage",
            "dimensions": [
              "ai_request_model"
            ]
          }
        }
      },
      {
        "id": "e6c5fbe0-9dd2-46ac-af1c-3ba3af9ffd43",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 3,
            "rows": 2
          },
          "position": {
            "col": 3,
            "row": 1
          }
        },
        "definition": {
          "chart": {
            "type": "timeseries_line",
            "stacked": false,
            "chart_title": "Model usage trend (top 5)"
          },
          "query": {
            "limit": 5,
            "filters": [
              {
                "field": "ai_provider",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "total_tokens"
            ],
            "datasource": "llm_usage",
            "dimensions": [
              "ai_request_model",
              "time"
            ]
          }
        }
      },
      {
        "id": "fd14bf28-0cd2-409e-9a48-2cbbd7907409",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 2,
            "rows": 2
          },
          "position": {
            "col": 0,
            "row": 3
          }
        },
        "definition": {
          "chart": {
            "type": "donut",
            "chart_title": "Claude health check"
          },
          "query": {
            "filters": [
              {
                "field": "gateway_service",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "ai_request_count"
            ],
            "datasource": "llm_usage",
            "dimensions": [
              "status_code_grouped"
            ]
          }
        }
      },
      {
        "id": "f26edaa8-56d5-49f8-8f04-ade9451c76ec",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 2,
            "rows": 2
          },
          "position": {
            "col": 2,
            "row": 3
          }
        },
        "definition": {
          "chart": {
            "type": "donut",
            "chart_title": "Claude provider usage"
          },
          "query": {
            "filters": [
              {
                "field": "gateway_service",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "ai_request_count"
            ],
            "datasource": "llm_usage",
            "dimensions": [
              "ai_provider"
            ]
          }
        }
      },
      {
        "id": "d27037a3-7ace-49cc-affb-3b34a71ab9b1",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 2,
            "rows": 2
          },
          "position": {
            "col": 4,
            "row": 3
          }
        },
        "definition": {
          "chart": {
            "type": "timeseries_bar",
            "stacked": true,
            "chart_title": "LLM latency (avg)"
          },
          "query": {
            "filters": [
              {
                "field": "ai_request_model",
                "operator": "not_empty"
              },
              {
                "field": "ai_request_model",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "llm_latency_average"
            ],
            "datasource": "llm_usage",
            "dimensions": [
              "ai_request_model",
              "time"
            ]
          }
        }
      },
      {
        "id": "5f88c24b-80de-4fe1-8948-ef0bde29f948",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 3,
            "rows": 2
          },
          "position": {
            "col": 0,
            "row": 9
          }
        },
        "definition": {
          "chart": {
            "type": "horizontal_bar",
            "stacked": true,
            "chart_title": "Claude model usage (requests)"
          },
          "query": {
            "filters": [
              {
                "field": "ai_request_model",
                "operator": "not_empty"
              },
              {
                "field": "ai_request_model",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "ai_request_count"
            ],
            "datasource": "llm_usage",
            "dimensions": [
              "ai_request_model"
            ]
          }
        }
      },
      {
        "id": "702f6291-e02d-47eb-b38b-85438c643712",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 3,
            "rows": 2
          },
          "position": {
            "col": 3,
            "row": 9
          }
        },
        "definition": {
          "chart": {
            "type": "vertical_bar",
            "stacked": true,
            "chart_title": "Claude model usage count (tokens)"
          },
          "query": {
            "filters": [
              {
                "field": "ai_provider",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "total_tokens"
            ],
            "datasource": "llm_usage",
            "dimensions": [
              "ai_request_model"
            ]
          }
        }
      },
      {
        "id": "2242b427-2999-4cfe-b17e-9e4ca61be362",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 2,
            "rows": 2
          },
          "position": {
            "col": 0,
            "row": 11
          }
        },
        "definition": {
          "chart": {
            "type": "vertical_bar",
            "stacked": true,
            "chart_title": "AI security report"
          },
          "query": {
            "filters": [
              {
                "field": "status_code_grouped",
                "value": [
                  "4XX"
                ],
                "operator": "in"
              },
              {
                "field": "ai_provider",
                "operator": "not_empty"
              },
              {
                "field": "ai_provider",
                "value": [
                  "UNSPECIFIED"
                ],
                "operator": "not_in"
              }
            ],
            "metrics": [
              "ai_request_count"
            ],
            "datasource": "llm_usage",
            "dimensions": [
              "route",
              "status_code"
            ]
          }
        }
      },
      {
        "id": "df1b0486-2c04-430e-a8ff-7e26c7821bf7",
        "type": "chart",
        "layout": {
          "size": {
            "cols": 3,
            "rows": 2
          },
          "position": {
            "col": 2,
            "row": 11
          }
        },
        "definition": {
          "chart": {
            "type": "timeseries_bar",
            "stacked": true,
            "chart_title": "Monthly spend trends"
          },
          "query": {
            "limit": 10,
            "filters": [],
            "metrics": [
              "cost"
            ],
            "datasource": "llm_usage",
            "dimensions": [
              "ai_request_model",
              "time"
            ],
            "time_range": {
              "type": "relative",
              "time_range": "30d"
            },
            "granularity": "daily"
          }
        }
      }
    ],
    "template_id": "AI_GATEWAY",
    "preset_filters": [
      {
        "field": "gateway_service",
        "value": [],
        "operator": "in"
      }
    ]
  },
  "labels": {
    "recipe": "claude-code-sso-recipe"
  }
}
EOF

Create the dashboard in Konnect:

EXISTING_DASHBOARDS=$(kongctl api get "/v2/dashboards?filter%5Blabels.recipe%5D=claude-code-sso-recipe" \
  --pat "${KONNECT_TOKEN}" -o json --jq '.data | length')

if [ "${EXISTING_DASHBOARDS}" -gt 0 ]; then
  echo "Claude Code Usage dashboard already exists. Reusing."
else
  DASHBOARD_ID=$(kongctl api post /v2/dashboards \
    -f claude-code-usage-dashboard.json \
    --pat "${KONNECT_TOKEN}" -o json --jq '.id' -r)
  echo "Created Claude Code Usage dashboard (id: ${DASHBOARD_ID})."
fi

The dashboard shows Claude Code traffic for the whole organization. To show only this cookbook’s AI Gateway, add a filter in the Konnect dashboard editor.

Connect Claude Code to Kong

Claude Code gets its credential from an apiKeyHelper script. Claude Code runs the script, then sends its output as both the X-Api-Key and Authorization: Bearer headers. The following script runs the OAuth Authorization Code flow with PKCE against Okta, caches the tokens in ~/.claude/okta-cache/, and uses the refresh token to get a new access token before the cached one expires.

Save the script to ~/.claude/okta-claude-auth.sh and make it executable:

cat <<'SCRIPT' > ~/.claude/okta-claude-auth.sh
#!/usr/bin/env bash
# okta-claude-auth.sh: apiKeyHelper for Claude Code + Okta PKCE
set -euo pipefail

OKTA_DOMAIN="${OKTA_DOMAIN:-"https://your-org.okta.com"}"
CLIENT_ID="${OKTA_CLIENT_ID:-"0oa1b2c3d4YourClientId"}"
REDIRECT_PORT="${OKTA_REDIRECT_PORT:-"9876"}"
REDIRECT_URI="http://localhost:${REDIRECT_PORT}/callback"
SCOPES="openid profile email offline_access"
AUDIENCE="${OKTA_AUDIENCE:-"api://default"}"
AUTH_SERVER="${OKTA_AUTH_SERVER:-"default"}"

CACHE_DIR="${HOME}/.claude/okta-cache"
TOKEN_CACHE="${CACHE_DIR}/tokens.json"
LOCK_FILE="${CACHE_DIR}/auth.lock"

log()  { echo "[okta-auth] $*" >&2; }
die()  { echo "[okta-auth] ERROR: $*" >&2; exit 1; }
b64url() { openssl base64 -A | tr '+/' '-_' | tr -d '='; }
random_str() { openssl rand -hex "${1:-32}"; }
json_get() { echo "$1" | grep -o "\"$2\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" | sed 's/.*: *"\([^"]*\)".*/\1/'; }
json_get_num() { echo "$1" | grep -o "\"$2\"[[:space:]]*:[[:space:]]*[0-9]*" | grep -o '[0-9]*$'; }

mkdir -p "$CACHE_DIR" && chmod 700 "$CACHE_DIR"

cache_read() { [[ -f "$TOKEN_CACHE" ]] && cat "$TOKEN_CACHE" || echo "{}"; }
cache_write() { echo "$1" > "$TOKEN_CACHE" && chmod 600 "$TOKEN_CACHE"; }

access_token_valid() {
  local cache exp now tok
  cache=$(cache_read)
  tok=$(json_get "$cache" "access_token")
  exp=$(json_get_num "$cache" "expires_at")
  now=$(date +%s)
  [[ -z "$tok" || -z "$exp" ]] && return 1
  (( now < exp - 60 )) && { echo "$tok"; return 0; }
  return 1
}

do_refresh() {
  local cache refresh_tok response new_access new_refresh expires_in expires_at now
  cache=$(cache_read)
  refresh_tok=$(json_get "$cache" "refresh_token")
  [[ -z "$refresh_tok" ]] && return 1
  log "Attempting silent refresh..."
  response=$(curl -sf -X POST "${OKTA_DOMAIN}/oauth2/${AUTH_SERVER}/v1/token" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d "grant_type=refresh_token&refresh_token=${refresh_tok}&client_id=${CLIENT_ID}&scope=${SCOPES// /%20}") || return 1
  new_access=$(json_get "$response" "access_token")
  new_refresh=$(json_get "$response" "refresh_token")
  expires_in=$(json_get_num "$response" "expires_in")
  [[ -z "$new_access" ]] && return 1
  now=$(date +%s); expires_at=$(( now + ${expires_in:-3600} ))
  cache_write "{\"access_token\":\"${new_access}\",\"refresh_token\":\"${new_refresh:-$refresh_tok}\",\"expires_at\":${expires_at}}"
  log "Token refreshed successfully."
  echo "$new_access"
}

start_callback_server() {
  local expected_state="$1"
  python3 - "$REDIRECT_PORT" "$expected_state" <<'PYEOF'
import sys, socket, urllib.parse
port, expected_state = int(sys.argv[1]), sys.argv[2]
HTML_OK = b"<html><body><h2>Authenticated! Close this tab.</h2><script>window.close();</script></body></html>"
HTML_ERR = b"<html><body><h2>Authentication failed.</h2></body></html>"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("127.0.0.1", port)); sock.listen(1); sock.settimeout(120)
try:
    conn, _ = sock.accept(); data = b""
    while b"\r\n\r\n" not in data:
        chunk = conn.recv(4096)
        if not chunk: break
        data += chunk
    path = data.decode(errors="replace").split(" ")[1] if b" " in data else "/"
    params = urllib.parse.parse_qs(urllib.parse.urlparse(path).query)
    code, state = params.get("code",[None])[0], params.get("state",[None])[0]
    error = params.get("error",[None])[0]
    ok = code and state == expected_state and not error
    conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n" + (HTML_OK if ok else HTML_ERR))
    conn.close()
    if not ok: sys.exit(1)
    print(code, end="")
except socket.timeout:
    sys.stderr.write("[okta-auth] Timed out waiting for callback.\n"); sys.exit(1)
finally: sock.close()
PYEOF
}

do_auth_code_flow() {
  local verifier challenge state auth_url code response access_tok refresh_tok expires_in expires_at now
  verifier=$(random_str 32)
  challenge=$(echo -n "$verifier" | openssl dgst -binary -sha256 | b64url)
  state=$(random_str 16)
  auth_url="${OKTA_DOMAIN}/oauth2/${AUTH_SERVER}/v1/authorize?response_type=code&client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&scope=${SCOPES// /+}&audience=${AUDIENCE}&state=${state}&code_challenge=${challenge}&code_challenge_method=S256"
  log "Opening browser for Okta login..."
  log "If the browser doesn't open, visit: ${auth_url}"
  if command -v open >/dev/null 2>&1; then
    open "$auth_url" &
  elif command -v xdg-open >/dev/null 2>&1; then
    xdg-open "$auth_url" >/dev/null 2>&1 &
  fi
  log "Waiting for callback on http://localhost:${REDIRECT_PORT}/callback ..."
  code=$(start_callback_server "$state")
  [[ -z "$code" ]] && die "No authorization code received."
  log "Authorization code received. Exchanging for tokens..."
  response=$(curl -sf -X POST "${OKTA_DOMAIN}/oauth2/${AUTH_SERVER}/v1/token" \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d "grant_type=authorization_code&code=${code}&redirect_uri=${REDIRECT_URI}&client_id=${CLIENT_ID}&code_verifier=${verifier}") || die "Token exchange failed."
  access_tok=$(json_get "$response" "access_token")
  refresh_tok=$(json_get "$response" "refresh_token")
  expires_in=$(json_get_num "$response" "expires_in")
  [[ -z "$access_tok" ]] && die "No access_token in response."
  now=$(date +%s); expires_at=$(( now + ${expires_in:-3600} ))
  cache_write "{\"access_token\":\"${access_tok}\",\"refresh_token\":\"${refresh_tok}\",\"expires_at\":${expires_at}}"
  log "Authentication successful. Token cached."
  echo "$access_tok"
}

acquire_lock() {
  local waited=0
  while ! mkdir "$LOCK_FILE" 2>/dev/null; do
    sleep 0.5; (( waited++ ))
    (( waited > 30 )) && { log "Lock timeout, removing stale lock"; rm -rf "$LOCK_FILE"; }
  done
  trap 'rm -rf "$LOCK_FILE"' EXIT INT TERM
}

main() {
  acquire_lock
  tok=$(access_token_valid) && { echo "$tok"; exit 0; }
  tok=$(do_refresh 2>/dev/null) && { echo "$tok"; exit 0; }
  tok=$(do_auth_code_flow)
  echo "$tok"
}

main "$@"
SCRIPT
chmod +x ~/.claude/okta-claude-auth.sh

Create or update ~/.claude/settings.json with the tab for your provider. Replace https://your-org.okta.com and 0oa1b2c3d4YourClientId with your Okta domain and the Client ID from the Okta prerequisite. Set OKTA_AUTH_SERVER to your server ID if you don’t use the default server.

The Bedrock, Azure AI Foundry, and Vertex AI tabs set CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS to 1. This removes Anthropic’s pre-release anthropic-beta headers and beta request fields, which a non-Anthropic provider can reject with an Extra inputs are not permitted error (Claude Code environment variables).

The env block sets these values:

  • ANTHROPIC_BASE_URL sends Claude Code’s requests to Kong.
  • OKTA_DOMAIN, OKTA_CLIENT_ID, OKTA_AUDIENCE, and OKTA_AUTH_SERVER configure the helper script.
  • ANTHROPIC_MODEL sets the model at session start, so the first request already uses a name that Kong knows.
  • ANTHROPIC_DEFAULT_SONNET_MODEL and ANTHROPIC_DEFAULT_OPUS_MODEL set the model names for /model sonnet and /model opus. They must match SONNET_ALIAS and OPUS_ALIAS. When you move a tier to a new model version, update the four values in the apply step and these three values together.

Claude Code caches the helper’s output and runs the script again after five minutes, after a 401 or 403 response, and before a request if the cached JWT has expired (apiKeyHelper reference). If your Okta access tokens expire in less than five minutes and your Claude Code version doesn’t check JWT expiry, set CLAUDE_CODE_API_KEY_HELPER_TTL_MS in the env block to a shorter interval in milliseconds.

On Windows, Claude Code doesn’t expand ~ in the apiKeyHelper path (GitHub issue). Use the absolute path, for example C:\\Users\\you\\.claude\\okta-claude-auth.sh.

Launch Claude Code

Start Claude Code:

claude

The first time, or after the refresh token expires, a browser window opens for the Okta sign-in. After you sign in, the terminal continues:

[okta-auth] Opening browser for Okta login...
[okta-auth] Waiting for callback on http://localhost:9876/callback ...
[okta-auth] Authorization code received. Exchanging for tokens...
[okta-auth] Authentication successful. Token cached.

Ask Claude Code a question. Kong validates your Okta JWT, attaches the claude-standard-users AI Consumer Group, checks the AI Model’s access control list, adds the provider credentials, and forwards the request. Later sessions reuse the cached token without opening the browser.

Hit the tier enforcement boundary

Standard users can only use the sonnet AI Model. In Claude Code, switch to Opus and send a prompt:

> /model opus
> Hello

Claude Code sends the OPUS_ALIAS value as the model. Kong matches it to the opus AI Model, but that AI Model’s access control list doesn’t include claude-standard-users, so Kong rejects the request before any provider call. The expected status is 403 Forbidden, which this cookbook hasn’t confirmed against a live AI Gateway:

API Error: 403 Forbidden

Run /model sonnet and the requests succeed again.

Hit the rate limit

The standard tier allows 20,000 tokens in a 60-second sliding window. Send three or four prompts in quick succession that each carry context, for example by asking Claude Code to summarize files in your repository. When the tier uses up its budget, Kong returns:

API Error: 429 Too Many Requests

The X-AI-RateLimit-Retry-After response header gives the number of seconds until the window allows requests again.

Swap to the power tier

The groups claim decides the AI Consumer Group, so you change a user’s tier in Okta. The claude-power-users AI Consumer Group, its AI Rate Limiting Advanced Policy, and the opus AI Model that allows it already exist.

  1. In Okta, go to Directory > Groups and create claude-power-users if it doesn’t exist.
  2. Open your test user, click the Groups tab, remove claude-standard-users, and add claude-power-users.
  3. Clear the helper script’s cache, so that it doesn’t reuse the old JWT with the old groups claim:

    rm -rf ~/.claude/okta-cache/
  4. Start Claude Code again and sign in. The new JWT has groups: ["claude-power-users"]. Run /model opus and send a prompt. The request succeeds, and the budget is 100,000 tokens in a 60-second window.

To swap back, move the user to claude-standard-users, clear the cache, and sign in again.

Check the results

The Claude Code sessions confirm the following:

Behavior

Evidence

Okta authentication Claude Code works only after the Okta sign-in. Developers never hold a provider key.
Tier enforcement A standard user gets an error for /model opus and succeeds with /model sonnet. A power user succeeds with both.
Token budget for each tier A standard user gets 429 Too Many Requests after about 20,000 tokens in a minute. A power user has 100,000.
Tiers follow Okta groups Moving the user to another Okta group changes the tier after the next sign-in, with no Kong configuration change.

To review the created entities and usage by AI Consumer Group, open the claude-code-sso-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu. The Claude Code Usage dashboard is in Observability > Dashboards.

How it works

Claude Code gets an Okta JWT from the helper script and sends it to Kong. Kong validates the JWT, maps the user’s Okta groups to an AI Consumer Group, checks the access control list of the AI Model that matches the requested model, applies the tier’s token budget, and forwards the request with the provider credentials.

sequenceDiagram
    participant CC as Claude Code
    participant H as apiKeyHelper script
    participant O as Okta
    participant K as Kong AI Gateway
    participant L as LLM Provider

    CC->>H: Request a credential
    activate H
    alt Cached token still valid
        H-->>CC: Cached JWT
    else Refresh or PKCE sign-in
        H->>O: Refresh token or browser sign-in
        activate O
        O-->>H: Access token (JWT) and refresh token
        deactivate O
        H-->>CC: New JWT
    end
    deactivate H

    CC->>K: POST /claude-code-sso (Authorization: Bearer JWT)
    activate K
    K->>K: OpenID Connect AI Auth Strategy validates the JWT (else 401)
    K->>K: Map Okta groups to an AI Consumer Group
    K->>K: AI Model access control list (else 403)
    K->>K: AI Rate Limiting Advanced Policy (else 429)
    K->>L: Request with the provider credentials
    activate L
    L-->>K: Response
    deactivate L
    K-->>CC: Anthropic-format response
    deactivate K

OpenID Connect AI Auth Strategy

Authentication is an AI Auth Strategy, not an AI Policy. The OpenID Connect AI Auth Strategy validates each JWT against Okta’s signing keys, rejects tokens that expired or were issued for another audience, and maps the values in the groups claim to AI Consumer Groups with the same names. The AI Consumer Group is the identity that the access control lists, rate limits, and analytics use.

The following settings configure the OpenID Connect AI Auth Strategy:

Setting

Purpose

issuer The Okta authorization server URL. Kong reads its OpenID Connect discovery document to find the signing keys, and caches the keys.
auth_methods: [bearer] Kong reads the JWT from the Authorization: Bearer header.
audience_required The JWT’s aud claim must match OKTA_AUDIENCE, so tokens issued for other applications are rejected.
consumer_groups_claim: [groups] Kong maps each value in the groups claim to the AI Consumer Group with the same name.
consumer_optional: true Requests don’t need an AI Consumer for each user. The AI Consumer Group is enough.
consumer_groups_optional: false Kong rejects an authenticated user whose groups match no AI Consumer Group, so an Okta user without a tier can’t use Claude Code.
upstream_headers_claims and upstream_headers_names Kong sends the JWT’s sub and email claims upstream as X-Authenticated-User and X-User-Email, for audit logs.
ssl_verify: true Kong verifies Okta’s TLS certificate when it fetches the discovery document and the signing keys.
hide_credentials: true Kong removes the JWT before it forwards the request. The provider only gets the AI Model Provider’s credentials.
cache_tokens_salt A stable salt for the token cache keys, from OIDC_CACHE_TOKENS_SALT. In production, generate it with openssl rand -hex 16 and store it in a secret manager.

Kong adds X-Consumer-Username and related headers to the upstream request, not to the response. Usage for each AI Consumer Group appears in Konnect analytics.

AI Model Provider and AI Models

The AI Model Provider holds the provider credentials. The two AI Models, claude-code-sso-sonnet and claude-code-sso-opus, share the /claude-code-sso path, and each one answers to one model name. Each AI Model requires the OpenID Connect AI Auth Strategy and has an access control list of AI Consumer Groups. Claude Code sends the alias, and Kong sends the target’s upstream model to the provider, so developers keep the same alias when you change the upstream model.

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

Setting

Purpose

config.auth (AI Model Provider) The provider credentials. Kong adds them to each upstream request. The fields depend on the provider tab.
formats: [{type: anthropic}] The AI Model accepts Anthropic Messages API requests, which is the format Claude Code sends. Kong forwards the Anthropic format to each provider’s Claude endpoint.
access.auth_strategies Each request must have a valid Okta JWT.
access.acls.allow The AI Consumer Groups that can use the AI Model. The sonnet AI Model allows both groups. The opus AI Model allows only claude-power-users. Kong rejects other groups before any provider call. The expected status is 403 Forbidden, which this cookbook hasn’t confirmed against a live AI Gateway.
config.route.model Kong reads the model field of the request body (body_param: model) and selects the AI Model whose value matches: SONNET_ALIAS or OPUS_ALIAS. An AI Model accepts one value, so each tier needs its own AI Model.
targets[].name The upstream model, from CHAT_MODEL_1 or CHAT_MODEL_2. For Anthropic it’s the same name as the alias. For Bedrock it’s a model ID such as global.anthropic.claude-sonnet-4-5-20250929-v1:0. Change it and re-apply to upgrade a tier.
targets[].config The provider type and provider-specific target fields, for example Bedrock’s region or Azure AI Foundry’s foundry_path_prefix.
config.max_request_body_size: 10485760 Accepts request bodies up to 10 MB, instead of the 8 MB default. Claude Code requests carry conversation history, tool results, and file contents.
config.response_streaming: allow Kong passes streamed responses to Claude Code, which streams its terminal output.
config.logging.payloads: true Kong logs request and response payloads for audits. The default is false.

Don’t use formats: [{type: openai}] with Claude Code. Claude Code sends Anthropic tool definitions, and the translation to the OpenAI format breaks them, so the provider returns 400 errors (typically tools: Input should be a valid list).

AI Rate Limiting Advanced Policy and AI Consumer Groups

Each AI Consumer Group lists its own AI Rate Limiting Advanced Policy in its policies. The policy counts prompt and completion tokens, the unit that drives LLM cost. The standard tier gets 20,000 tokens in a 60-second window, and the power tier gets 100,000. When a tier uses up its budget, Kong returns 429 Too Many Requests until the window allows requests again.

The following settings configure the AI Consumer Groups and the AI Rate Limiting Advanced Policies:

Setting

Purpose

ai_gateway_consumer_groups[].policies Attaches the policy to each request that Kong maps to the AI Consumer Group. To add a tier, add an AI Consumer Group with its own policy. This attachment hasn’t been confirmed against a live AI Gateway.
config.policies[].limits The token limit and the window size in seconds: 20000 and 60 for the standard tier, 100000 and 60 for the power tier.
window_type: sliding A sliding window, for smoother limits than the fixed window.
tokens_count_strategy: total_tokens Counts prompt and completion tokens. Other values count only prompt tokens, only completion tokens, or cost.
identifier: consumer-group Counts usage for each AI Consumer Group, so the budget is shared by the tier.
strategy: local Keeps the counters in memory on each data plane node. For several nodes, use redis so that the nodes share the counters.

The 60-second windows let a few prompts use up the budget in this cookbook. In production, most teams use daily or monthly budgets, for example limits: [{limit: 5000000, window_size: 2592000}]. Store the credentials in AI Vaults instead of environment variables.

Response headers

Kong adds the following rate limit headers to each response:

Header

Description

X-AI-RateLimit-Limit-minute-policy-1 The token limit for the 60-second window of the first entry in config.policies.
X-AI-RateLimit-Remaining-minute-policy-1 The tokens left in the current window.
X-AI-RateLimit-Reset The seconds until the window resets.
X-AI-RateLimit-Retry-After On a 429 response, the seconds until the tier can send requests again. There is no plain Retry-After header.

Variations and next steps

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

Variation

How

Upgrade a tier’s model Change CHAT_MODEL_1 or CHAT_MODEL_2 and re-apply. Developers keep sending the same alias. If the new model is a newer version, also update the aliases and each developer’s settings, as the warning in Apply the Kong configuration explains.
Change the token budgets Change limits in each AI Rate Limiting Advanced Policy, for example limits: [{limit: 5000000, window_size: 2592000}] for 5 million tokens a month. Add several limits entries to set a burst limit and a sustained limit in the same policy.
Share rate limit counters Set strategy: redis and point the policy at a shared Redis instance, so that all data plane nodes use the same counters.
Restrict access by IP address Add an IP Restriction Policy with allow set to your egress ranges, for example your VPN and CI runners.
Add a tier Create an Okta group, an AI Consumer Group with the same name, an AI Rate Limiting Advanced Policy for it, and an AI Model that allows it. For example, an intern tier with only Haiku.
Use another identity provider Change issuer and the claim names on the OpenID Connect AI Auth Strategy for Microsoft Entra ID, Auth0, Keycloak, or another OpenID Connect provider. The helper script works with any provider that supports Authorization Code with PKCE. The AI Models and AI Consumer Groups don’t change.
Distribute the helper script Ship okta-claude-auth.sh and the Claude Code settings through your internal tooling, for example a Homebrew tap, a package manager, or Claude Code managed settings, instead of having each developer copy the script from this page.
Support clients other than Claude Code For OpenAI-format clients and providers such as OpenAI, Gemini, or Mistral, use formats: [{type: openai}], as in the Basic LLM Routing cookbook.

Cleanup

The recipe’s kongctl namespace scoped all resources, so this teardown removes only this recipe’s configuration.

The Claude Code Usage dashboard is an organization-level resource that outlives the AI Gateway. Delete it first:

DASHBOARD_IDS=$(kongctl api get "/v2/dashboards?filter%5Blabels.recipe%5D=claude-code-sso-recipe" \
  --pat "${KONNECT_TOKEN}" -o json --jq '.data[].id' -r)

if [ -z "${DASHBOARD_IDS}" ]; then
  echo "No Claude Code Usage dashboard found. Skipping."
else
  for id in ${DASHBOARD_IDS}; do
    if kongctl api delete "/v2/dashboards/${id}" --pat "${KONNECT_TOKEN}"; then
      echo "Deleted Claude Code Usage dashboard ${id}."
    else
      echo "Failed to delete dashboard ${id}."
    fi
  done
fi

Tear down the local data plane and delete the AI Gateway from Konnect:

export KONNECT_CONTROL_PLANE_NAME='claude-code-sso-recipe' && curl -Ls https://get.konghq.com/ai | bash -s -- -d -k $KONNECT_TOKEN

Remove the dashboard definition, the helper script, and the cached tokens:

rm -f claude-code-usage-dashboard.json
rm -f ~/.claude/okta-claude-auth.sh
rm -rf ~/.claude/okta-cache/

Revert ~/.claude/settings.json. The following jq command removes only the keys this cookbook added and keeps everything else in the file:

tmp=$(mktemp) && jq '
  del(.apiKeyHelper)
  | del(
      .env.ANTHROPIC_BASE_URL,
      .env.OKTA_DOMAIN,
      .env.OKTA_CLIENT_ID,
      .env.OKTA_AUDIENCE,
      .env.OKTA_AUTH_SERVER,
      .env.ANTHROPIC_MODEL,
      .env.ANTHROPIC_DEFAULT_SONNET_MODEL,
      .env.ANTHROPIC_DEFAULT_OPUS_MODEL,
      .env.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS
    )
  | if (.env // {}) == {} then del(.env) else . end
' ~/.claude/settings.json > "$tmp" && mv "$tmp" ~/.claude/settings.json

If you unset ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN for this cookbook, export them again from your shell profile to return to your previous Claude Code authentication.

Subscription seat users (Pro, Max, Team, or Enterprise): Claude Code attempts to fall back to your cached subscription credentials once apiKeyHelper is removed from settings.json. Run claude /status to confirm the session resumed, and run claude /login again if it didn’t.

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!