The default model probably never uses its prompt cache

A dusty storage cabinet full of boxes stands unused while a small delivery cart drives past it.

Cache never hits Reported

A chat app whose 2.0 code already takes caching seriously, with Anthropic cache marks and a stable system prompt.

≈ 8,000,000 tokens per 1,000 calls, for default-model chats, if the gateway does not cache them estimate

Estimates, not measurements. Every figure on this page is an estimate from reading the code, not a measurement of the app in use.

Repository
keinsaasforever/better-chatbot
Commit audited
6f41835
leanprompt version
0.1.0-dev.14
Audit date
Status
The maintainers have been told about this audit; no response yet.
Maintainers
the issue
How it ran
Unattended audit: nobody was asked during the run, so priority, volume and prices were left open; the questions for the owner are listed at the end.

A chat app whose 2.0 code already takes caching seriously: Anthropic cache marks, a stable system prompt with the clock moved out, slimmed history, cost-weighted metering. The open question is the default model, an Alibaba model on Vercel AI Gateway that caches only at explicit marks the app does not send it. Figures are estimates from the code.

leanprompt read the code at commit 6f41835 and found 6 places where its LLM calls could use fewer tokens, 1 more that costs no tokens today, 1 the project already knows about, plus 2 other problems, not scored. Static score: 69 of 100 (an estimate from the findings, leaving out what the project already knows about). The code may have changed since.

