Swaran Soft
AI Strategy

Optimising Generative AI Spend: 7 Ways to Control LLM Costs

November 29, 20266 min readBy Yogesh Huja, Founder & CEO
Optimising Generative AI Spend: 7 Ways to Control LLM Costs

Initial tests for generative AI are frequently perceived as relatively low cost. With a couple of hundred users and a couple of thousand queries a month, even the largest API bills are easily covered by a company’s corporate card. But then the test is successful and the AI assistant is deployed to 10,000 staff, together with a front facing chatbot and an agent to process invoices. Within a quarter the monthly bill has risen markedly and the finance department is keen to understand why.

This situation is typical. As mentioned, generative AI cost increases with use, and this escalation is often under-recognised. Each query, the documents analysed for context and indeed each action undertaken by an agent, and even re-tries, all add to token cost.

Optimising generative AI is about managing generative AI so that cost does not rise as fast as use – and without compromising on quality. In this article I explain cost drivers and highlight key optimisation methods.

Costs of using LLMs

Tokens are charged for both the input (for example, user queries and prompts, plus a chat history and documents analysed for context) and output (the model response). More advanced models incur significantly greater token costs than less sophisticated ones.

Typically within an enterprise, input token costs predominate. Even for a brief query there may be lengthy prompts, ten sections of documents and ten chat exchanges in the input. And in agentic systems, this can be further increased due to multiple model calls being made to complete a task, each with its own context.

For self-hosted models, cost centres switch to include the infrastructure (GPUs, servers etc.) and the expertise to manage it. Costs are more consistent here. However, infrastructure must still be utilised effectively to ensure value for money is realised.

1. Employ the most suitable model

Whilst many groups deploy their most advanced model, in reality most enterprise use cases are fairly basic in nature for example categorising support tickets, analysing invoice data, answering basic policy related queries and creating a summary for a brief email. Smaller models can easily and at greatly reduced cost manage these use cases.

Automated ‘model switching’ enables different models to be directed to different use cases. An initial, light-weight classification or rule-based system analyses incoming use cases and then routes them to either a small or large model depending on whether they are basic or more complex. Done effectively, this can lead to significant cost reductions and with minimal (if any) impact on quality. Again, validation using real world examples is recommended.

2. Simplify prompts and context

As additional functionality is introduced to prompts, they often become increasingly lengthy. Periodically analyse and eliminate unnecessary elements. As well as reducing costs, simplified prompts can also enhance performance.

Analysis of context represents another main area for improvement. Often ten lengthy document sections are submitted for analysis although in fact just two brief sections may hold sufficient information. Improved search, re-ranking and document sectioning will therefore both decrease the volume and increase the precision of context. Furthermore, a summary of a chat history can be provided rather than submitting the whole history at each exchange.

3. Leverage re-use

Within an enterprise, there is often considerable re-use of queries. Staff will, for example, commonly seek clarification around annual leave and expense policies. Responses to popular or near-identical queries can be cached so that future queries can benefit from this without needing to invoke the model.

Many API services also provide cost savings for re-occurrences of a prompt prefix (for instance an extended prompt). Organisation of prompts so that the static element is presented initially will exploit this.

4. Process non-critical work in batches

Whilst immediate responses are not always essential, functions such as analysing historic support tickets, categorising customer feedback and analysing old contracts can all be batch processed. Moreover, several service providers provide cost benefits for batch processing. Self-hosted models can also batch process work during off-peak periods thus avoiding periods where GPUs are under-utilised.

5. Manage output volume

Typically output token costs exceed those for inputs. Where applicable, request succinct outputs. Also impose maximum output volumes and utilise structured output (for example JSON) for analysis related use cases. Within agentic systems, ensure that the model only delivers output relevant to subsequent stages.

6. Possibly self-host models

Whilst for low volumes, APIs are typically both more cost effective and easier to implement, at higher volumes self-hosting open source models on in-house GPUs or within a private cloud can often prove more economic. Unlike APIs, infrastructure costs for self-hosting are largely fixed irrespective of token volumes processed.

API hosted modelsSelf-hosted open source models
CostVariable (token driven)Mostly fixed (infrastructure driven)
Optimal forLow/unpredictable volumes and high-level reasoningHigh and stable volumes and predictable workloads
Data storageOff-siteOn-site
ManagementLow levelModerate – requires both GPU and MLOps expertise
AdaptabilityAccess to the latest cutting edge modelsComplete control over model selection and adjustment

Self-hosting is also beneficial for data security. Indian enterprises must comply with the DPDP Act in relation to data privacy. By self-hosting or hosting within an Indian private cloud, data remains in-house. Innovations such as Ollama, along with effective serving and quantised models (which require less memory), have vastly improved the feasibility of self-hosting in recent years.

Generally, the ideal solution is to self-host models for high volume and/or data security purposes and access API hosted cutting edge models for the small element of work which does require them.

7. Measure cost as you would other performance indicators

Without measurement there can be no optimisation. Analyse the number of tokens and associated cost by application, team, feature and model. Establish cost and accuracy objectives (for instance cost per employee query resolved and/or per invoice processed). This allows comparison and understanding of trade-offs. For example, if switching to a more advanced model leads to only a marginal improvement in accuracy but costs three times as much per query, businesses can then consciously decide on the optimal option – rather than discovering this via an invoice!

After the initial month of live deployment, re-analyse this information. Actual usage is seldom as initially anticipated.

Going forward

Analyse LLM usage from last month and sub-divide this by application and model. Then ascertain the three most costly workloads and assess whether each is genuinely requiring the model currently being applied. Usually, this stage highlights initial areas for cost saving.

Want to talk it through with us? We run a free 45-minute AI Pilot Assessment. Drop a line to info@swaransoft.com, or ring +91 9220-313-650 (India) | +971-50-9292-650 (UAE). You'll find more at www.swaransoft.com

Common queries

Free 45-minute AI Pilot Assessment

Share this article:
Yogesh Huja — Founder & CEO, Swaran Soft
Yogesh HujaFounder & CEO

AI Architect and Entrepreneur building India's Edge AI ecosystem. 25+ years in enterprise technology. Founder of Swaran Soft, Gignaati, and Copilots.in.

Published: ⏱ 6 min read