No chapter marks its prompt for caching, so each agent turn is billed in full

Four paper cups stand on a small wooden rack beside two clean glass jars that nobody uses.

Cache never hits Reported

A step-by-step teaching build of a Claude-Code-style agent: 17 chapters with tests, in three languages, and an active maintainer team.

≈ 7,700,000 tokens per 1,000 calls, prompt tokens the cache could have served, on the default Claude model 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
shareAI-lab/learn-claude-code
Commit audited
448c75b
leanprompt version
0.1.0-dev.15
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.

leanprompt read the code at commit 448c75b and found 4 places where its LLM calls could use fewer tokens, plus 3 other problems, not scored. Static score: 47 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

    s08_context_compact/code.py:554 · severity high · saves prefill

    What happens. Every chapter's agent loop resends the whole growing conversation on each turn with no cache_control mark anywhere in the repository, so on Anthropic nothing is ever read from the prompt cache. The compaction chapter teaches how to shrink context but never shows that the unchanged part of it can be reused at a tenth of the price.

    The change. Mark the end of the tools, the end of the system prompt and the newest message, on copies made at send time (a ten-line helper). Put it in s08, where context cost is the topic, and say in its README why it is safe next to compaction.

    Why it helps. Without marks each turn pays the full input price for the system prompt, the tools and all earlier turns, although the model saw them seconds ago. A coding-agent loop returns within seconds, so a mark pays back after the first read.

    Overlaps. Finding 2, 3 loses some of the same tokens; the totals count the larger once.

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

    Assumptions. default Anthropic target (claude-sonnet-4-6, .env.example), 9,000-token average call: about 2,000 for system plus tools and 7,000 of history (s08 compacts at 50,000 characters, about 12k tokens); about 1,000 new tokens per turn; calls seconds apart; first turns under the 1,024-token minimum lose nothing. The Anthropic-compatible hosts listed in .env.example (MiniMax, GLM, Kimi, DeepSeek) cache by their own rules: unknown, check each provider's docs. Figures are per 1,000 loop calls of one chapter run by a learner.

  2. LP-PC-02 Stable content first, without hurting meaning

    s15_integrated_harness/code.py:858 · severity high · saves prefill

    What happens. The capstone rebuilds its system prompt on every model call and writes the current time to the second into it. The prompt is therefore different on every call, so once cache marks exist the provider can reuse nothing after the time line: not the skills catalog, not the memory text, not the conversation.

    The change. Remove the time line from the system prompt. If the model needs the date, give the date only, or put the time in the newest user message.

    Why it helps. The cache reads a prefix byte for byte; one changed byte early in the system prompt re-prices everything after it, which is the whole conversation on every call.

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

    Estimate about 7.4M prompt tokens per 1,000 loop calls would still miss the cache after marks are added (estimate)

    Assumptions. marks added as in the other finding; the part before the time line (about 750 tokens of fixed sections) and the tool list stay cacheable; 7,000 tokens of history plus a 400-token tail of the system prompt are lost on each call.

  3. LP-PC-02 Stable content first, without hurting meaning

    s09_memory/code.py:731 · severity medium · saves prefill · how often is assumed

    What happens. The memory records chosen for each new user request are written into the system prompt (build_system, lines 353-354, used at line 731; the same in s15 lines 863-864). The system prompt then changes on every new request, so the whole earlier conversation is re-priced at its first call. Issue 311 reported this in May and PR 312 moved the records into the user turn; the current tree has them in the system prompt again.

    The change. Keep the memory index in the system prompt and put the selected records in the newest user message, as PR 312 did.

    Why it helps. The system prompt sits before the messages, so any change to it makes every message after it a cache miss.

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

    Estimate about 1.2M prompt tokens per 1,000 loop calls (estimate)

    Assumptions. 6 loop calls per user request; one lost history of about 7,000 tokens per request; only matters once marks exist; applies when the memory store is not empty.

    How often. how many loop calls a typical user request makes, and whether the .memory folder holds records

  4. LP-GE-06 Cache exact responses for deterministic requests

    s15_integrated_harness/code.py:3184 · severity medium · saves prefill and generation · how often is assumed

    What happens. The capstone's loop calls update_context on every cycle (line 3184), and that runs the s09 memory selection (s09 lines 285-310), which is a separate model call. The inputs are the same user text on every tool turn, so one request with six tool turns asks the model the same question six times. In s09 the same call runs once per request.

    The change. Run update_context once per user request, before the while loop, as s09 does; refresh only when a memory is written.

    Why it helps. Each extra selection call sends the request text and up to 12,000 characters of catalog, adds a blocking round trip before every turn, and, because the answer is sampled, can change the system prompt between turns.

    Estimate about 580k prompt tokens and 17k output tokens per 1,000 loop calls (estimate)

    Assumptions. 6 loop calls per user request, so 5 of 6 selection calls repeat; about 700 tokens per selection prompt, 20 output tokens; memory store not empty (selection returns early when it is).

    How often. how many loop calls a typical user request makes, and whether the .memory folder holds records

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. A compaction summary cut at 2,000 tokens replaces the whole history

    s08_context_compact/code.py:486 · LP-GE-12 Read the finish reason before using an answer · severity high

    What happens. summarize_history asks for at most 2,000 tokens and never reads stop_reason. A summary that stopped at the limit becomes the entire conversation (compact_history, line 504) and is trusted as complete. The same pattern is at s15_integrated_harness/code.py line 2205.

    The change. Read response.stop_reason; on max_tokens, ask again with a higher limit or keep the old messages instead of replacing them.

    Why it matters. Nothing fails: the model continues from a summary that is missing its last items, such as remaining work and user constraints.

    To check. Stub the client to return stop_reason max_tokens: the compactor must not return a one-message history.

  2. s15 compaction summarises the oldest 80,000 characters and drops the newest

    s15_integrated_harness/code.py:2193

    What happens. summarize_history cuts the JSON with [:80000], which keeps the start of the conversation. When history is longer than that, the newest turns, which hold the current state, are not summarised and not marked as omitted. Issue 109 reported the same cut in the old chapter; agents/s06_context_compact.py:118 and agents/s_full.py:255 use [-80000:], and s08_context_compact/code.py:469-475 keeps head and tail with a marker.

    The change. Use the s08 approach: keep a head, a tail and an 'omitted' marker, or summarise only the older part and keep the newest messages.

    Why it matters. A summary of the wrong end of a long conversation makes the agent restart from stale state.

    To check. Feed a history of 120,000 characters whose last message holds a distinctive string: it must reach the summary prompt.

  3. The goal loop's token counter reads two usage fields and skips the evaluator

    s17_goal_loop/code.py:120 · LP-GE-11 Meter every billed token, cached and unfinished ones too · severity medium

    What happens. _usage_total adds input_tokens and output_tokens only (used at line 705) and the status shows it as 'Tokens' (line 349). Cache read and write tokens are left out, and the evaluator's own call (line 261) is never counted. It enforces nothing, so it only misreports; today, with no cache marks, only the evaluator is missing.

    The change. Add cache_read_input_tokens and cache_creation_input_tokens, and add the evaluator response's usage to the same total.

    To check. For one goal turn, the status total equals the sum of the usage of the main call and the evaluator call.

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.