Per-Seat Token Caps for a Small Team Using Docker Compose
At 3:17 AM, the on-call pager vibrates with a Slack alert: "Budget exhausted – monthly tokens depleted." The root cause wasn't model misconfiguration or sloppy prompts. It was a single shared API key with no per-seat ceilings, allowing one rogue process to drain the account while customer source code fragments sat in the provider's 30-day retention window. By the time the dashboard appeared at standup, the damage was irreversible.
This incident drove us to Docker Compose as the control plane for a 12-person remote agency. With only three processes on one host—proxy, Redis, and worker—we gain tight per-seat enforcement without Kubernetes complexity.
What Actually Runs
Engineers call only the proxy. It accepts OpenAI-compatible traffic, reads each seat's remaining daily and monthly token budget from Redis, applies a 1.15x estimate buffer, debits usage, and returns 429 when either cap is exhausted. Upstream requests always target https://b-lost.com/v1. The proxy never writes request or response bodies to disk or stdout.
Redis holds lightweight integer counters keyed by seat and UTC date. The alert-worker scans these counters every minute, multiplies the day's tokens by the configured rate, and fires one webhook per seat when daily spend crosses $50. There is no prompt store and no trace pipeline.
SDKs point at http://gateway.internal:8080/v1 and send a short-lived seat token. The proxy maps that token to Redis keys, checks both caps, and either forwards to the upstream or rejects the call with a remaining-budget header. When the upstream returns a usage object we reconcile; otherwise we over-debit to avoid under-protection. The worker is deliberately simple: every minute it computes tokens × rate and fires the webhook once. Fifty dollars of notice is sufficient warning for twelve people before the monthly key is threatened.
The File We Deploy
services: redis: image: redis:7.4-alpine command: ["redis-server", "--save", "", "--appendonly", "no"] volumes: - redis-data:/data healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 5s timeout: 3s retries: 5 quota-proxy: image: ghcr.io/agency/quota-proxy:1.8.2 depends_on: redis: condition: service_healthy environment: UPSTREAM_BASE_URL: "https://b-lost.com/v1" REDIS_URL: "redis://redis:6379/0" DAILY_TOKEN_CAP: "200000" MONTHLY_TOKEN_CAP: "4000000" LOG_BODIES: "false" ESTIMATE_FACTOR: "1.15" ports: - "8080:8080" restart: unless-stopped alert-worker: image: ghcr.io/agency/budget-alert:1.8.2 depends_on: redis: condition: service_healthy environment: REDIS_URL: "redis://redis:6379/0" DAILY_USD_THRESHOLD: "50" WEBHOOK_URL: "https://hooks.internal.example/budget" SEAT_COST_PER_1K: "0.002" restart: unless-stopped volumes: redis-data: Hardening for Production
An adversarial audit revealed several risks that could break this setup at scale. We addressed them without altering the core images:
Concurrency races and shared state: The worker and proxy access Redis concurrently. We enforced atomic decrements via Redis Lua scripts or CAS operations in the proxy layer. Connection pooling (e.g., Go's sync.Pool equivalent) bounds concurrent upstream connections to prevent TCP exhaustion.
Buffer and memory exhaustion: Redis can grow without limits. We added memory caps and eviction policies to the Redis command line and container limits. Seat tokens are validated for length before key construction.
Unhandled error codes: The proxy passes 5xx errors through without retry to avoid reopening uncapped windows. For 502/503 we recommend a configurable retry (max 3 attempts with exponential backoff) before returning 429. We incorporated a simple circuit breaker in the hardened proxy image.
Resource limits: Container DoS vectors were closed by adding resource constraints:
deploy: resources: limits: cpus: '0.5' memory: 128M reservations: cpus: '0.1' memory: 64M Redis received a 256M limit with LRU eviction. The worker was capped at 64M.
Transport limits: The proxy was configured for HTTP/2 with keep-alive pooling. We verified the upstream endpoint supports it and set connection limits to avoid memory bloat.
Edge cases: Redis failure triggers 503 from the proxy with retry-after. Daily keys reset on host reboot (acceptable risk for this team size). Seat tokens expire after one hour. If the upstream returns no usage object we over-debit rather than let the seat run long.
These changes turned the setup into a resilient system while preserving the original YAML structure.
Trade-offs That Show Up After the First Incident
Single-host Compose is cheap and fast to operate, but its simplicity is its limit. We disabled append-only persistence to cut fsync latency on every decrement. A reboot resets daily keys, so a seat can spend twice before the next alert. For this team that risk remains cheaper than replicated storage. If you need durability, enable AOF and measure the added latency; do not treat memory-only Redis as durable.
The proxy adds one hop. Inside the same network it is a few milliseconds when healthy, but it becomes the outage if the process wedges. Startup ordering relies solely on the Redis health check. No circuit breaker exists in the base config; upstream 5xx errors propagate immediately. This choice prevents retry storms that could reopen uncapped windows.
The ultimate failure mode is credential bypass: if the relay key still lives in someone's .env, Compose does nothing. We keep that credential only in the proxy environment and treat direct use as an incident. Seat tokens are scoped to the proxy and expire.
Zero retention is a setting, not a Compose property. LOG_BODIES=false and integer-only Redis are necessary but not sufficient. A later logging driver or collector that samples bodies reintroduces a retention path. We review the compose diff for new volumes and log options before merge.
This stack will not do per-request policy, multi-region failover, or finance-system chargeback. It will stop one laptop from emptying the month and keep client source off a provider retention window. Caps change by editing the environment block and running docker compose up -d. There is no control plane beyond that file, which is exactly the point for a team this size.
The single hardest operational dilemma is choosing between operational simplicity for a small team and the need for enterprise-grade observability and resilience. For teams of our size this Compose-based approach delivers immediate protection at minimal cost—but scaling to 50+ seats will eventually demand a shift to managed gateways.
What’s the most challenging trade-off you’ve faced when balancing AI cost controls with team autonomy in your infrastructure? Share your battle scars in the comments—I’d love to hear how others have solved it.
