fix: optimize LLM inference β latency, reliability, cost & throughput#3664
Open
badhra-ajaz wants to merge 1 commit intoQuivrHQ:mainfrom
Open
fix: optimize LLM inference β latency, reliability, cost & throughput#3664badhra-ajaz wants to merge 1 commit intoQuivrHQ:mainfrom
badhra-ajaz wants to merge 1 commit intoQuivrHQ:mainfrom
Conversation
β¦ghput Identified 14 LLM inference points across the RAG pipeline using PeakInfer analysis. Applied 12 fixes across 4 categories: Critical: Add timeout (30s) + retry (3x) to all 7 LLM providers, convert sync routing to async, add return_exceptions to asyncio.gather calls. Warnings: Convert sync answer/generate methods to async ainvoke, fail-fast on missing API keys instead of silent warnings. Opportunities: Use gpt-4o-mini for routing/rephrasing (96% cheaper), add tenacity retry with exponential backoff, add LRU cache for structured output. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes 12 LLM inference issues across 4 files in the Quivr RAG pipeline, targeting latency, reliability, cost, and throughput improvements.
All issues were automatically identified using PeakInfer β an AI-powered inference analysis skill for Claude Code that scans codebases for LLM-specific performance anti-patterns.
How These Issues Were Found
We ran the
/peakinfer-analyzeskill inside Claude Code, which:PeakInfer Analysis Report Snapshot
InferenceMAX Benchmark Comparison
This benchmark data directly informed the model downgrade recommendation β gpt-4o-mini is 33x cheaper on input and 25x cheaper on output for routing/rephrasing tasks that don't need full gpt-4o capability.
Changes
π΄ Critical Fixes (4)
llm_endpoint.py:250timeout=Noneβ requests hang indefinitelytimeout=30.0+max_retries=3llm_endpoint.py:232-314timeout=30+max_retries=3to Azure, OpenAI, Mistral, Gemini, Groq, fallbackquivr_rag_langgraph.py:337.invoke()in async pipeline β blocks event loop, +200% latencyrouting()toasyncwithawait .ainvoke()quivr_rag_langgraph.py:509,571asyncio.gatherwithoutreturn_exceptionsβ one failure kills all parallel tasksreturn_exceptions=True+ per-task error handlingπ‘ Warning Fixes (5)
quivr_rag.py:161answer()blocks while user waits for full responseasync def answer()withawait .ainvoke()quivr_rag_langgraph.py:923generate_zendesk_rag()blocks event loopasyncwithawait .ainvoke()quivr_rag_langgraph.py:956generate_rag()blocks event loopasyncwithawait .ainvoke()quivr_rag_langgraph.py:968generate_chat_llm()blocks event loopasyncwithawait .ainvoke()config.py:362raise ValueError()for fail-fast behaviorπ΅ Opportunity Fixes (3)
quivr_rag_langgraph.pylightweight_llmproperty using gpt-4o-mini ($0.15/$0.60 per 1M) β 96% cheaperquivr_rag_langgraph.py@retrydecorator (tenacity) with exponential backoff for transient API errorsquivr_rag_langgraph.pyEstimated Impact
Files Changed
core/quivr_core/llm/llm_endpoint.pyβ timeout + retry on all 7 LLM provider branchescore/quivr_core/rag/quivr_rag.pyβ async answer methodcore/quivr_core/rag/quivr_rag_langgraph.pyβ async methods, lightweight LLM, retry, caching, resilient gathercore/quivr_core/rag/entities/config.pyβ fail-fast API key validationTest plan
ValueErrorimmediatelyreturn_exceptions=Truehandles individual task failures gracefullyπ€ Generated with Claude Code