How leanprompt works

leanprompt is a set of skills for coding assistants. It finds the places where an app's LLM calls lose tokens or time (a prompt the cache cannot reuse, dead weight in the prompt, output thrown away), proposes a fix for each that keeps meaning, and proves the fix with the provider's own usage numbers. This page covers the problem, the audit, the rules and the measure loop.

The problem

Every call to an LLM resends the system prompt, the tool list and the history. Providers and engines cache the part that did not change, but only if it really did not change, byte for byte, from the first token. Most apps break that without knowing: a timestamp on the first line, tools built from an unsorted dict, a history store that re-serialises old messages, a client that never sends Anthropic's cache marks. Then they pay again to generate what one call could have produced: a retry after a truncation, one request per sample, a parse failure re-asked from scratch.

leanprompt gives your coding assistant the rules to find those places in your code, the fix for each, and a way to prove the fix helped with the provider's own usage fields.

How the audit works

Ask your assistant to audit this app's LLM calls. The llm-call-audit skill:

  1. Inventories the call sites by SDK and HTTP shape (Anthropic, OpenAI, Gemini, LangChain, LlamaIndex, LiteLLM, the Vercel AI SDK, raw fetch or requests), and follows each one back to where its system prompt, tools and history are built.
  2. Checks each call against the rules and records every finding with its rule id, file and line. A finding outside the repository (an engine's launch flags, say) is recorded as <external> with where it lives, and one that depends on a fact the code cannot show carries the question that would settle it; both count in the static score and are listed as open questions. A finding whose fix is a setting of the server or engine the app calls is listed separately, as a question for the server's administrator, and is not in the app's score (score.md). A verified problem the rules have no place for (a spend meter that ignores cache tokens, a cut-off answer accepted as complete) is recorded as a note: shown and handed off, never scored. A rule that depends on a provider feature (a cache mark, a batch API) applies only to the providers it lists. A rule whose fix is the same everywhere (output limits, retries, timeouts, structured output, response caching) applies to every target, including a hosted API that has no provider table here.
  3. Proposes one fix per finding, with the rule's "Don't break meaning" check, and asks before editing anything. A fix that cuts dead weight from the prompt (prompt-diet) ships only after an answer check, and a cut to the cached head of the prompt is weighed against the cache it breaks.
  4. Hands each fix to measure-loop to prove it with a before and after number.

The audit writes .leanprompt/findings.json, and leanprompt report --md writes leanprompt-report.md from it, so the report and the page say the same thing. The report follows report-template.md: a plain-English headline, what the findings cost, each finding's problem, change and reason, a hand-off block for a coding agent, the score, the call inventory and the next steps. leanprompt report --html turns the same content into a single local page (.leanprompt/report.html) with copy buttons, for the person who reads the audit. If you tell the audit you are on a particular provider or self-hosted engine, or that speed matters more than cost, put it in leanprompt.yaml and the findings are ranked for that target.

The rules at a glance

Prefill rules first, then generation, then dead weight in the prompt, then self-hosted. Every rule file ends with a "Verify" section: the usage field that must change and the kvsig id that must stay equal.

RuleIn one lineSaves
LP-PC-01Put cache marks where the provider needs themprefill
LP-PC-02Stable content first, without hurting meaningprefill
LP-PC-03No ids, timestamps, versions or paths at the top of the promptprefill
LP-PC-04Fixed tool order and JSON key orderprefill
LP-PC-05Tool lists grow only at the endprefill
LP-PC-06Append-only historyprefill
LP-PC-07Shared context before per-user contextprefill
LP-PC-08Cache-aware compactionprefill
LP-PC-09Consistent reasoning handling; thinking fixed per sessionboth
LP-PC-10Stable image and attachment placementprefill
LP-PC-11Keep pauses inside the cache lifetimeprefill
LP-PC-12Do not split one prefix across models or keys that do not share a cacheprefill
LP-GE-01n samples in one requestprefill
LP-GE-02Batch independent questions about one contextboth
LP-GE-03Output and thinking budgets that fit the taskgeneration
LP-GE-04Stream long outputs; timeouts that match generation timeboth
LP-GE-05Retry only retryable errors; never retry a truncation unchangedboth
LP-GE-06Cache exact responses for deterministic requestsboth
LP-GE-07Batch API for work that can waitboth
LP-GE-08Route trivially small calls to a small modelboth
LP-GE-09Predicted outputs for mostly unchanged textgeneration
LP-GE-10Structured output instead of parse-and-re-askgeneration
LP-GE-11Meter every billed token, cached and unfinished ones toonone: a correctness rule, recorded as a note
LP-PD-01Offer each route only the tools it can useprefill
LP-PD-02Say each instruction onceprefill
LP-PD-03Do not resend the same boilerplate on every turnprefill
LP-PD-04Trim tool results and retrieved context at the tool boundaryprefill
LP-PD-05Drop stale history on purpose, not by strippingprefill
LP-PD-06Compact serialisation for data in the promptprefill
LP-LW-01Turn on prefix caching and set the match unitprefill
LP-LW-02Keep a conversation on the replica that holds its cacheprefill
LP-LW-03One key per app or tenant; cache scope is a privacy boundaryprefill

Prove it: measure, change, measure

Start with what the provider already tells you. OpenAI-compatible APIs, vLLM, DeepInfra and OpenRouter report usage.prompt_tokens_details.cached_tokens; Anthropic reports usage.cache_read_input_tokens and usage.cache_creation_input_tokens. Compare them across two requests of the same conversation, before and after a fix.

To keep a fix from coming undone, add a stable-head test. It builds the request your app would send for two fixed inputs and fails when the system prompt or tool list differs, using kvsig ids. It runs offline, with no provider call:

import { assertStableHead } from "./stable-head";
it("the head does not change from one day to the next", () =>
  assertStableHead(
    (now) => app.buildRequest("Where is my order?", { session: 0, turn: 0, now }),
    [new Date("2026-11-03T08:15:42Z"), new Date("2026-11-04T17:02:09Z")],
    { sys: true, tools: true },
  ));

When sys differs, something in the system prompt changes per input (a date, an id, a path); when tools differs, a tool was reordered or edited. The helper templates (TypeScript and Python) to copy into your tests and the table of which id to assert for each rule are in measure-loop.

For a measured score, log every call with the usage logger: leanprompt/log for TypeScript and leanprompt-cli for Python, both published with the first release. It writes one JSON line per call to .leanprompt/usage.jsonl with keyed ids, token counts and flags, and never writes prompt or completion text. How the log becomes a score: score.md.

Evidence and example apps

The figures in the README, these docs and the skills come from the table in evidence.md (rows E1 to E12, each with its source, date and limits) or from measured result files under eval/results/, which stays empty until the first real replay is committed. Results from the cache simulator run in CI only and are never quoted. npm run check:numbers fails the build when a number in the README, a page under docs/ or a skill has no source.

The example apps in examples/ each come in a before/ and an after/ form with an answer check, and npm run eval replays them: against the local simulator at no cost, or against a provider you name, which sends real, billed requests under a spend cap.

What leanprompt is not

  • Not a proxy and not a prompt rewriter on the wire. It changes your code, with your approval, one fix at a time.
  • Not a prompt-compression library. Compressing a prompt changes its bytes on every call, which is what breaks a prefix cache.
  • Not a hosted service. There is no telemetry, no account and no upload. The CLI makes no network calls except profile detect (to the URL you give it) and the replay harness (to providers you name).
  • Not a promise of a percentage for your app. Measure it: the score says what it was computed from.

Edit this page on GitHub