The page leanprompt report --html writes for Nutlope/pdftochat at commit 94eb281, 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 Nutlope/pdftochat

leanprompt audit of Nutlope/pdftochat

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.

2 places where this app pays for tokens it doesn't need

The biggest, at utils/ragChain.ts:28, about 360k tokens per 1,000 calls: the answer prompt puts the four retrieved chunks inside the system message, before the chat history.

Together about 371k tokens per 1,000 calls An estimate from the code, not a measurement.

Tokens per 1,000 calls, by finding, and each one's share of the total

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 can overlap (one failed call can show up in two), so the sum may count some tokens twice. 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.

FindingWhereTokens per 1,000 calls
LP-PC-02utils/ragChain.ts:28360k not cached
LP-GE-03app/api/chat/route.ts:4211k output
LP-GE-04 (already reported)app/api/chat/route.ts:427k prompt, 2.4k output

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

2 open, 1 already known to the project, 0 fixed. Largest first, by the audit's estimate of tokens lost per 1,000 calls.

The answer prompt puts the four retrieved chunks inside the system message, before the chat history.

Finding 1. utils/ragChain.ts:28 high LP-PC-02

The chunks change on every question, so from the second question on the provider cannot reuse any of the earlier conversation and recomputes the whole history on every answer call.

What to change
Keep the system message fixed (the instructions only) and send the retrieved context with the newest question, after the history: for example a last user message 'Context: <context>...</context> Question: {input}'.
Why it matters
Together caches the longest identical prefix automatically, and its prompt-caching page says to put retrieved snippets at the end. Here the first difference is about 90 tokens in, so every follow-up answer prefills all earlier questions and answers again.
Estimate
about 360k prompt tokens per 1,000 calls not served from cache (estimate)
What the estimate assumes

4-question conversations, answers of about 400 tokens, 4 chunks of 1,000 characters (about 1,000 tokens); 7 calls per conversation (4 answers, 3 rephrase calls). Together's minimum cacheable prefix is not published. Qwen/Qwen3.5-9B has no cached-input price on Together (serverless models page, checked 2026-11-27), so on today's model the saving is prefill time, not money.

How to check the fix Ask two follow-up questions in one chat and log usage.prompt_tokens_details.cached_tokens of the answer call: before, it is about 0 on the third question; after, it covers the system message and the earlier turns. Check that answers still cite the document.

One model object with no output limit serves both calls: the rephrase call, whose output is used only as a short search query, and the answer.

Finding 2. app/api/chat/route.ts:42 low LP-GE-03 how often is assumed

Together's default is no limit (the model writes until it stops or hits the 262k context), so a small model that loops at temperature 0 keeps generating until the serverless function is cut off.

What to change
Use two model objects: maxTokens about 64 for the rephrase call (createHistoryAwareRetriever) and about 1,500 for the answer, and show a short 'answer cut off' marker when finish_reason is length.
Why it matters
Without a limit, a runaway answer or an answer written into the rephrase step is billed in full and thrown away.
How often it happens
The code shows the problem; how often it happens is assumed. To measure it: the longest answers and rephrase outputs in Together's request logs for this key, and whether any answer ran until the function was stopped
Estimate
about 11k output tokens per 1,000 calls (estimate)
What the estimate assumes

0.5% of the 571 answer calls per 1,000 calls run about 4,000 tokens past a useful answer; rephrase outputs are not counted.

How to check the fix Log finish_reason and usage.completion_tokens per call for a day: no rephrase output above 64 tokens and no answer above the limit; answers that hit it show the marker.

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.

The chat route sets no timeout and no maxRetries, so each model call gets the OpenAI client's 10-minute timeout and LangChain's 6 retries with backoff, and the route exports no maxDuration.

Finding 3. app/api/chat/route.ts:42 medium LP-GE-04 how often is assumed

The platform's function limit is shorter, so a slow or rate-limited call ends as a 504 after tokens were already billed.

What to change
Set timeout (for example 30 s to first byte) and maxRetries 1 or 2 on ChatTogetherAI, and export maxDuration for the route to match.
Why it matters
Retries and generation that the platform kills are billed and give the user nothing.
How often it happens
The code shows the problem; how often it happens is assumed. To measure it: how many /api/chat invocations end with FUNCTION_INVOCATION_TIMEOUT in the Vercel logs
Already reported
open since 2024-10: 504 FUNCTION_INVOCATION_TIMEOUT on every chat, several +1s, no maintainer reply; reported on the Mixtral/Pinecone version. https://github.com/Nutlope/pdftochat/issues/27
Estimate
about 9k tokens per 1,000 calls (estimate)
What the estimate assumes

1% of conversations hit a timeout after the rephrase call and part of an answer.

How to check the fix With a Together key that is rate limited, the route answers with an error within the timeout instead of a 504.

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.

Source page buttons get no page number with Chroma, the default store

Note 1. app/api/utils/vector_store/chroma.ts:44 not scored

