Groww
Medium
LLM cost per query is too high for millions of users. What do you do?
This is a unit economics question. Not a "switch to a cheaper model" question. Find out which queries never needed a big model, then cut cost stage by stage without losing answer quality.
Step 1. Clarifying Questions
- Which feature? Assume Groww's in-app assistant that answers fund, stock and portfolio questions.
- How high is too high? Assume ₹1.5 per query and 10 lakh queries a day, so ₹15 lakh a day (illustrative).
- Is quality acceptable today? Assume yes. The goal is lower cost at the same quality.
- Do users pay for it? Assume it is free.
Why it matters: cost grows with every query, revenue does not. And in investing you cannot buy savings with worse answers. One wrong NAV or tax number costs more trust than the money saved.
Step 2. Break the cost into an equation
Cost per day = Queries × LLM calls per query × (input tokens × input price + output tokens × output price)
Every term is a lever. Now trace one request: query arrives → context assembled (system prompt, portfolio, documents, chat history) → model call → response.
Step 3. Where the money leaks
- Many queries are repeats: "What is NAV?", "How do I start a SIP?"
- Many are deterministic: "What is my portfolio value?" is an API call, not reasoning.
- Context bloat: full chat history and 10 document chunks sent on every turn.
- One large model for everything, including "thanks".
- Agent loops and retries that make 3 calls for one answer.
Step 4. Solution per stage
1. Before the model: an intent router. Portfolio value, SIP dates and order status go to APIs and templates. Common FAQs go to a semantic cache.
2. Context: summarise old turns, send top 3 chunks instead of 10, cache the static system prompt.
3. Model: small model for simple explanations, large model only for comparisons and analysis.
4. Output: cap length. A short answer with a "show more" is cheaper and easier to read on a phone.
Rough maths (illustrative): 40% of queries go to APIs or cache, 40% to a small model at one tenth of the cost, 20% stay on the large model. New cost ≈ 0.4 × 0.1 + 0.2 = 0.24 of today. About 75% saved.
Cross question: what if the router sends a risky query to the small model? Optimise the router for recall on complex and advice-like intents ("Should I switch funds?"). Sending an easy query to the large model wastes a rupee. Sending a hard one to the small model can cost a customer.
Step 5. Prove quality held
Run the golden dataset on the new setup before rollout, then shadow traffic, then 5% of users. Quality must match the old setup on every intent.
Step 6. Metrics
- North Star: cost per resolved query, not cost per query. A cheap answer that leads to a re-ask costs twice.
- Guardrails: accuracy on the golden set, re-ask rate, p95 latency, thumbs-down rate.
Interview tip: don't open with "use a smaller model". Open with "which queries didn't need an LLM at all".
Community Answers (2)
1. Define the business problem and success criteria: - What product are we building? Let's say a Coding Agent. - What are our current economics? Cost per query, average number of queries per user and Revenue per user. Let's say 100M queries per month, average cost per query $ 0.02, target reduction of 30%. 2. Diagnosing the cost driver: Cost = Input tokens * Cin + Output tokens * Cout + Cmiscellaneous Miscellaneous cost includes any retrieval cost, re-ranking cost, etc. Then I would drill down the cost into customer segments, use cases and choice of model. This should give us a good idea on where exactly the cost is going. Let us say 40% of use cases are simple but use a reasoning model. (simple code generation, one file bug fixes, code refactoring) 25% of the cost comes from excessive input and output token generation 15% cost comes from repeated similar queries rest comes for complex and multi turn use cases. Solutions: 1. Model Routing: Use a simple complexity classifier model trained on internal company data/anonymysed user data. Tradeoff: wrong routing reduces quality 2. Token optimization: Shorter prompt or Prompt rewrite, Relevant context retrieval, regex based retrieval for exact queries. I would also suggest an auto-cut on the similarity score-gap detection, if the gap in similarity score drop significantly then only take the chunks before that for input context. 3. Caching: Use KV caching on frequently used queries. tradeoff is staleness and lack of personalization. I would start with an offline evaluation dataset, then a small production A/B test, and gradually expand traffic. I would optimize for Cost per successful task while maintaining the guardrails. Guardrails: 1. Quality guardrails: Task success rate, factual accuracy, hallucination rate 2. Experience guardrails: P95 latency, user satisfaction, retry rate 3. Business guardrails: Retention, paid conversion, gross margin
Clarifying Question: 1. Are we looking to contain optimize for the overall LLM spend and not cost per query - Yes Assumption: Since we don't know the exact kind of system where these queries are getting fired, hence below are some of the general ways of reducing the LLM spend: 1. Prompt Caching: In case same or group of similar prompts are getting hit again & again, we can deploy prompt caching, which is available from most of the LLM vendors at relatively cheaper price points 2. We shall even re-evaluate if we are using the LLMs in the right call and it is not possible to use any rule, regex pattern or off the shelf ML model to solve this problem 3. The third part could be opting for an open source or less costly model, without even letting go the quality of the actual output We can also cap the number of times the flow can be used by a user.