DEV Community

ZhiHong Chua
ZhiHong Chua

Posted on

Stop Burning Cash: Real-World Lessons in LLM Token Optimisation

There is immense hype around AI, but the real work is in making it efficient and cost-effective. This week, I had a great conversation with a friend about integrating AI into their family business, and the first barrier we hit was ensuring that deployment costs wouldn't swallow their budget.

My recent experiments in optimising Large Language Model (LLM) calls revealed critical lessons on balancing performance with operational expenditure. Here are my key findings on drastically reducing token usage and navigating API vendor choices.

1. Cost Efficiency: Cutting Token Consumption by 87%

Initial Cost

Final Cost

The goal of implementing AI tools is to harness their ability to scale content production while maintaining a lower operational cost approach. In one workflow, I managed to reduce the average token usage from 4,000 tokens per request down to approximately 500. This efficiency demonstrates the power of integrating human creativity and strategic thinking with AI.

Here is the strategic breakdown of how this massive reduction was achieved:

  1. Isolation of Deterministic Logic: The initial cut involved isolating any logic that could be handled purely by the backend. This step ensures that the LLM is only used for tasks requiring true natural language understanding or generation, reducing the prompt size significantly.
  2. Reliance on Model Training Data: The second optimisation involved relying more heavily on the AI model's existing training data. Instead of providing extensive examples or foundational knowledge (which consumes tokens), the process focused on maintaining local context relevant to the current conversation rather than providing unnecessarily global context.
  3. Implementing Intent Classification: Where minimal context was available, it became necessary to implement intent classification. This addition allows the system to accurately direct the query, even when the context provided to the model is small, thus preventing ambiguous results or expensive, irrelevant generations.

2. Strategic API Choice: Hugging Face vs. OpenAI

Here is a practical comparison based on testing:

Feature Hugging Face Inference API OpenAI API (e.g., GPT models)
Cost Model Offers a free tier (up to 10 cents, periodically resetting, approximately once a week) Generally requires payment; lacks a substantial free trial
Best Use Case Excellent for small-scale testing and validating concepts due to the minimal initial cost Best for maintaining large context efficiently in chat sessions
Context Handling Requires the initial prompt (the system message/context) to be sent with each request (which becomes costly if your context is large) The initial large prompt can typically be sent once only at the start of the chat session, improving efficiency and reducing recurring token expenditure

3. Looking Forward: The Move to Agentic AI

While some may categorize this optimization work strictly within the realm of generative AI, the course I completed with Vanderbilt University suggests that the field is rapidly moving toward agentic AI.

My objective is to push my knowledge deeper into agentic AI soon, focusing on building systems that can actively plan and execute tasks to deliver higher-order value!

Top comments (0)