feat(licences): per-tenant LiteLLM key with a Werkpilot grant #29

Merged
sharang merged 1 commits from feat/licence-llm-key into main 2026-09-14 08:27:26 +00:00
Owner

Demonstration plan, phase D6 (registry side). A prospect trialling Werkpilot had no model access until someone created a LiteLLM key by hand. The registry issues one with the licence, scoped and budgeted per tenant.

On issue (product=werkpilot): POST /key/generate on LiteLLM with key_alias = werkpilot-<slug>-<licence id>, metadata carrying tenant id/slug/licence/product, the model list and the plan's budget (trial and default, USD per 30 days). LiteLLM's hashed token id goes on the licences row (migration 0007, exposed as llm_key_id on GET /v1/licences); the sk-… secret is returned in that one response as llm_api_key, together with llm_base_url, and is never stored or returned again.

On renewal (licence heartbeat): the existing key moves to the new licence row rather than a second key being generated — the secret cannot be re-read, and the CLI still holds the one it stored.

On revoke: the key is deleted; a 404 from LiteLLM is treated as already gone. If the delete fails, the licence is still revoked and the key id stays on the row so it can be retried by hand.

Failure is never fatal: with LITELLM_MASTER_KEY unset, LiteLLM unreachable or refusing, the licence is issued anyway and the grant carries warnings: ["llm_key_unavailable"] with no LLM fields. Also, a key that cannot be persisted is deleted again rather than handed out un-revocable.

Config: LITELLM_BASE_URL (default https://llm.breakpilot.com), LITELLM_MASTER_KEY, LITELLM_MODELS (default Qwen3-Coder-30B-A3B-Instruct,gpt-oss-120b), LITELLM_BUDGET_TRIAL (5), LITELLM_BUDGET_DEFAULT (50). Startup logs whether provisioning is on.

Note the migration is numbered 0007: 0006 is taken by the members PR (#28), which is independent of this one — merge order does not matter.

Verified locally: go vet ./..., gofmt clean, go test ./... green, CI coverage command at 74.4% (gate 70%). internal/llmkeys is tested against an httptest fake; no live LiteLLM call was made.

Pairs with a bp-licence bump (the Rust Grant gains the optional fields) and with LITELLM_MASTER_KEY on tenant-registry-stage.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB

Demonstration plan, phase D6 (registry side). A prospect trialling Werkpilot had no model access until someone created a LiteLLM key by hand. The registry issues one with the licence, scoped and budgeted per tenant. **On issue** (`product=werkpilot`): `POST /key/generate` on LiteLLM with `key_alias = werkpilot-<slug>-<licence id>`, `metadata` carrying tenant id/slug/licence/product, the model list and the plan's budget (trial and default, USD per 30 days). LiteLLM's hashed token id goes on the `licences` row (migration `0007`, exposed as `llm_key_id` on `GET /v1/licences`); the `sk-…` secret is returned in that one response as `llm_api_key`, together with `llm_base_url`, and is never stored or returned again. **On renewal** (licence heartbeat): the existing key moves to the new licence row rather than a second key being generated — the secret cannot be re-read, and the CLI still holds the one it stored. **On revoke:** the key is deleted; a 404 from LiteLLM is treated as already gone. If the delete fails, the licence is still revoked and the key id stays on the row so it can be retried by hand. **Failure is never fatal:** with `LITELLM_MASTER_KEY` unset, LiteLLM unreachable or refusing, the licence is issued anyway and the grant carries `warnings: ["llm_key_unavailable"]` with no LLM fields. Also, a key that cannot be persisted is deleted again rather than handed out un-revocable. Config: `LITELLM_BASE_URL` (default `https://llm.breakpilot.com`), `LITELLM_MASTER_KEY`, `LITELLM_MODELS` (default `Qwen3-Coder-30B-A3B-Instruct,gpt-oss-120b`), `LITELLM_BUDGET_TRIAL` (5), `LITELLM_BUDGET_DEFAULT` (50). Startup logs whether provisioning is on. Note the migration is numbered `0007`: `0006` is taken by the members PR (#28), which is independent of this one — merge order does not matter. Verified locally: `go vet ./...`, `gofmt` clean, `go test ./...` green, CI coverage command at **74.4%** (gate 70%). `internal/llmkeys` is tested against an httptest fake; no live LiteLLM call was made. Pairs with a `bp-licence` bump (the Rust `Grant` gains the optional fields) and with `LITELLM_MASTER_KEY` on `tenant-registry-stage`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang added 1 commit 2026-09-12 19:43:35 +00:00
feat(licences): per-tenant LiteLLM key with a Werkpilot grant
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 22m6s
ci / image (pull_request) Skipped
972cae6195
A Werkpilot trial needed a hand-made LiteLLM key before the CLI could talk
to a model. The registry now provisions one when it issues the licence:

- POST /v1/licences for product=werkpilot calls LiteLLM /key/generate with
  the tenant in the alias and metadata, the plan's monthly budget and the
  configured model list, stores LiteLLM's token id on the licence row
  (migration 0007) and returns llm_api_key + llm_base_url in the grant.
  The secret is returned exactly once and never persisted.
- A heartbeat renewal moves the key to the new licence row instead of
  generating a second one, because the secret cannot be re-read and the
  CLI still holds it.
- Revoking a licence deletes the key; a 404 from LiteLLM is fine.
- LiteLLM being off, unreachable or refusing never fails the licence: the
  grant comes back without the LLM fields and with
  warnings: ["llm_key_unavailable"].

Configured with LITELLM_BASE_URL, LITELLM_MASTER_KEY (empty = feature off),
LITELLM_MODELS, LITELLM_BUDGET_TRIAL and LITELLM_BUDGET_DEFAULT.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
CODEOWNERS rules requested review from Benjamin_Boenisch 2026-09-12 19:43:35 +00:00
sharang merged commit e0ae19f2a7 into main 2026-09-14 08:27:26 +00:00
sharang deleted branch feat/licence-llm-key 2026-09-14 08:27:26 +00:00
Sign in to join this conversation.