Charlotte, NC
BlogApril 8, 2026

Why I Self-Host My LLM Proxy, and What LiteLLM's Spend Logs Showed Me

Blake McCarn
Why I Self-Host My LLM Proxy, and What LiteLLM's Spend Logs Showed Me
In the first quarter of 2026, my self-hosted LiteLLM proxy logged about 853,000 requests, 763 million tokens and $1,702 of model spend. I can quote those numbers only because every call that went through it landed in Postgres, tagged with the key that made it. Before the proxy, each AI service in my homelab called its provider directly with its own key. To answer "what is this costing me?" I had to open each provider's console and add the numbers up by hand. Even then I couldn't tell which service had spent what. LiteLLM puts one OpenAI-compatible endpoint in front of every provider. A client sends it a model name and a virtual key. LiteLLM forwards the request to the real provider, retries if needed, and writes a spend row. I run it myself so the provider keys and the request log stay on my own hardware, in a database I can query. The proxy runs in the tools namespace of my homelab cluster, and Flux applies it from Git like everything else there. I wrote about that cluster in A Boring Kubernetes Homelab. LiteLLM is three workloads in the cluster. There's a Deployment for the proxy, a Deployment for Redis, and a Job for schema migrations. Postgres runs outside the cluster.
Tap to enlarge, then pinch to zoom
This is the proxy Deployment. I've left out the image digest and a block of environment variables that blank out HTTP proxy settings. I pin the image by tag and digest, and Renovate opens a pull request when a new release ships. The memory request equals the limit, so the scheduler reserves the full 4 GiB on whatever node the pod lands on. With maxSurge: 1 and maxUnavailable: 0, a rollout starts the new pod and waits for it to pass readiness before it stops the old one. A single replica still rolls over without a gap, as long as some node has room for a second 4 GiB pod during the rollout. The readiness probe hits /health/readiness, which reports whether the proxy is connected to its database. The startup probe gives a cold start up to five minutes before the liveness probe takes over. DISABLE_SCHEMA_UPDATE=true keeps the serving pod from touching the database schema. Schema changes belong to the migration Job. I started with two replicas. In July, after 31 days of uptime, both 4 GiB pods were OOM-killed on the same day. Two pods that fail together aren't redundancy, and each replica was also running its own copy of LiteLLM's background health checks, which I cover below. I went to one replica with one Uvicorn worker. That leaves one process to watch for memory growth. At the same time I set --max_requests_before_restart 10000, so Uvicorn would replace the worker every 10,000 requests and cap any slow leak. With a single worker, every replacement took the proxy down. On September 9 the worker hit the limit and exited cleanly with code 0. The endpoint was gone for about 24 seconds while Kubernetes restarted the container, and a knowledge graph evaluation run that was using the proxy failed with connection errors. I removed the flag. Before removing it, I tested two workers with restart jitter on loopback. All four slow streaming responses in the test survived the recycling, but 2 of 120 short requests timed out when both workers drained together. Every client depends on this proxy, so that wasn't good enough, and the deployment stays at one worker with no request limit. Since then the pod restarts only for upgrades. The current one started with the 1.106.6 upgrade in November, hasn't restarted, and was using about 750 MiB at last check. One gap remains. Kubernetes gives a pod the default 30 seconds to shut down, and LiteLLM's request_timeout is 600 seconds. A rollout can cut off a long request that's in flight, so I don't merge proxy changes while a long batch job is running.
Tap to enlarge, then pinch to zoom
LiteLLM keeps its schema in Prisma, and by default the proxy applies schema changes when it starts. I want each schema change to run once, in a pod that does nothing else, and the new proxy version to start only after that succeeds. The migration Job uses the same image and the same config as the proxy. Here it is without the resource and security settings: --skip_server_startup runs the startup work, including the migration, and exits without serving traffic. Earlier versions also needed --enforce_prisma_migration_check and --use_v2_migration_resolver. Version 1.106 ignores both, so I removed them with that upgrade. Kubernetes won't let you change a Job's pod template, so the kustomize.toolkit.fluxcd.io/force annotation tells Flux to delete the Job and recreate it when the image changes. Renovate bumps the image in the Job and the Deployment in the same pull request. I added the init container when I removed the MCP servers from the config and turned off background health checks. External Secrets refreshes Secrets on its own schedule, and I didn't want the migration to run against the old config. It waits until the config Secret has no mcp_servers block and has background health checks turned off. The ordering comes from two Flux Kustomizations: With wait: true, Flux marks litellm-migration ready only after the Job completes. The litellm Kustomization depends on it, so Flux doesn't apply the new Deployment until the migration has succeeded. If the Job runs out of retries or passes its 15-minute deadline, the old proxy pod keeps serving on the old version and Flux reports the failure.
Tap to enlarge, then pinch to zoom
Nothing sensitive for the proxy lives in Git. External Secrets Operator reads two items from a 1Password vault and writes two Kubernetes Secrets. litellm-env holds the database URL, the master key, the salt key and the admin UI login. litellm-files holds the whole config.yaml, because that file includes the database URL. creationPolicy: Orphan means the ExternalSecret doesn't own the Secret, so deleting or renaming the ExternalSecret leaves the Secret the proxy runs on in place. deletionPolicy: Retain keeps the Secret if the 1Password item disappears. The provider API keys aren't in either Secret. The model routes live in LiteLLM's database, and LiteLLM encrypts the provider credentials stored there with LITELLM_SALT_KEY. That makes the salt key the one value I can't lose or rotate casually, because without it LiteLLM can't decrypt the stored credentials. The Deployment mounts the config file with subPath, and LiteLLM reads the envFrom variables once at startup. Neither picks up a changed Secret in a running pod. When I change the config in 1Password, I also bump the secret-revision annotation in the Deployment's pod template. Flux applies the new annotation and Kubernetes rolls the pod. The database holds the virtual keys, the model routes and the spend logs. It's the only state the proxy has that I'd miss, and I keep very few stateful workloads inside the cluster. It runs on Postgres 18 on two Proxmox VMs managed by Patroni, with an etcd witness on a third VM. HAProxy and a keepalived virtual IP give clients one writer address. DATABASE_URL points at that address, so a Patroni failover doesn't need a LiteLLM change. pgBackRest archives WAL and backups to the NAS and to S3. These general_settings control what the proxy writes there: store_prompts_in_spend_logs: false here and turn_off_message_logging: true in litellm_settings keep prompt and response text out of the database. A spend row has the key, model, token counts, cost and timing, and nothing a model said. proxy_batch_write_at: 30 batches spend writes every 30 seconds instead of writing on every request. Per-request rows expire after 90 days. LiteLLM also keeps daily per-key activity totals, and the quarterly numbers in this post come from those. Inside the cluster, clients call a plain ClusterIP Service on port 80, which forwards to port 4000 on the pod. The tools namespace has a baseline NetworkPolicy that admits traffic from a few namespaces and from my LAN and VPN ranges. A client in any other namespace needs its own policy. This is the one for my self-hosted Firecrawl scraper: The policy lets in the scraper's API pods and nothing else from that namespace. My document pipeline's worker has an equivalent policy. That pipeline has been paused since September, but its key and policy stay in place. The admin UI and clients outside the cluster go through an HTTPRoute on my Envoy Gateway. On the LAN, split DNS points the hostname straight at the gateway. With ui_access_mode: admin_only, only admin users can sign in to the UI. A service holding a virtual key can call models and nothing more. My services don't request claude-sonnet-4-6. They request sonnet, opus or haiku, and LiteLLM maps each alias to a concrete model. With store_model_in_db: true, I keep the routes in the database and edit them in the admin UI. If you'd rather keep routes in the config file, the same mapping looks like this: When Anthropic ships a new Opus, I repoint opus in the UI. Every client that asks for opus gets the new model on its next request, with no code change and no redeploy. My document pipeline goes a step further and asks for aliases named after the job. It has a default document alias, a stronger one for hard documents and one for deep review. They point at a Gemini Flash model, Claude Sonnet and a GPT model, and I can swap any of them without touching the pipeline. The proxy-wide behavior is in litellm_settings: drop_params: true matters once more than one provider sits behind the proxy. Providers accept different request parameters. Without this setting, LiteLLM returns an error when a parameter has no equivalent on the target provider. With it, LiteLLM drops that parameter and sends the rest. Clients can send one request shape no matter which provider serves it. The cache sends identical completion and embedding requests within an hour to Redis instead of the provider, and a cache hit costs nothing. In my setup that mostly helps retries, because a retried job sends exactly the same prompt. Since 1.106, LiteLLM skips the cache for any request with more than four messages, so long conversations always go to the provider. Redis runs as its own single-replica Deployment with maxmemory 512mb and allkeys-lru eviction, on an emptyDir volume. Losing the cache on a restart costs a few repeated calls, so I don't give it persistent storage. Fallbacks go in router_settings. This is an excerpt: enable_pre_call_checks makes the router check a request against each model's context window before sending it. If a prompt is too big for haiku, the router tries sonnet, then opus, then Gemini, instead of returning the provider's error to the client. Content-policy fallbacks send a refused request to another model, usually from a different provider.
Tap to enlarge, then pinch to zoom
LiteLLM can run background health checks that call every model route on a timer. Each check is a real, billed request, and each replica runs its own loop. I turned them on in late May. In the 30 days before July 23 they made 474,005 calls and cost $41.57, and they made the request count useless as a measure of real work. I turned them off on July 23, and monthly requests fell from about 392,000 in July to about 43,000 in August. The probes already tell Kubernetes whether the proxy process and its database connection are healthy. A broken model route shows up as errors in the client that uses it, and as failed requests on that client's key in the spend log. Between the probes and the spend log, I don't need the proxy paying for calls nobody asked for. Each service gets its own virtual key from the admin API: The service sends that key instead of a provider key. It never sees the real Anthropic or OpenAI credential, and I can revoke it without rotating anything upstream. models limits the key to the listed aliases. max_budget and budget_duration make the proxy reject the key's requests once it has spent $10 in a 30-day window, which stops a batch job that loops on the same documents all night. Since 1.106 that rejection is an HTTP 422, not a 429, which matters if a client retries on 429. rpm_limit caps requests per minute. I added keys after the proxy was already in use, which was a mistake. Several clients were still on the master key, and I had to work out which ones before I could move each to its own key. Most of the requests in that first quarter came from keys without an alias, and I can't trace those to a service anymore. Next time I'd create the keys before connecting a single client. The key table eventually had 136 rows, 119 of them without an alias. I cut it down to 8 keys. Seven of them can call only a listed set of models. Two have a $10 budget per 30 days, the scraper and a label-printing service, and three have per-minute request limits. The knowledge graph's key, the heaviest user, has no budget, and I watch the dashboard for it instead. New services have since got their own keys from day one, such as the index behind my Vault RAG search. In the same cleanup I removed the 14 MCP servers I'd attached to the proxy. Nothing had called them in the previous 30 days, and I'd rather upgrade a model proxy without also carrying tool traffic through it. The per-key breakdown showed which service spent what, which I couldn't get from the provider consoles. In February and March, my main automation agent was the biggest line item. Its key spent $777 in February and $308 in March, more than all the other keys combined in each month. Before that, the most expensive thing on the proxy had been a GraphRAG experiment over my document archive. Its key had spent about $1,540, most of it in December, the month I started the experiment. The memory layer made about 11,500 calls in February and March, and they cost $67 in total. The heaviest key changes from month to month. In September the proxy handled about 51,000 requests, 277 million tokens and $527 of spend, and the key for my document knowledge graph made 92% of those requests. The log has one blind spot. In early March I pointed the memory layer's extraction model straight at OpenAI to remove a network hop while I chased capture timeouts. Late in March I moved the automation agents' Anthropic calls to direct as well. My OCR pipeline has always called its providers directly. Direct calls never reach LiteLLM, so they never appear in its spend log, and for the full Anthropic bill I still check Anthropic's console.
Share this post: