AI › AI Tools

Restricting model access with LiteLLM virtual keys

Give two virtual keys different model permissions in LiteLLM Proxy, then check which key can send each Jekyll summary request.

Restricting model access with LiteLLM virtual keys

I gave two apps their own virtual keys on the same LiteLLM Proxy. Both model aliases pointed to the same local model. Each key still saw and could call only the aliases I had allowed for it.

I ran the test with LiteLLM Proxy v1.111.1, PostgreSQL 17, Ollama 0.40.18, Bash, curl 8.7.1, and jq 1.7.1. The upstream was the local smollm2:135m-instruct-q4_K_M model. LiteLLM release · Ollama integration

Restrict what each app can call

In my OpenRouter request walkthrough, one app called a model with its own API key. A shared gateway adds a decision about which models each app needs. I used a reader app that can summarize releases and a reviewer app that can also call a separate review alias. The proxy checks the model permissions associated with each virtual key. The app sends the gateway’s model_name, and the gateway configuration identifies the deployment in litellm_params.model. This lets me keep app access rules in terms of the model names the apps see. LiteLLM virtual keys · Model configuration

What I needWhere I would start
One app calling one provider APICall the provider directly.
A shared endpoint with different model permissions per appRegister aliases in the proxy and issue app-specific virtual keys.
A policy shared across a teamDefine the team’s model list and check how its keys inherit access.

Point two aliases at one deployment

I ran the proxy and PostgreSQL with Docker Compose. The gateway received its database connection through DATABASE_URL. I bound the proxy to 127.0.0.1:4419 and ran Ollama on the host at port 4418. In this Docker Desktop setup, the container reached Ollama via host.docker.internal. In another environment, use an Ollama address the proxy can reach. LiteLLM Docker quickstart · Ollama provider setup

Both aliases in this excerpt use the same upstream. That kept the local inference deployment fixed while I changed the key and the alias in the request. The aim was to check access to the gateway names, so there was no need to compare the output of two separate models.

1
2
3
4
5
6
7
8
9
model_list:
  - model_name: release-summary
    litellm_params:
      model: ollama_chat/smollm2:135m-instruct-q4_K_M
      api_base: http://host.docker.internal:4418
  - model_name: release-review
    litellm_params:
      model: ollama_chat/smollm2:135m-instruct-q4_K_M
      api_base: http://host.docker.internal:4418

Issue one key per app

Set GATEWAY to http://127.0.0.1:4419 in Bash and set LITELLM_MASTER_KEY to the key configured for the proxy. I enabled set -o pipefail in the test shell so a failed command inside a pipeline returned a failure status. The example reads just .key from the creation response into a shell variable. I used the master key for issuance and the generated keys for the app requests. Each key had a one-hour lifetime for the local test. Generate virtual keys

1
2
3
4
5
6
7
8
9
10
11
12
13
issue_key() {
  local alias=$1
  shift
  jq -nc --arg alias "$alias" --args \
    '{key_alias: $alias, models: $ARGS.positional, duration: "1h"}' "$@" |
    curl -fsS "$GATEWAY/key/generate" \
      -H "Authorization: Bearer ${LITELLM_MASTER_KEY:?}" \
      -H 'Content-Type: application/json' --data-binary @- |
    jq -er '.key'
}

READER_KEY=$(issue_key reader-app release-summary)
REVIEWER_KEY=$(issue_key reviewer-app release-summary release-review)

The models array contains the names the apps send. The reader receives release-summary. The reviewer receives both release-summary and release-review. The separation matters even with a shared upstream because the client requests the alias and the key scopes access to that name.

Compare the catalog with the completion requests

I saved the same public Jekyll 4.4.0 excerpt as release-notes.md. The list_models helper reads the names visible to one key. The ask helper sends the model alias and the same note with a one-sentence summary prompt. Its third argument is the path where curl saves the response body, and the HTTP status goes to standard output. This made it possible to inspect a rejection body separately from the successful model output. Jekyll 4.4.0 release notes · LiteLLM client setup

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
list_models() {
  curl -fsS "$GATEWAY/v1/models" -H "Authorization: Bearer $1" |
    jq -r '[.data[].id] | sort | join(", ")'
}

ask() {
  jq -nc --arg model "$2" --rawfile notes release-notes.md '{
    model: $model,
    messages: [{role:"user", content: ("Summarize in one sentence:\n\n" + $notes)}],
    temperature: 0, max_tokens: 96
  }' |
    curl -sS "$GATEWAY/v1/chat/completions" \
      -H "Authorization: Bearer $1" -H 'Content-Type: application/json' \
      --data-binary @- -o "$3" -w '%{http_code}\n'
}

list_models "$READER_KEY" showed only release-summary. list_models "$REVIEWER_KEY" showed both aliases. I then ran the actual summary requests with the same keys.

KeyRequested aliasObserved response
reader-apprelease-summaryHTTP 200 with summary text
reader-apprelease-reviewHTTP 403, key_model_access_denied
reviewer-apprelease-reviewHTTP 200 with summary text

ask "$READER_KEY" release-review reader-review.json returned 403. When I changed the key to REVIEWER_KEY and sent the same model and prompt, the gateway returned 200 and nonempty text. I used jq -r '.error.type' reader-review.json to read the error type. The terminal excerpt keeps the model lists and status codes.

LiteLLM terminal output showing each virtual key's visible aliases and HTTP 403 and 200 for the same release-review request Figure 1. Model lists and HTTP statuses from the local LiteLLM Proxy connected to Ollama.

Check the path the app actually uses

A model picker built from GET /v1/models can tell the app which names to offer. A completion using the same key verifies what happens when it sends a name. I checked both because that comparison exposes stale app configuration after a rename or permission change. For team-managed keys, check the team’s model rules along with the permissions on the key. This exercise covered model access. Budgets and rate limits are separate settings. What a virtual key inherits · Budgets and rate limits

  • Keep the master key with the gateway administrator and distribute the scoped keys to apps.
  • Use the app key for both the catalog check and a permitted and forbidden completion.
  • After deploying a replacement key, revoke the old one and record the new key’s expiration and owner.
This post is licensed under CC BY 4.0 by the author.