Skip to main content
Python functools caching speeds LLM API calls, showing code and a graph of performance improvement.

Editorial illustration for Python functools In‑Memory Caching Speeds Expensive LLM API Calls

Speed Up LLM API Calls with Python Functools Caching

Python functools In‑Memory Caching Speeds Expensive LLM API Calls

Updated: 3 min read

Calling an LLM API is expensive, not just in dollars, but in latency. Every redundant request with the same prompt wastes time and money. Python’s `functools` library hands you a near-instant fix: the `@lru_cache` decorator.

Apply it to your function, and identical inputs fetch results from memory instead of hitting the network. That’s in-memory caching, elegant and immediate. But what happens when your script stops?

Or when the API itself fails? Two more decorators extend the optimization: `diskcache` for persistent storage across sessions, and `tenacity`’s `@retry` for network resilience. Together, they turn a brittle LLM call into a robust, fast, and cost-effective operation.

In-memory Caching This solution comes from Python's functools standard library, and it is useful for expensive functions like those using LLMs. If we had an LLM API call in the function defined below, wrapping it in an LRU (Least Recently Used) decorator adds a cache mechanism that prevents redundant requests containing identical inputs (prompts) in the same execution or session. This is an elegant way to optimize latency issues.

Caching On Persistent Disk Speaking of caching, the external library diskcache takes it a step further by implementing a persistent cache on disk, namely via a SQLite database: very useful for storing results of time-consuming functions such as LLM API calls. This way, results can be quickly retrieved in later calls when needed. Consider using this decorator pattern when in-memory caching is not sufficient because the execution of a script or application may stop.

Network-resilient Apps Since LLMs may often fail due to transient errors as well as timeouts and "502 Bad Gateway" responses on the Internet, using a network resilience library like tenacity along with the @retry decorator can help intercept these common network failures. The example below illustrates this implementation of resilient behavior by randomly simulating a 70% chance of network error.

These three patterns, in-memory caching, persistent disk storage, and resilient retries, form a pragmatic stack for any serious LLM application. Caching slashes latency on repeated prompts, turning minutes into milliseconds. Diskcache ensures those savings survive a restart.

And tenacity makes your pipeline shrug off transient errors. Individually, each is a small decorator. Together, they transform fragile, expensive API calls into a fast, durable, fault-tolerant system.

The elegance isn’t in any single trick, it’s in how these layers compose, letting you focus on logic while Python handles the messiness of network reality.

Common Questions Answered

How does Python's functools LRU cache help reduce costs when calling LLM APIs?

The LRU (Least Recently Used) cache decorator prevents redundant API calls by storing recent results for identical input prompts. By wrapping an LLM API call with @lru_cache, developers can automatically cache and retrieve previous responses, reducing both latency and unnecessary API expenses during repeated function invocations.

What specific problem does in-memory caching solve for developers working with large language model APIs?

In-memory caching addresses the issue of repeated API calls within loops, retries, or user-driven workflows where the same prompt might be processed multiple times. The functools LRU cache mechanism ensures that identical inputs retrieve cached results instead of making redundant and costly API requests, optimizing both performance and resource utilization.

Why is the @lru_cache decorator considered an elegant solution for LLM API call optimization?

The @lru_cache decorator provides a lightweight, built-in Python mechanism for automatically managing function call results without complex custom caching logic. It seamlessly stores recent function outputs and serves cached results for identical inputs, making it a simple yet powerful tool for reducing latency and controlling API call expenses.

LIVE03:21OpenAI's Miles Wang in Talks for USD 2B AI Drug Discovery Startup