Research and question loops send Claude no cache marks; every round pays in full

Cache never hits Reported

An open-source AI tutor with research, question and chat agents; its chat loop already sets cache marks for Claude, and its TypeScript engine flags cut-off answers.

The mistake: Cache never hitsAPPDeepTutorPROMPTSystem prompt, toolschemas and earlierrounds, resentPROVIDER CACHENo cache_control inthese loops:nothing is reusedYOU PAYabout 1.8Mfull-price prompttokens per 1,000callsOnly the chat loop got cache marks; these loops didnotThe mistake: Cache never hitsAPPDeepTutorPROMPTSystem prompt, toolschemas and earlierrounds, resentPROVIDER CACHENo cache_control inthese loops:nothing is reusedYOU PAYabout 1.8Mfull-price prompttokens per 1,000callsOnly the chat loop got cache marks; these loopsdid notThe mistake: Cache never hitsAPPDeepTutorPROMPTSystem prompt, tool schemas andearlier rounds, resentPROVIDER CACHENo cache_control in these loops:nothing is reusedOnly the chat loop got cachemarks; these loops did notYOU PAYabout 1.8M full-price prompttokens per 1,000 calls

≈ 1,800,000 tokens per 1,000 calls, of prompt read at full price on OpenRouter Claude profiles 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
HKUDS/DeepTutor 64.4k GitHub stars
Commit audited
122cd98
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 122cd98 and found 2 places where its LLM calls could use fewer tokens, plus 12 other problems, not scored. Static score: 91 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

    deeptutor/runtime/agentic/labeled_step.py:184 · severity high · saves prefill · how often is assumed

    What happens. The research, question and page-index loops, and the context pre-pass, call chat.completions.create themselves and never add cache_control. The chat loop got its breakpoints in #2081; these paths did not. A Claude model reached through OpenRouter caches nothing on them, so every round of a tool loop pays full price for the system prompt, tool schemas and all earlier rounds.

    The change. In run_labeled_step (and the explorer's _call_llm) call the same helper agent_loop._apply_prompt_caching uses: OpenAICompatProvider.supports_prompt_caching_for(model), the binding check, then OpenAICompatProvider._apply_cache_control(messages, tools, stable_history_end=False).

    Why it helps. Anthropic caches a prefix only at a cache_control breakpoint. A loop of N rounds re-sends a growing prefix N times; with marks, rounds 2..N read it at the cached rate instead of the full input rate.

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

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

    Assumptions. average_call 12,000 prompt tokens; 30% of calls run through these loops; about 8,000 reusable tokens per such call (system, tools, earlier rounds); one write per loop priced at 1.25x. Only OpenRouter-Claude profiles are affected.

    Who reaches this. only OpenRouter profiles that route anthropic/ or claude models; native Anthropic bindings get marks in their provider

    How often. how many calls per 1,000 run through run_agentic_loop, run_labeled_step or the explorer loop on an OpenRouter profile whose model id starts with anthropic/ or claude (usage.sqlite3 rows with cache_read_input_tokens = 0 for those sources)

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

    deeptutor/capabilities/explore_context/explorer.py:428 · severity medium · saves prefill · how often is assumed

    What happens. The context pre-pass lays its first messages out so the bytes before the question are stable (_cache_shaped_messages says 'becomes a cache hit'), but its request carries no cache_control, so on an OpenRouter Claude profile the stable prefix is never cached.

    The change. Apply the shared cache-marking helper to kwargs before the create call, as agent_loop does.

    Why it helps. The layout work only pays when the provider is told where the prefix ends.

    Estimate about 240k prompt tokens per 1,000 calls (estimate)

    Assumptions. 4% of calls are exploration rounds; about 6,000 reusable tokens each.

    Who reaches this. only OpenRouter-Claude profiles, only turns with attached sources

    How often. how many chat turns attach sources that trigger the explore_context pre-pass on an OpenRouter-Claude profile

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. reason tool returns an answer cut off at max_tokens as if complete

    deeptutor/tools/reason.py:104 · LP-GE-12 Read the finish reason before using an answer · severity medium

    What happens. reason() streams with llm_stream and no StreamOutcome, then returns the joined text. A reply the provider stopped at the 4096-token default cap (hidden reasoning tokens count against it) goes back to the calling loop as a finished analysis. The TypeScript engine's model tool flags the same case with truncated: true (packages/chat-engine/src/tools/model-tool.ts:83).

    The change. Pass outcome=StreamOutcome() to llm_stream and add outcome.truncated to the returned dict and the ToolResult metadata; optionally append a one-line note to the answer.

    To check. Set the solve max_tokens to 50, call reason, and check the result says truncated.

  2. brainstorm tool returns an answer cut off at max_tokens as if complete

    deeptutor/tools/brainstorm.py:89 · LP-GE-12 Read the finish reason before using an answer · severity medium

    What happens. Same shape as reason: llm_stream with no StreamOutcome; the 2048-token default cap can end the idea list mid-item and the result is returned as complete.

    The change. Same as reason: pass outcome=StreamOutcome() and report outcome.truncated.

    To check. Set the brainstorm max_tokens to 50 and check the result reports truncated.

  3. Research note summary accepts a cut-off reply

    deeptutor/agents/research/pipeline.py:1272 · LP-GE-12 Read the finish reason before using an answer · severity low

    What happens. The non-streaming note call reads choices[0].message.content and never looks at finish_reason; a reply cut at the note budget becomes the stored note.

    The change. Read response.choices[0].finish_reason and fall back to cleaned[:600] when it is a truncation reason (finish_was_truncated).

    To check. With the note budget set to 20 tokens the stored note equals the raw-answer fallback.

  4. Question tool summarizer accepts a cut-off summary

    deeptutor/agents/question/pipeline.py:1122 · LP-GE-12 Read the finish reason before using an answer · severity low

    What happens. The streamed summary is joined and passed forward; finish_reason is never read, so a summary cut at tool_summarizer.max_tokens (default 800) replaces the raw tool result.

    The change. Track the last chunk's finish_reason and pass the raw tool result forward when it is a truncation reason.

    To check. With max_tokens 20 the raw result is passed forward and a notice is shown.

  5. Context exploration accepts a cut-off investigation

    deeptutor/capabilities/explore_context/explorer.py:327 · LP-GE-12 Read the finish reason before using an answer · severity low

    What happens. _call_llm never reads finish_reason; a final round stopped at the loop max_tokens is injected into the answer as the investigation.

    The change. Record finish_reason in _CallResult and, when truncated, fall back to the single pass or mark the briefing as partial.

    To check. With a tiny loop budget the briefing header says it is partial.

  6. Cache writes are never recorded for OpenAI GPT-5.6+ or OpenRouter

    deeptutor/services/llm/usage_frame.py:146 · LP-GE-11 Meter every billed token, cached and unfinished ones too · severity medium

    What happens. usage_breakdown reads cache writes only from cache_creation_input_tokens (Anthropic's field). OpenAI's Responses usage reports input_tokens_details.cache_write_tokens and OpenRouter reports prompt_tokens_details.cache_write_tokens, and neither is read, so the usage ledger and the chat usage footer show no cache write column for them even though DeepTutor itself turns on explicit caching for GPT-5.6+ (openai_compat_provider.py:962), where writes cost 1.25x the input rate (checked on 2026-11-28).

    The change. Read cache_write_tokens from input_tokens_details and prompt_tokens_details as a second source for counts['cache_creation_input_tokens'].

    To check. Feed usage_breakdown a frame with input_tokens_details.cache_write_tokens = 7000 and check cache_creation_input_tokens is 7000.

  7. Cost summary prices every unlisted model as gpt-4o-mini and ignores cache pricing

    deeptutor/logging/stats/llm_stats.py:56 · LP-GE-11 Meter every billed token, cached and unfinished ones too · severity low

    What happens. get_pricing falls back to the gpt-4o-mini rate for any model not in a table of nine older ids, and UsageTracker.summary multiplies all prompt tokens, cached ones included, by that one input rate. The CLI prints the result as cost=$ (deeptutor_cli/common.py:385). The number is a rough estimate for a current model.

    The change. Label the figure as an estimate, or return None for an unlisted model instead of a gpt-4o-mini price.

    To check. A claude-sonnet-5-5 run prints no cost or a labelled estimate.

  8. Codex OAuth responses drop the usage the parser can read

    deeptutor/services/llm/provider_core/openai_codex_provider.py:318 · LP-GE-11 Meter every billed token, cached and unfinished ones too · severity low

    What happens. _request_codex calls consume_sse(response, on_content_delta) without on_provider_event, so the usage the Responses parser reads from response.completed (parsing.py, _response_usage) is discarded and LLMResponse carries no usage for this binding. Whether the ChatGPT endpoint sends usage is not established here.

    The change. Pass an on_provider_event hook that keeps the usage event and return it on LLMResponse.

    To check. With a Codex profile the usage ledger records prompt and completion tokens for a call.

  9. Codex path ignores max_tokens

    deeptutor/services/llm/provider_core/openai_codex_provider.py:169 · LP-GE-17 Send the output cap the owner set, on every path · severity low

    What happens. chat() does del max_tokens, temperature, so the per-stage budgets and the truncation handling built on them do nothing on this binding. May be a limit of the endpoint; not checked.

    The change. Document it next to the del, or forward max_output_tokens if the endpoint accepts it.

    To check. A tiny budget on a Codex profile either ends the reply early or the code comment says why not.

  10. Anthropic thinking raises the configured output cap

    deeptutor/services/llm/provider_core/anthropic_provider.py:454 · LP-GE-17 Send the output cap the owner set, on every path · severity low

    What happens. With thinking on for the older (budget-token) families, max_tokens becomes max(max_tokens, budget + 4096), so a stage configured at a few hundred tokens (topic naming sends 60) is sent at 5,120 to 8,192 when a reasoning effort is set. Not confirmed that task calls carry an effort.

    The change. Skip thinking for calls whose cap is below the minimum budget, or document the override.

    To check. Log the final max_tokens for complete(max_tokens=60) on a claude-sonnet-4-5 profile with effort medium.

  11. Wizard fallback model lists name retired and deprecated OpenAI models

    deeptutor_cli/init_wizard.py:63 · LP-GE-13 Default to a model the provider still serves · severity low

    What happens. The OpenAI fallback list (used when GET /models fails) offers o4-mini, retired 2026-10-23, and gpt-4o-mini, gpt-4o, gpt-4.1 and gpt-4.1-mini, listed as Deprecated for 2027-01-14 (OpenAI deprecations page, checked on 2026-11-28).

    The change. Replace them with models the page lists as active.

    To check. Every id in LLM_FALLBACK_MODELS['openai'] is absent from the deprecations table.

  12. Anthropic default model id is retired

    deeptutor/runtime/agentic/client.py:347 · LP-GE-13 Default to a model the provider still serves · severity high

    What happens. claude-sonnet-4-20250514 is retired since 2026-06-15 (Anthropic deprecations page, checked on 2026-11-28); used when no model is configured.

    The change. Already proposed in #2888.

    In progress. open PR that moves both Anthropic fallbacks to a current model https://github.com/HKUDS/DeepTutor/pull/2888

    To check. See #2888.

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.