Cache & Distil: Optimising API Calls to Large Language Models

Guillem Ramírez, Matthias Lindemann, Alexandra Birch, Ivan Titov

Abstract

Large-scale deployment of generative AI tools often depends on costly API calls to a Large Language Model (LLM) to fulfil user queries, a process that also exposes the request stream to external providers. To curtail the frequency of these calls, one can employ a local smaller language model—a student—which is continuously trained on the responses of the LLM. This student gradually gains proficiency in independently handling an increasing number of user requests, a process we term neural caching. The crucial element in neural caching is a policy that decides which requests should be processed by the student alone and which should be redirected to the LLM, subsequently aiding the student’s learning. In this study, we focus on classification tasks, and we consider a range of classic Active Learning-based selection criteria as the policy. Our experiments suggest that Margin Sampling and Query by Committee bring consistent benefits over other policies and baselines across tasks and budgets.

Introduction

Deploying Large Language Models (LLMs) at scale presents significant challenges, primarily centered around exorbitant operational costs and data privacy risks. Most organizations rely on commercial APIs (e.g., OpenAI), requiring every user query to be transmitted to an external provider. While traditional static caches can mitigate costs for identical queries, they fail to generalize. This paper introduces "Neural Caching," a system designed to generalize from past API responses to handle new, similar queries locally.

The Neural Caching Framework

The proposed framework utilizes a "student-teacher" architecture to minimize external API dependency. The system comprises three main components:

  1. The Teacher: A high-performing LLM (e.g., GPT-3.5) accessed via API.
  2. The Student: A smaller, local model (e.g., T5-base with LoRA adapters) that is continuously distilled from the teacher’s responses.
  3. The Policy Algorithm: A decision-making component that determines whether the student is sufficiently confident to answer a query or if the teacher must be queried.
The Neural Caching architecture: A student model acts as a local cache, with a policy algorithm deciding when to query the teacher LLM.

The core innovation lies in treating the decision to call the API as an Active Learning (AL) problem. Instead of picking queries at random, the researchers evaluated several classic AL selection criteria:

  • Margin Sampling (MS): Targets queries where the student's top two predicted labels have a small probability gap (high uncertainty).
  • Query by Committee (QBC): Uses a "committee" of student versions; if they disagree on an answer, the teacher is called.
  • Prediction Entropy: Measures the overall uncertainty of the student's probability distribution.
  • Coreset: An embedding-based approach that picks queries most "different" from what the student has seen before.

Experimental Setup

The framework was evaluated across four classification benchmarks: ISEAR (emotion), RT-Polarity (sentiment), FEVER (fact-checking), and Openbook (science QA). Notably, the authors released a new benchmark of LLM-generated "soft labels" (log-probabilities) to facilitate further research into online distillation and neural caching.

Performance and Policy Comparison

The results demonstrate that uncertainty-based logic is significantly more effective than similarity-based logic for neural caching.

Average online accuracy across all datasets for different selection policies. Margin Sampling and Query by Committee consistently outperform other methods.

Key findings include:

  • Superior Efficiency: Margin Sampling (MS) and Query by Committee (QBC) consistently outperformed all baselines, achieving higher "online accuracy" (the average accuracy a user experiences over the system's lifetime) for the same budget.
  • Generalization: Unlike standard caches, the student model learns underlying patterns, becoming increasingly useful for novel queries over time.
  • Failure of Embedding-Based Caching: Interestingly, the "Coreset" method—which mirrors many current industry "semantic caches" based on vector similarity—performed poorly. This suggests that uncertainty-based logic is far more effective for deciding when to call an LLM.
  • Robustness: The student model proved surprisingly resilient, effectively "smoothing out" some of the teacher's noise during the distillation process.

Limitations and Future Work

While effective for classification, extending neural caching to free-text generation remains a challenge due to the difficulty of measuring uncertainty in long-form text. Additionally, real-world deployment requires tuning confidence thresholds and addressing "concept drift" in non-stationary request streams. Future research will explore more complex student models and adaptation to shifting user trends.

Summary updated

BibTeX
@inproceedings{ramirez2024cache,
  author = {Guillem Ramírez and Matthias Lindemann and Alexandra Birch and Ivan Titov},
  title = {{Cache \& Distil: Optimising API Calls to Large Language Models}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=m1S755d7rc},
}
Figure