Three Open-Source Levers to Cut LLM Costs: Task Routing, Instruction Optimization, and Consumption Control
Most of the savings happen before any model swap: enabling prefix caching, compressing context, and static routing are cheap, revertable moves that can cut a large share of context tokens. The hard part is attribution and regression testing, and the open-source maintenance landscape is shifting under long-tail projects, so tool selection needs an ownership check.
RouteLLM (UC Berkeley, ICLR 2025) is the reference for learned routing: a lightweight classifier decides whether a request needs a strong model, and on benchmarks it cuts up to 85% of cost while holding 95% of GPT-4 quality. But it only does strong/weak binary routing, its repo stopped updating in August 2024, and production savings depend on the share of simple requests. LiteLLM remains the active gateway for rule-based routing and budget caps; the two are commonly stacked as gateway plus per-request router.
Prompt optimization has become a compile step. DSPy's optimizer ladder runs from LabeledFewShot and BootstrapFewShot up to MIPROv2 and GEPA, where instructions and few-shot examples are searched as parameters; a single compile can cost a few hundred dollars in LM calls, so it only pays off for fixed, reusable tasks. Microsoft's PromptWizard is lighter, iterating on one prompt with an LLM in about 20-30 minutes per dataset.
On the consumption side, vendor prefix caching is the zero-rewrite baseline, semantic caching can hit 25%-60% on repeated traffic, and LLMLingua claims up to 20x compression, though compression breaks prefix caches. Context hygiene, like summarizing history and pruning RAG results, cuts 40%-75% of context tokens with no new dependency. The bill is structurally call count times model price times token volume, so observability is the first move: without per-call attribution, no optimization is measurable. For a small text-generation site, the practical sequence is: observe two weeks, turn on caching, do context hygiene, add static routing, and only then consider learned routing and prompt compilers, with a 100-item golden set as a quality gate.
RouteLLM's transfer result is the least-appreciated number: the router learns difficulty features, not the specific strong/weak pair, so model swaps cost only 2-3 PGR points.
Because the three levers multiply in the bill equation, optimizing one reduces the others' returns; teams should start on whichever term is largest rather than chasing the newest tool.
Prompt compilers only pay off when a task has a computable metric and long-term reuse; a one-off task cannot amortize a few hundred dollars of compile cost, which is why static rules and context hygiene deliver most savings for most teams.
Compression and prefix caching are in direct tension: rewriting prompts invalidates the cached prefix, so stable system prompts should stay byte-identical while volatile content goes through compression or summarization.
Project ownership now shapes tool choice: RouteLLM is a frozen research prototype, GPTCache stopped releasing, Helicone moved to maintenance after acquisition, so production users need a maintenance plan before depending on any long-tail OSS component.