Skip to content
Groww
Medium

LLM cost per query is too high for millions of users. What do you do?

Go deeper with the courseAI Product Management CourseBuild RAG apps, AI evals and agents, then crack AI PM interviews. Rated 4.9

Community Answers (2)

a
ankur helak

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

C
Curious Coder

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.