The page leanprompt report --html writes for keinsaasforever/better-chatbot at commit 6f41835, audited 2026-11-27. Every figure on this page is an estimate from reading the code, not a measurement of the app in use. Corrections and takedown: email info@podwarden.com or open an issue. Back to the audit of keinsaasforever/better-chatbot
leanprompt audit of keinsaasforever/better-chatbot
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.
5 places where this app pays for tokens it doesn't need
The biggest, at apps/web/src/lib/db/pg/repositories/chat-repository.pg.ts:158, about 2.5M tokens per 1,000 calls: each turn sends the newest 50 messages (CHAT_MESSAGE_WINDOW, lib/const.ts:12).
The largest item is an open question, at apps/web/src/app/api/chat/route.ts:1370, about 8M tokens per 1,000 calls if it holds: the only cache marks the chat sends are Anthropic ones (providerOptions.anthropic, lib/ai/prompt-cache.ts:17).
Togetherabout 4.1M tokens per 1,000 calls An estimate from the code, not a measurement. Findings that overlap are counted once. If the open questions hold, add about 7.5M per 1,000 calls.
Tokens per 1,000 calls, by finding, and each one's share of the total
Plus 1 open question to confirm, listed after the findings.
Plus 1 finding that costs no tokens today, listed with the findings and not counted as places.
Plus 1 finding the project already knows about (planned, in progress, reported or declined), listed after the findings. They are not in the totals or the score.
Plus 2 other problems the audit found, listed after the findings. Not scored.
What this costs you
Every figure here is an estimate from the code, not a measurement. Findings the audit marked as overlapping are counted once in the totals, the largest of each group; other findings can still overlap. Money is not part of the score. The table lists this app's findings first, largest first, then its open questions, then the questions for the server's administrator, then the findings the project already knows about, which are not in the totals.
8M not cached; the fix writes 1.5M to the cache at the write price
LP-PC-03 (already reported)
apps/web/src/app/api/chat/temporary/route.ts:88
150k not cached
To see this in money, add to leanprompt.yaml:
volume: {calls_per_month: N}: how many calls the app makes a month (with targets:, on each entry)
prices: per model: input, cached_input and output per 1M tokens (US dollars, or set currency), with source and checked date (or, self-hosted, engine: {prefill_tokens_per_second: N})
leanprompt never guesses a volume or a price. Your coding agent can look up the provider's current price page and add it with the date; how.
What's wrong and what to change
7 open, 1 already known to the project, 0 fixed. Largest first, by the audit's estimate of tokens lost per 1,000 calls.
1
Each turn sends the newest 50 messages (CHAT_MESSAGE_WINDOW, lib/const.ts:12).
Finding 1. apps/web/src/lib/db/pg/repositories/chat-repository.pg.ts:158mediumLP-PD-05how often is assumed
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.
What to 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 matters
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.
How often it happens
The code shows the problem; how often it happens is assumed. To measure it: How many chat requests come from threads with more than 50 messages (count of chat_message rows per thread at request time).
Estimate
about 2.5M prompt tokens per 1,000 calls not served from cache (estimate)What the estimate assumes
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.
2
slimHistoryForModel keeps the last 2 user turns whole and truncates older bulky parts.
Finding 2. apps/web/src/app/api/chat/chat/slim-history.ts:124mediumLP-PC-06how often is assumed
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.
What to 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 matters
The rewrite makes the last two turns, including their full tool results, prefill again at full price.
How often it happens
The code shows the problem; how often it happens is assumed. To measure it: Share of turns that follow, two turns later, a turn with a tool result or document preview over 2,000 characters.
Estimate
about 1M prompt tokens per 1,000 calls not served from cache (estimate)What the estimate assumes
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.
3
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.
Finding 3. apps/web/src/app/api/chat/route.ts:1392mediumLP-PC-01how often is assumed
What to 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 matters
Anthropic caches only up to a mark; everything after the last mark is billed as new input on each step.
How often it happens
The code shows the problem; how often it happens is assumed. To measure it: 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.
Overlaps
Finding 7 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)What the estimate assumes
15% of chat calls on Anthropic models; multi-step turns average 3 steps with about 3k tokens of tool output per step.
4
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.
Finding 4. apps/web/src/lib/ai/resilient-object.ts:98mediumLP-GE-10how often is assumed
The failed answer was usually the same JSON inside a code fence.
What to 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 matters
Each parse failure pays for a second full call: the whole source text and payload again, and the whole answer again.
How often it happens
The code shows the problem; how often it happens is assumed. To measure it: How often the 'strict parse failed, trying text repair' warning is logged per project-brain run.
Estimate
about 50k prompt and 7.5k output tokens per 1,000 calls (estimate)What the estimate assumes
project-brain calls are 10% of all calls, 5% of them fail strict parsing, a call sends 10k prompt tokens and returns 1.5k.
5
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).
Finding 5. apps/web/src/lib/ai/resilient-object.ts:141mediumLP-GE-05how often is assumed
So layer 3 repeats layer 1 exactly: same model, same system, same prompt.
What to 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 matters
An identical request that just failed to parse is sent again with the full prompt and a full answer.
How often it happens
The code shows the problem; how often it happens is assumed. To measure it: How often 'fallback model failed' or 'recovered via fallback model' is logged.
Estimate
about 50k prompt and 7.5k output tokens per 1,000 calls (estimate)What the estimate assumes
same as the text-repair finding; this applies when both earlier layers fail.
6
The project-brain system prompts start with the current date and time to the minute, so they differ between runs.
Move the date into the user content if these prompts grow.
Why it matters
A per-minute value at the top stops the provider reusing the rest of the prompt.
Estimate
0 today (estimate)What the estimate assumes
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.
Open questions
Findings outside this repository, or whose behaviour the code cannot show. They still count in the score. Each says what to confirm.
7
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.
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.
Change, if confirmed
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 matters
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)What the estimate assumes
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.
Already known to the project
The project's own issues, pull requests or docs already cover these, so they are not new and are not in the totals or the score. Read the link before working on one.
8
Temporary chat puts the current date and time (to the minute) at the end of the system prompt and sends no cache marks.
Finding 8. apps/web/src/app/api/chat/temporary/route.ts:88mediumLP-PC-03how often is assumed
The main chat route moved the clock out of the system prompt for exactly this reason (retrieval-context.ts:24-29).
What to 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 matters
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.
How often it happens
The code shows the problem; how often it happens is assumed. To measure it: Share of chat requests that go to the temporary chat route.
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)What the estimate assumes
3% of chat calls are temporary chats with about 5k tokens of reusable prompt.
Other problems the audit found
Problems the audit checked in the code that cost no tokens of their own, such as a spend meter that ignores cache tokens or a cut-off answer accepted as complete. They are not scored. Check each one before you change anything.
N1
Chat gives the model the server's clock without a time zone
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.
What to change
Format with the same Intl.DateTimeFormat in Europe/Berlin (or the user's zone) and name the zone in the sentence.
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.
What to change
Leave out the tool_usage block when no tools are bound, as the main route does with buildToolCallUnsupportedModelSystemPrompt.
Fix it with your coding agent
Paste this into Claude Code, Codex, Cursor or any coding assistant. It will check each finding against your code and propose fixes one at a time, asking before it changes anything.
Please check and fix the LLM-call findings from a leanprompt audit of this repository.
1. If the leanprompt skills are not installed for you here, install them: npx leanprompt@0.1.0-dev.14 install
2. Read .leanprompt/findings.json and leanprompt-report.md. If they are missing, use the list below.
3. Check each finding against the code before you change anything. The audit can be wrong: if a finding does not hold, tell me why and leave that code alone.
4. Fix one finding at a time. Show me the change as a small diff and apply it only after I say yes.
5. Prove each fix with the measure-loop skill: a before and after on the same scenario. Ask me before running anything that sends real, billed requests.
6. When a fix is proved, set "fixed": true on its finding in .leanprompt/findings.json and run npx leanprompt@0.1.0-dev.14 score.
Findings, largest first:
1. LP-PD-05 at apps/web/src/lib/db/pg/repositories/chat-repository.pg.ts:158: 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.
2. LP-PC-06 at apps/web/src/app/api/chat/chat/slim-history.ts:124: 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.
3. LP-PC-01 at apps/web/src/app/api/chat/route.ts:1392: 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.
4. LP-GE-10 at apps/web/src/lib/ai/resilient-object.ts:98: 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.
5. LP-GE-05 at apps/web/src/lib/ai/resilient-object.ts:141: 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.
6. LP-PC-03 at apps/web/src/lib/project-brain/prompts.ts:11: The project-brain system prompts start with the current date and time to the minute, so they differ between runs.
Already known to the project (read the link first; do not duplicate work the project has planned or started):
- LP-PC-03 (apps/web/src/app/api/chat/temporary/route.ts:88): 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). 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
Open questions (confirm before fixing):
- LP-PC-01 (apps/web/src/app/api/chat/route.ts:1370): 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. 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.
Other problems found (check each against the code first; not scored):
- Chat gives the model the server's clock without a time zone (apps/web/src/lib/ai/prompts.ts:94): 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. Change: Format with the same Intl.DateTimeFormat in Europe/Berlin (or the user's zone) and name the zone in the sentence.
- Temporary chat describes tools it never passes (apps/web/src/app/api/chat/temporary/route.ts:86): 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. Change: Leave out the tool_usage block when no tools are bound, as the main route does with buildToolCallUnsupportedModelSystemPrompt.
Score
One number from 0 to 100 for how well this app avoids paying for the same tokens twice; higher is better. It is computed from the findings above (static) or from your usage log (measured).
69static
E, prefill reuse
0.50
S, prefix stability
1.00
G, generation waste
0.02
Default shares
0 of 7
Findings scored with severity defaults, not estimates. The fewer, the more the static score says about this app.
It leaves out 1 finding the project already knows about (planned, in progress, declined or reported); with them it would be 68.
A static score is an estimate from the audit's findings, not a measurement.
Scored 2026-11-27 (the date on the machine that ran it).
Questions for the owner
These were not answered during the audit. Until they are, the page shows tokens only and ranks the advice without a priority.
Priority: which matters most for this app, cost, speed or quality (or weights)? Not set, so the advice uses equal weights. Answer with priority: in leanprompt.yaml.
Volume: how many calls does the app make a month? Answer with volume: {calls_per_month: N, source: "..."} in leanprompt.yaml.
Prices: what does each model cost (input, cached input, output per 1M tokens, with the source and the date it was read, and the currency if not US dollars)? Answer with prices: in leanprompt.yaml.
If this helped
If this page saved you some digging, a star or a word to a colleague helps other builders find it.
leanprompt checked how our app calls LLMs and found 5 places where it pays for tokens it doesn't need, about 4.1M tokens per 1,000 calls, an estimate from the code. It explains each finding in plain English and hands the fixes to your coding agent. It is free, open source, and runs locally: https://github.com/Podwarden/leanprompt
The README badge
The badge needs a public repository on GitHub, GitLab.com or Bitbucket with .leanprompt/score.json committed: shields.io reads the file from there. This repository's remote is not one of those, so there is no snippet to paste.