A daily spending cap that can be overshot by about a third

A small meter box watches coins pour through a pipe into a flower pot, while some coins spill from a side opening and roll away behind it unseen.

Blind spend meter Reported

A gardening-advice demo on Claude that already marks its tools and system prompt for Anthropic's cache.

≈ 1,200,000 tokens per 1,000 calls, the largest finding: tool-loop messages sent without a cache mark 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
wyattlindsey/rootwise
Commit audited
ff18600
leanprompt version
0.1.0-dev.13
Audit date
Status
The maintainers have been told about this audit; no response yet.
Maintainers
the issue

A gardening-advice demo on Claude with a tool loop. It already marks its tools and system prompt for Anthropic's cache; the audit found the tool loop's own messages unmarked, and, outside the rules, two gaps in the daily budget meter. Audited with leanprompt 0.1.0-dev.11, re-scored with 0.1.0-dev.13.

leanprompt read the code at commit ff18600 and found 3 places where its LLM calls could use fewer tokens, plus 2 other problems, not scored. Static score: 87 of 100 (an estimate from the findings). The code may have changed since.

Findings

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

    lib/model/client.ts:60 · severity medium · saves prefill

    What happens. Only the system prompt carries a cache mark. Inside one question the tool loop calls the model again after every tool round, and each call resends the question, the model's earlier tool requests and every tool result so far. None of that part is marked, so it is never read from the cache and is paid at the full input price on every round.

    The change. Also mark the newest message: at send time, add cache_control {type: 'ephemeral'} to the last content block of the last message (or set the request-level automatic cache_control). Keep the stored history unchanged. Update the budget meter to count cache writes and reads at the same time (see the notes).

    Why it helps. Each tool round extends the previous request byte for byte, so with a mark on the newest message the next round reads everything before its new tool results from cache at 0.1x the input price instead of paying full price again.

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

    Assumptions. 3 model calls per question (two tool rounds and the answer, as route.ts:13 and :33 describe); about 10k uncached input tokens per question, inferred from the 5-7 cents per question in route.ts:33 at the prices in lib/budget/pricing.ts; tool rounds of about 3k tokens each; the 3.5k-token tools and system head is already cached. A heavy question (README.md:80, about 40k input) loses proportionally more.

  2. LP-GE-04 Stream long outputs; timeouts that match generation time

    app/api/chat/route.ts:15 · severity low · saves prefill and generation · how often is assumed

    What happens. A whole question, up to 8 model calls at high effort with adaptive thinking and a 16,000-token output limit each, must finish inside one 60-second function. If it does not, the function is stopped: the visitor gets a cut-off answer, the tokens already generated are paid for, and the budget meter never records them because the done event is never sent.

    The change. Measure first. If questions come near 60 seconds, either raise maxDuration (the plan allows it) or lower the output and effort settings for later rounds; and record usage after every model call rather than only on done.

    Why it helps. Tokens generated before the function is stopped are billed and thrown away, and a visitor who gets no answer usually asks again, paying for the whole question twice.

    Estimate about 137k prompt and 9k output tokens per 1,000 calls (estimate, if 2% of questions hit the limit)

    Assumptions. 2% of questions exceed 60 seconds (unknown; the comment at lib/model/client.ts:7-10 shows the author watches p95 latency); a cut-off question has cost 20.5k prompt and 1.4k output tokens and is asked again.

    How often. The p95 duration of a whole question (all tool rounds) in production, compared with maxDuration = 60 seconds. Vercel's function logs show runs that hit the limit.

  3. LP-PC-07 Shared context before per-user context

    lib/chat/system-prompt.ts:94 · severity low · saves prefill · how often is assumed

    What happens. The visitor's coordinates (line 94) and today's date (line 54) are inside the one system block that carries the cache mark. Two visitors with different locations therefore never share the cached tools and instructions, and the date changes the block once a day.

    The change. Split the system prompt into two blocks: the fixed instructions first with the cache mark, then a short second block with the date and the location line and no mark. Alternatively put a mark on the last tool as well.

    Why it helps. A cache entry is found only for the exact bytes up to the mark, so one changed coordinate in the last paragraph makes the whole 3.5k-token head a miss for that visitor.

    Estimate about 57k prompt tokens per 1,000 calls (estimate, depends on traffic)

    Assumptions. 6 calls per conversation (two questions of three calls), so 167 first calls per 1,000; 10% of them start within the 5-minute cache lifetime of another visitor with a different location or date; about 3.4k tokens would be shared. At the demo's stated traffic (about 15 questions a day) the real figure is close to zero.

    How often. How often a visitor starts a conversation within 5 minutes of another visitor's request, and how often their shared location (or the UTC date) differs. Read it from the Vercel request log or the app's budget store timestamps.

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. The daily budget meter undercounts spend

    lib/budget/pricing.ts:9

    What happens. costCents is fed message.usage.input_tokens and output_tokens only (lib/chat/run.ts:98). On Anthropic input_tokens counts only the tokens after the last cache breakpoint, so cache writes (billed at 1.25x) and cache reads (0.1x) never reach the meter, and the daily cap can be overspent by about a third.

    The change. Pass cache_creation_input_tokens and cache_read_input_tokens to the meter and price each at its own rate.

    Why it matters. The cap is what keeps the public demo inside its budget. Adding cache marks to the agent loop moves more tokens into the cache fields and widens the gap.

    To check. For one question, the meter's cents match the sum of the three input fields and the output at the prices in lib/budget/pricing.ts.

  2. Questions that end without done are not metered

    lib/chat/handle-chat-request.ts:156

    What happens. recordUsage runs only on a done event. A question that hits the 8-round limit, errors, is refused after partial output or is aborted has been billed but is never counted.

    The change. Record usage after every model call (or in a finally block), not only on done.

    To check. Abort a question after the first tool round: the budget store shows that round's tokens.

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.