Source page links lost their page numbers when the app moved to Chroma

A compact Together AI and LangChain demo for chatting with PDFs, with hybrid dense and sparse search in Chroma Cloud.
≈ 360,000 tokens per 1,000 calls, recomputed because the retrieved text comes before the chat history estimate
Audited with leanprompt 0.1.0-dev.15
Estimates, not measurements. Every figure on this page is an estimate from reading the code, not a measurement of the app in use.
- Repository
- Nutlope/pdftochat
- Commit audited
94eb281- 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.
A Next.js demo for chatting with PDFs on Together AI, LangChain.js and Chroma Cloud. Since the move to Chroma the source page buttons get no page number, and the retrieved text sits before the chat history, so no follow-up can reuse the cached conversation. Figures are estimates from the code.
leanprompt read the code at commit 94eb281 and found 2 places where its LLM calls could use fewer tokens, 1 the project already knows about, plus 2 other problems, not scored. Static score: 82 of 100 (an estimate from the findings, leaving out what the project already knows about). The code may have changed since.
Findings
-
LP-PC-02Stable content first, without hurting meaningutils/ragChain.ts:28· severity high · saves prefillWhat happens. 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.
The 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 helps. 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)
Assumptions. 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.
-
LP-GE-03Output and thinking budgets that fit the taskapp/api/chat/route.ts:42· severity low · saves generation · how often is assumedWhat happens. 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.
The 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 helps. Without a limit, a runaway answer or an answer written into the rephrase step is billed in full and thrown away.
Estimate about 11k output tokens per 1,000 calls (estimate)
Assumptions. 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 often. the longest answers and rephrase outputs in Together's request logs for this key, and whether any answer ran until the function was stopped
Already known to the project
The project’s own issues, pull requests or docs already cover these, so they are not counted above and are left out of the static score.
-
LP-GE-04Stream long outputs; timeouts that match generation timeapp/api/chat/route.ts:42· severity medium · saves prefill and generation · how often is assumedWhat happens. 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.
The 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 helps. Retries and generation that the platform kills are billed and give the user nothing.
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)
Assumptions. 1% of conversations hit a timeout after the rephrase call and part of an answer.
How often. how many /api/chat invocations end with FUNCTION_INVOCATION_TIMEOUT in the Vercel logs
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.
-
Source page buttons get no page number with Chroma, the default store
app/api/utils/vector_store/chroma.ts:44What happens. 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.
The 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.
To check. 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
app/api/utils/vector_store/index.ts:46·LP-GE-13Default to a model the provider still serves · severity mediumWhat happens. 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.
The 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.
To check. With NEXT_PUBLIC_VECTORSTORE unset, a fresh deploy uploads and chats without an error, or fails at start with a message naming the variable.
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.