Urgent.News

What's breaking now, across thousands of outlets.

AI

The Semantic Cache That Made a Free LLM Quota Feel Infinite

A token allowance is usually treated as a spending budget, which is the wrong mental model for free tiers. The right model is a cache to be managed, because agent workloads repeat themselves far more than developers realize. A semantic cache that serves previous responses for rephrased requests can cut token consumption by roughly half in typical agent loops. This article documents a working…

Token allowances for free language model tiers are often viewed as a spending budget, but this approach misses a crucial optimization opportunity. Rather than treating the allowance as something to be depleted, it's more effective to think of it as a cache to be managed efficiently. A semantic cache that serves previously generated responses for rephrased queries can reduce token consumption by up to half in typical agent workflows.

This article explains how to implement a zero-dependency semantic cache and fine-tune its parameters for safe production use. MonkeyCode's free offering currently provides a 10-million-token allowance and a free server option, making effective quota management a practical concern. The percentages and latency figures presented below are illustrative measurements from a controlled prototype, not guarantees of specific outcomes.

In most agent workflows, developers assume prompts are mostly unique, leading to unnecessary token consumption and latency. A closer look at request logs reveals that about 35% of prompts are semantically near-duplicates of previous answers. Sending duplicate prompts incurs unnecessary token costs, increased latency, and the risk of inconsistent responses when the model reformulates answers differently.

Instead of expecting the model to become smarter, the solution lies in preventing duplicate requests from reaching it in the first place. A semantic cache placed in front of the endpoint can recognize and return cached responses for similar prompts, reducing the number of calls to the model. The cache does not replace the model; it simply filters out redundant requests.

To implement a semantic cache, the first step is selecting an appropriate similarity metric and threshold. A zero-dependency option is character n-gram Jaccard similarity, which tokenizes text into three-character overlapping shingles and compares their sets. While more advanced methods like embedding models exist, they introduce additional dependencies and API costs.

A threshold of 0.92 was chosen through calibration of one hundred logged prompts, balancing the need to catch duplicates versus the risk of returning incorrect cached responses. The choice of similarity metric depends on prompt length, with character n-grams suitable for short prompts and word-based methods or embeddings potentially better for longer text.

After a response is retrieved from the cache, it should be stored with a time-aware eviction policy to prevent memory leaks. The prototype uses SQLite with a created_at timestamp and a TTL (time-to-live) of one hour, striking a balance between freshness and hit rate. Shorter TTLs protect against stale answers, while longer TTLs improve hit rates but risk serving outdated information.

The schema stores the original prompt, response text, token cost of the original call, creation time, and hit counter. The hit counter enables a popularity-aware eviction strategy, where the cache removes the oldest entries with the lowest hit counts when it exceeds a size limit. This strategy focuses on prompts that actually recur, rather than one-off requests.

The provided Python code demonstrates a complete cache implementation, including initialization, tokenization, similarity scoring, retrieval, and insertion. It is designed to be dependency-free, allowing it to run on any fresh Python environment, including free servers without package installation constraints.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in AI

How I Built Smart Scraper M2M: A Fast ~30ms Scraper API for AI Agents

Building AI Agents with frameworks like CrewAI or LangChain often hits a bottleneck: heavy, slow web scraping that bloats context windows and increases LLM token costs.

  • Smart Scraper M2M returns clean JSON data in 30ms
  • Optimizes context by removing unnecessary HTML/CSS
  • Designed for integration with CrewAI, LangChain, and Node.js agents

More from Sunday 23 August →