Skip to content
Kaiov0.8.0

LiteLLM

Every agent stack on a host wants a provider key, and each copy of a key is one more place it can leak from. LiteLLM holds them all behind an OpenAI-compatible proxy, and hands each agent a virtual key with its own budget and model list, revocable without touching the provider.

In Kaio that means the real keys live in one stack’s secrets, and the other stacks only ever see a virtual key.

services:
litellm:
image: docker.litellm.ai/berriai/litellm:${LITELLM_TAG:-main-stable}
restart: unless-stopped
environment:
DATABASE_URL: postgresql://litellm:${POSTGRES_PASSWORD}@db:5432/litellm
LITELLM_MASTER_KEY: ${LITELLM_MASTER_KEY}
LITELLM_SALT_KEY: ${LITELLM_SALT_KEY}
STORE_MODEL_IN_DB: "True"
ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
OPENAI_API_KEY: ${OPENAI_API_KEY}
ports:
- "127.0.0.1:4000:4000"
networks:
- default
- llm
depends_on:
db:
condition: service_healthy
healthcheck:
test:
- CMD-SHELL
- python3 -c "import urllib.request; urllib.request.urlopen('http://localhost:4000/health/liveliness')"
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
db:
image: docker.io/library/postgres:16
restart: unless-stopped
environment:
POSTGRES_DB: litellm
POSTGRES_USER: litellm
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- db:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -d litellm -U litellm"]
interval: 2s
timeout: 5s
retries: 10
networks:
llm:
name: llm
volumes:
db:
  • STORE_MODEL_IN_DB keeps the model list in Postgres, edited from the admin UI, so the stack needs no config.yaml beside it. A file next to the Compose file would be a bind mount, and a copy leaves bind mounts behind.
  • The llm network has a fixed name so other stacks can join it. LiteLLM answers there as http://litellm:4000, and the database stays on the stack’s own network, out of their reach.
  • Port 4000 on the loopback is for you and the admin UI; agents use the network.
LITELLM_TAG=main-stable
POSTGRES_PASSWORD=generate-with-openssl-rand-hex-32
LITELLM_MASTER_KEY=sk-generate-with-openssl-rand-hex-32
LITELLM_SALT_KEY=sk-generate-with-openssl-rand-hex-32
ANTHROPIC_API_KEY=sk-ant-...
OPENAI_API_KEY=sk-...

Every one but the tag is flagged as a secret by its name. LITELLM_SALT_KEY encrypts the provider credentials stored in the database and cannot be rotated once models are added: the stack versions keep it, and a backup of Kaio’s database is now also a backup of that key.

Terminal window
kaio-cli stack deploy litellm ./compose.yml --env-file .env
kaio-cli stack logs litellm --follow

The admin UI answers on http://localhost:4000/ui, logged in with the master key. Add the models there.

Terminal window
curl -s http://localhost:4000/key/generate \
-H "Authorization: Bearer $LITELLM_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{"key_alias": "hermes", "max_budget": 20}'

The agent’s stack joins the network and carries that key, never a provider’s. For the Hermes Agent stack, the key goes in as a variable:

services:
hermes:
environment:
- LITELLM_VIRTUAL_KEY=${LITELLM_VIRTUAL_KEY}
networks:
- default
- llm
networks:
llm:
external: true

and Hermes is told where to find it, in /opt/data/config.yaml:

model:
default: claude-sonnet
provider: custom
base_url: http://litellm:4000/v1
key_env: LITELLM_VIRTUAL_KEY

default is a model name as you entered it in LiteLLM’s UI. key_env makes Hermes read the key from the environment, so it stays a Kaio secret rather than a line in the volume. Other agents take an OpenAI-compatible endpoint under their own setting names: check their documentation for the base URL.

A leaked virtual key is revoked in the UI, and the agent stack gets the new one with stack env set and stack apply.

Terminal window
kaio-cli stack check-update litellm # did main-stable move?
kaio-cli stack update litellm --wait
kaio-cli stack scan litellm
kaio-cli stack rollback litellm 3

With --wait, the update answers only once LiteLLM passes its /health/liveliness check, and puts back the images it ran before when it does not get there, so a broken main-stable never stays in front of the agents.

A rollback restores the Compose file and its variables, keys included, but not the database: models and virtual keys created since stay where they are.

Deploy this stack before the ones that join llm: it is the one that creates the network, and an external network has to exist when a stack comes up.

Kaio, built by Régis Gaidot