From 82e532b40ba88c6bfdb73ac966b397f634039570 Mon Sep 17 00:00:00 2001 From: Botbot Date: Wed, 26 Aug 2026 22:03:16 -0600 Subject: [PATCH] Add Component 7: LiteLLM proxy for per-user keys, usage metering, model routing --- inference-coop/README.md | 97 ++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 13 deletions(-) diff --git a/inference-coop/README.md b/inference-coop/README.md index b9e5c03..0c46643 100644 --- a/inference-coop/README.md +++ b/inference-coop/README.md @@ -370,28 +370,99 @@ This gives members a space to decide on model selection, pricing, privacy polici --- -## Component 7: API access (missing from original design goals) +## Component 7: API access gateway (LiteLLM) -In addition to the chat frontend, members need direct API access to use models in their own code, agents, and integrations. +The cooperative needs a layer between members and the inference backends (Tinfoil, Ollama, OpenRouter) that handles: -### How to provide it +- **Per-user API keys** — each member gets their own key +- **Usage metering** — track token usage per user for billing +- **Rate limiting** — enforce per-tier limits (free, member, supporter) +- **Model routing** — route requests to the right backend (Tinfoil for TEE, Ollama for free, OpenRouter for frontier) +- **Cost tracking** — track spend per user, per model, per key + +### Solution: LiteLLM Proxy + +[LiteLLM](https://www.litellm.ai/) is an open-source AI gateway that does all of the above. It sits between Open WebUI / LobeChat and the inference backends: -Both Ollama and vLLM expose an **OpenAI-compatible API** (`/v1/chat/completions`). Members point their code at: ``` -https://inference.coop/v1/chat/completions +Members → Open WebUI / LobeChat → LiteLLM Proxy → Tinfoil (TEE) + → Ollama (self-hosted, free) + → OpenRouter (frontier fallback) ``` -with their API key (issued via Cloudron SSO or a simple key management layer). +**LiteLLM provides:** +- **Virtual API keys** — per-user, per-team keys with individual budgets and rate limits +- **Spend tracking** — per key, per user, per team, per model, across 140+ providers +- **Model access control** — restrict which users can access which models +- **Budget enforcement** — when a user hits their cap, requests stop automatically +- **Admin dashboard** — usage analytics, spend tracking, key management +- **OpenAI-compatible** — one endpoint for all backends; Open WebUI and LobeChat see a single API +- **Fallback routing** — if Tinfoil is down, automatically fall back to OpenRouter +- **Self-hostable** — runs as a Docker container, can be deployed via Cloudron or alongside it -### Key management +Sources: [LiteLLM](https://www.litellm.ai/), [LiteLLM + Ollama gateway guide](https://everylocalai.com/stack/litellm-ollama-gateway), [LiteLLM proxy setup](https://markaicode.com/tutorial/litellm-configuration-guide/) -Open WebUI has built-in API key generation for users. Alternatively, a lightweight API gateway (e.g., LiteLLM proxy) can handle: -- Per-user API keys -- Rate limiting per tier -- Usage logging -- Model routing (lightweight → self-hosted, frontier → OpenRouter) +### How it fits the cooperative model -- [LiteLLM](https://github.com/BerriAI/litellm) — proxy that unifies 100+ LLM providers behind OpenAI API +1. **Tinfoil API key** is stored only in LiteLLM's config — members never see it. The cooperative holds one Tinfoil account, LiteLLM distributes access via virtual keys. + +2. **Per-member virtual keys** — each member gets an `sk-coop-xxxxx` key. LiteLLM tracks their usage and enforces their tier's limits. This is how the cooperative meters who uses what. + +3. **Open WebUI integration** — Open WebUI supports per-user API keys and has built-in token tracking (via the [token tracking pipes feature](https://github.com/dartmouth/openwebui-token-tracking)). It can display per-conversation token counts and costs to users. + +4. **Billing connection** — LiteLLM's spend tracking API can be queried by the billing middleware (Component 5) to reconcile usage with Open Collective payments. If a member stops paying, the middleware disables their LiteLLM virtual key. + +5. **Model governance** — LiteLLM's model access control lets the cooperative decide which models each tier can access. Members vote on model selection in Loomio; admins update LiteLLM config to reflect decisions. + +### Architecture with LiteLLM + +``` +┌──────────────────────────────────────────────────────────┐ +│ Member's browser │ +│ Chat: Open WebUI or LobeChat → api.inference.coop │ +│ API: Direct → api.inference.coop/v1/chat/completions │ +└────────────────────────┬─────────────────────────────────┘ + │ sk-coop-xxxxx (per-member key) +┌────────────────────────▼─────────────────────────────────┐ +│ LiteLLM Proxy (Docker) │ +│ • Virtual keys per member │ +│ • Rate limits per tier │ +│ • Usage tracking per user/model │ +│ • Spend tracking for billing │ +│ • Model routing + fallback │ +└──────┬──────────────────┬──────────────────┬────────────┘ + │ │ │ +┌──────▼─────┐ ┌──────▼──────┐ ┌──────▼──────┐ +│ Tinfoil │ │ Ollama │ │ OpenRouter │ +│ (TEE) │ │ (self-host) │ │ (fallback) │ +│ Default │ │ Free tier │ │ Frontier │ +└────────────┘ └─────────────┘ └─────────────┘ +``` + +### Is LiteLLM in Cloudron? + +LiteLLM is not currently in the Cloudron app store. It would need to be deployed as a custom Docker container — either: +- Via Cloudron's [custom app/LAMP stack](https://docs.cloudron.io/apps/lamp/) option +- As a standalone Docker container on the same VPS (with a reverse proxy for SSL) +- Via Coolify alongside Cloudron (if using Coolify instead) + +This is the one piece of the MVP that doesn't fit neatly into Cloudron's one-click model. It's also the only piece of custom infrastructure (beyond the billing middleware) that the cooperative needs to build and maintain. + +### Alternatives to LiteLLM + +| Tool | Per-user keys | Usage tracking | Rate limiting | Self-hosted? | +|------|-------------|---------------|--------------|-------------| +| **LiteLLM** | ✅ | ✅ Per-user, per-model | ✅ | ✅ Docker | +| **Open WebUI built-in** | ✅ | ✅ Via token tracking pipes | Partial (in development, [#23323](https://github.com/open-webui/open-webui/issues/23323)) | ✅ (already in Cloudron) | +| **Morph LLM Proxy** | ✅ | ✅ | ✅ | ✅ | +| **Custom middleware** | Build it | Build it | Build it | ✅ | + +Open WebUI's built-in token tracking is improving and may eventually handle per-user limits natively. For the MVP, LiteLLM is the more complete solution. As Open WebUI's features mature, the cooperative could simplify by dropping LiteLLM and using Open WebUI's native tracking. + +- [LiteLLM](https://www.litellm.ai/) — open-source AI gateway +- [LiteLLM docs](https://docs.litellm.ai/) +- [Open WebUI token tracking](https://github.com/dartmouth/openwebui-token-tracking) +- [Open WebUI per-user limits discussion](https://github.com/open-webui/open-webui/issues/23323) ## Component 8: Privacy and confidential inference