sanitizeMetadata turns every nested object into a JSON string, so the chunk's loc ({pageNumber, lines}) is stored as the string '{"pageNumber":3,...}'. The chat client reads metadata['loc.pageNumber'] ?? metadata.loc?.pageNumber (app/document/[id]/document-client.tsx:92); on a string both are undefined, so all four sources collapse into one button reading 'p. ' and clicking it jumps to page NaN.

What to change
Flatten loc at ingest (store loc.pageNumber as its own number key, as the Pinecone and Mongo stores did) or JSON.parse metadata.loc when it is a string in extractSourcePageNumber.
Why it matters
The page links are how users check an answer against the PDF; since the move to Chroma (PR #30) they do not work.

How to check the fix Upload a multi-page PDF, ask a question whose answer is on page 3: the buttons show the real page numbers and jump to them.

Pinecone and MongoDB chats cannot retrieve: no embeddings passed, and the embedding model is gone

Note 2. app/api/utils/vector_store/index.ts:46 LP-GE-13 medium not scored

The chat route calls loadRetriever without embeddings (app/api/chat/route.ts:55), and index.ts:46 passes that undefined value on behind a non-null assertion, so retrieval on Pinecone or MongoDB throws on embedQuery. Ingest for those stores uses togethercomputer/m2-bert-80M-8k-retrieval (embeddings/index.ts:6), removed from Together serverless on 2025-06-13; Together's models page says no embedding models are offered via serverless. index.ts:14 and :36 still default to 'pinecone' when NEXT_PUBLIC_VECTORSTORE is unset, and .env.example still offers both.

What to change
Either remove the Pinecone and MongoDB options (and make chroma the default when the variable is unset), or pass an embeddings model that exists from the chat route and switch ingest to it.

How to check the fix With NEXT_PUBLIC_VECTORSTORE unset, a fresh deploy uploads and chats without an error, or fails at start with a message naming the variable.

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.15 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.15 score.

Findings, largest first:
1. LP-PC-02 at utils/ragChain.ts:28: The answer prompt puts the four retrieved chunks inside the system message, before the chat history. The chunks change on every question, so from the second question on the provider cannot reuse any of the earlier conversation and recomputes the whole history on every answer call.
2. LP-GE-03 at app/api/chat/route.ts:42: One model object with no output limit serves both calls: the rephrase call, whose output is used only as a short search query, and the answer. Together's default is no limit (the model writes until it stops or hits the 262k context), so a small model that loops at temperature 0 keeps generating until the serverless function is cut off.

Already known to the project (read the link first; do not duplicate work the project has planned or started):
- LP-GE-04 (app/api/chat/route.ts:42): The chat route sets no timeout and no maxRetries, so each model call gets the OpenAI client's 10-minute timeout and LangChain's 6 retries with backoff, and the route exports no maxDuration. The platform's function limit is shorter, so a slow or rate-limited call ends as a 504 after tokens were already billed. Already reported: open since 2024-10: 504 FUNCTION_INVOCATION_TIMEOUT on every chat, several +1s, no maintainer reply; reported on the Mixtral/Pinecone version. https://github.com/Nutlope/pdftochat/issues/27

Other problems found (check each against the code first; not scored):
- Source page buttons get no page number with Chroma, the default store (app/api/utils/vector_store/chroma.ts:44): sanitizeMetadata turns every nested object into a JSON string, so the chunk's loc ({pageNumber, lines}) is stored as the string '{"pageNumber":3,...}'. The chat client reads metadata['loc.pageNumber'] ?? metadata.loc?.pageNumber (app/document/[id]/document-client.tsx:92); on a string both are undefined, so all four sources collapse into one button reading 'p. ' and clicking it jumps to page NaN. Change: Flatten loc at ingest (store loc.pageNumber as its own number key, as the Pinecone and Mongo stores did) or JSON.parse metadata.loc when it is a string in extractSourcePageNumber.
- Pinecone and MongoDB chats cannot retrieve: no embeddings passed, and the embedding model is gone (app/api/utils/vector_store/index.ts:46) [LP-GE-13]: The chat route calls loadRetriever without embeddings (app/api/chat/route.ts:55), and index.ts:46 passes that undefined value on behind a non-null assertion, so retrieval on Pinecone or MongoDB throws on embedQuery. Ingest for those stores uses togethercomputer/m2-bert-80M-8k-retrieval (embeddings/index.ts:6), removed from Together serverless on 2025-06-13; Together's models page says no embedding models are offered via serverless. index.ts:14 and :36 still default to 'pinecone' when NEXT_PUBLIC_VECTORSTORE is unset, and .env.example still offers both. Change: Either remove the Pinecone and MongoDB options (and make chroma the default when the variable is unset), or pass an embeddings model that exists from the chat route and switch ingest to it.

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).

82static
E, prefill reuse
0.74
S, prefix stability
1.00
G, generation waste
0.05
Default shares
0 of 2

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 81.

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.