Findings

  1. LP-PC-01 Put cache marks where the provider needs them

    apps/web/src/app/api/chat/route.ts:1370 · severity high · saves prefill · needs a fact to confirm

    What happens. The only cache marks the chat sends are Anthropic ones (providerOptions.anthropic, lib/ai/prompt-cache.ts:17). The default Base model is alibaba/qwen3.6-plus (lib/ai/models.ts:76), which Alibaba lists for explicit cache only, and AI Gateway's caching option is not set, so the default model's chats are probably never cached.

    The change. Add providerOptions.gateway.caching = 'auto' to the chat streamText call (and the other multi-turn calls), next to the existing Anthropic marks, or add Alibaba cache marks for Alibaba models.

    Why it helps. Every turn and every tool step re-sends the tools, the system prompt and the history at full input price instead of the cached price (10% of input on Alibaba explicit cache).

    Estimate about 8M prompt tokens per 1,000 calls not served from cache (estimate)

    Assumptions. half of chat calls use the Base model; an average chat call sends 20k prompt tokens of which 16k repeat from the previous call; Alibaba docs (2026-11-24) list qwen3.6-plus for explicit cache only; AI Gateway docs (2026-09-11) list Alibaba as an explicit-caching provider and say requests pass through unchanged when caching is not set. Whether the gateway translates Anthropic marks for Alibaba is not stated.

    To confirm. In the AI Gateway logs (or usage.inputTokenDetails.cacheReadTokens), cached input tokens on the second turn of a Base model chat sent within 5 minutes of the first. 0 confirms the finding.

  2. LP-PD-05 Drop stale history on purpose, not by stripping

    apps/web/src/lib/db/pg/repositories/chat-repository.pg.ts:158 · severity medium · saves prefill · how often is assumed

    What happens. Each turn sends the newest 50 messages (CHAT_MESSAGE_WINDOW, lib/const.ts:12). Once a thread passes 50 messages the window slides by two messages every turn, so the first message of the history changes on every turn and the provider can reuse nothing after the system prompt.

    The change. Move the window in steps: start it at a multiple of, say, 20 messages, so it stays byte-stable for 10 turns and then drops 20 at once; or compact past a size threshold.

    Why it helps. On threads past 50 messages every turn re-pays the whole history at full price, and on Anthropic also writes it to the cache at 1.25x, a write that is never read.

    Estimate about 2.5M prompt tokens per 1,000 calls not served from cache (estimate)

    Assumptions. 10% of chat calls come from threads past 50 messages; such a history is about 25k tokens after slimming. The stepped window sends up to 20 more old messages, so it needs an answer check on long threads.

    How often. How many chat requests come from threads with more than 50 messages (count of chat_message rows per thread at request time).

  3. LP-PC-06 Append-only history

    apps/web/src/app/api/chat/chat/slim-history.ts:124 · severity medium · saves prefill · how often is assumed

    What happens. slimHistoryForModel keeps the last 2 user turns whole and truncates older bulky parts. A turn's large tool result is sent in full for two turns and truncated on the third, which rewrites history at that turn, so the cache misses from there to the end once per bulky turn.

    The change. Truncate the bulky part once when it is stored for later turns (or move the keep-recent boundary in steps), so each old message has one form after it is first sent.

    Why it helps. The rewrite makes the last two turns, including their full tool results, prefill again at full price.

    Estimate about 1M prompt tokens per 1,000 calls not served from cache (estimate)

    Assumptions. a bulky part ages out on 1 turn in 6; the two recent turns hold about 6k tokens. The slimming still saves tokens on long threads, so this is a timing change, not a removal.

    How often. Share of turns that follow, two turns later, a turn with a tool result or document preview over 2,000 characters.

  4. LP-PC-01 Put cache marks where the provider needs them

    apps/web/src/app/api/chat/route.ts:1392 · severity medium · saves prefill · how often is assumed

    What happens. Inside one turn the tool loop runs up to 10 steps, but the last cache mark stays on the user's message (withTrailingCacheBreakpoint before streamText). prepareStep never moves it, so on Anthropic every step re-reads the earlier steps' tool calls and results at full price.

    The change. In prepareStep, return the messages with a cache mark on the last loop message (reuse withTrailingCacheBreakpoint on the step's messages), or rely on gateway caching 'auto', which marks the last message of each request.

    Why it helps. Anthropic caches only up to a mark; everything after the last mark is billed as new input on each step.

    Overlaps. Finding 1 loses some of the same tokens; the totals count the larger once.

    Estimate about 450k prompt tokens per 1,000 calls not served from cache (estimate)

    Assumptions. 15% of chat calls on Anthropic models; multi-step turns average 3 steps with about 3k tokens of tool output per step.

    How often. Share of chat calls on Anthropic models and the average step count per turn, from the gateway logs or the [chat] step=N log lines.

  5. LP-GE-10 Structured output instead of parse-and-re-ask

    apps/web/src/lib/ai/resilient-object.ts:98 · severity medium · saves prefill and generation · how often is assumed

    What happens. When strict generateObject fails to parse, the model's text is thrown away and the same prompt is sent again with generateText, to run extractJsonObject on the new answer. The failed answer was usually the same JSON inside a code fence.

    The change. Pass experimental_repairText to the first generateObject call and run extractJsonObject on the text it receives; keep the second call only for when that fails.

    Why it helps. Each parse failure pays for a second full call: the whole source text and payload again, and the whole answer again.

    Estimate about 50k prompt and 7.5k output tokens per 1,000 calls (estimate)

    Assumptions. project-brain calls are 10% of all calls, 5% of them fail strict parsing, a call sends 10k prompt tokens and returns 1.5k.

    How often. How often the 'strict parse failed, trying text repair' warning is logged per project-brain run.

  6. LP-GE-05 Retry only retryable errors; never retry a truncation unchanged

    apps/web/src/lib/ai/resilient-object.ts:141 · severity medium · saves prefill and generation · how often is assumed

    What happens. All four project-brain callers pass fallbackModel: customModelProvider.getModel(), which with no argument returns the Base model, the same id as projectBrainModelId (models.ts:337-339). So layer 3 repeats layer 1 exactly: same model, same system, same prompt.

    The change. Skip layer 3 when the fallback equals the primary model, or pass a different model (for example one with reliable structured output) as the fallback.

    Why it helps. An identical request that just failed to parse is sent again with the full prompt and a full answer.

    Estimate about 50k prompt and 7.5k output tokens per 1,000 calls (estimate)

    Assumptions. same as the text-repair finding; this applies when both earlier layers fail.

    How often. How often 'fallback model failed' or 'recovered via fallback model' is logged.

  7. LP-PC-03 No ids, timestamps, versions or paths at the top of the prompt

    apps/web/src/lib/project-brain/prompts.ts:11 · severity low · saves prefill · costs no tokens today

    What happens. The project-brain system prompts start with the current date and time to the minute, so they differ between runs.

    The change. Move the date into the user content if these prompts grow.

    Why it helps. A per-minute value at the top stops the provider reusing the rest of the prompt.

    Estimate 0 today (estimate)

    Assumptions. the extraction prompt is under about 800 tokens, below the 1,024-token minimum cacheable size, and the user content is unique per source, so nothing is lost now.

Already known to the project

The project’s own issues, pull requests or docs already cover these, so they are not counted above and are left out of the static score.

  1. LP-PC-03 No ids, timestamps, versions or paths at the top of the prompt

    apps/web/src/app/api/chat/temporary/route.ts:88 · severity medium · saves prefill · how often is assumed

    What happens. Temporary chat puts the current date and time (to the minute) at the end of the system prompt and sends no cache marks. The main chat route moved the clock out of the system prompt for exactly this reason (retrieval-context.ts:24-29).

    The change. Build the request like the main route: system as a message with the cache mark, withTrailingCacheBreakpoint on the messages, and the date through appendRetrievalContext.

    Why it helps. Each new minute changes the system prompt, so the whole history after it is a cache miss on most turns, and Anthropic or Alibaba models cache nothing without marks.

    Already reported. Open pull request #371 (2026-02-21, an outside contributor, against the layout before 2.0) separates the static system prompt from the date and time, temporary chat included. The team's 2.0 work superseded it for the main chat route, not for temporary chat. https://github.com/keinsaasforever/better-chatbot/pull/371

    Estimate about 150k prompt tokens per 1,000 calls not served from cache (estimate)

    Assumptions. 3% of chat calls are temporary chats with about 5k tokens of reusable prompt.

    How often. Share of chat requests that go to the temporary chat route.

Other problems the audit found

Problems read in the code that cost no tokens of their own, such as a cost meter or an error path. Not scored.

  1. Chat gives the model the server's clock without a time zone

    apps/web/src/lib/ai/prompts.ts:94

    What happens. buildCurrentDateTimePrompt formats new Date() with date-fns format, which uses the server's zone (UTC on Vercel and in most containers) and does not name it. Voice already uses Europe/Berlin for this reason (prompts.ts:243). Chat, temporary chat and project brain tell the model a UTC time as if local.

    The change. Format with the same Intl.DateTimeFormat in Europe/Berlin (or the user's zone) and name the zone in the sentence.

    To check. Unit test with TZ=UTC at 23:30 UTC: the prompt shows the next day in Berlin and names the zone.

  2. Temporary chat describes tools it never passes

    apps/web/src/app/api/chat/temporary/route.ts:86

    What happens. Temporary chat sends buildUserSystemPrompt, whose tool_usage block (prompts.ts:165-179) tells the model to search the web, run python-execution and draw charts, but the streamText call has no tools. The model can promise a search it cannot run.

    The change. Leave out the tool_usage block when no tools are bound, as the main route does with buildToolCallUnsupportedModelSystemPrompt.

    To check. Ask temporary chat for today's weather: it says it cannot search instead of announcing a search.

The maintainers’ response

No response from the maintainers yet.

How this was made

The llm-call-audit skill read the code and wrote findings.json; nothing was run against the project’s providers. The full audit page is what leanprompt report --html writes from the same findings.

Corrections and takedown: email info@podwarden.com or open an issue.