Lite library for local caching of LLM API requests
Project description
llm-cache-lite
A lightweight, decorator-based caching library for LLM (Large Language Model) API calls. Save money, reduce API latency, and speed up your AI development cycle instantly.
Why use llm-cache-lite?
When developing AI applications, you often send the same prompts over and over again. This wastes API credits and slows down your testing. llm-cache-lite solves this by intercepting your AI functions and caching the responses in a local SQLite database.
- Universal: Works seamlessly with OpenAI, Anthropic, Gemini, local models, or any custom function.
- Async Ready: Native support for
async/awaitfunctions - Zero Boilerplate: Just add the
@llm_cachedecorator. No complex clients to initialize. - Smart TTL: Automatically expire and invalidate old cached responses.
- Developer Friendly: Type-hinted and strictly validated with Pydantic.
Installation
pip install llm-cache-lite
Quick Start
Standard (Sync) Usage
import time
from llm_cache_lite import llm_cache, CacheSettings
# Optional: Configure database path and TTL (Time-To-Live in seconds)
# Here, the cache will expire after 1 hour (3600 seconds)
settings = CacheSettings(db_path="my_ai_cache.db", ttl_seconds=3600)
@llm_cache(settings=settings)
def ask_ai(prompt: str) -> str:
# Simulate an expensive API call to OpenAI/Claude/Gemini etc.
print(f"Calling real AI for: {prompt}")
time.sleep(2)
return f"AI response to: {prompt}"
# First call: Executes the function (takes 35 seconds)
print(ask_ai("What is Python?"))
# Second call: Returns instantly from the local SQLite cache! (~0.001 seconds)
print(ask_ai("What is Python"))
Asynchronous (Async) Usage
import asyncio
from llm_cache_lite import llm_cache, CacheSettings
# Optional: Configure database path and TTL (Time-To-Live in seconds)
# Here, the cache will expire after 1 hour (3600 seconds)
settings = CacheSettings(db_path="my_ai_cache.db", ttl_seconds=3600)
@llm_cache(settings=settings)
async def async_ask_ai(prompt: str) -> str:
# Simulate an expensive API call to OpenAI/Claude/Gemini etc.
print(f"Calling real AI for: {prompt}")
await asyncio.sleep(2)
return f"Async AI response to: {prompt}"
async def main():
# First call: Executes the function (takes 35 seconds)
print(await async_ask_ai("What is the speed of sound?"))
# Second call: Returns instantly from the local SQLite cache! (~0.001 seconds)
print(await async_ask_ai("What is the speed of sound?"))
asyncio.run(main())
Configuration
The CacheSettings object uses Pydantic to ensure your configuration is always valid. It accepts the following parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
db_path |
str |
"llm_cache.db" |
The path to the SQLite database file. |
ttl_seconds |
int |
0 |
Time-To-Live in seconds. 0 means the cache never expires. |
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_cache_lite-0.3.0.tar.gz.
File metadata
- Download URL: llm_cache_lite-0.3.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d892e077345e369c5ccc0fec2ef61bcd57979092255ece244f61ebc2188138e9
|
|
| MD5 |
baf13d5f83793c4b7c1af0316d6016d4
|
|
| BLAKE2b-256 |
1a30c0c17bc593637e5509b0d2de171beae955e47636298413c7e4e178e650d5
|
Provenance
The following attestation bundles were made for llm_cache_lite-0.3.0.tar.gz:
Publisher:
publish.yml on andi2345/llm-cache-lite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_cache_lite-0.3.0.tar.gz -
Subject digest:
d892e077345e369c5ccc0fec2ef61bcd57979092255ece244f61ebc2188138e9 - Sigstore transparency entry: 1235286351
- Sigstore integration time:
-
Permalink:
andi2345/llm-cache-lite@27da340e7ce19d1be0c3558e6f06807bdbe50945 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/andi2345
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@27da340e7ce19d1be0c3558e6f06807bdbe50945 -
Trigger Event:
push
-
Statement type:
File details
Details for the file llm_cache_lite-0.3.0-py3-none-any.whl.
File metadata
- Download URL: llm_cache_lite-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
037dc5520721c86852b63e67f256cec3e703d35d56d5cf10cb09f02dfc1bcc2e
|
|
| MD5 |
53ed5bc7002b0dc5837b3a1ca964ef9a
|
|
| BLAKE2b-256 |
665fc2c223cfe807a040aea0ff2cd68ea9434ae1e44157ca4a34d4151811277b
|
Provenance
The following attestation bundles were made for llm_cache_lite-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on andi2345/llm-cache-lite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_cache_lite-0.3.0-py3-none-any.whl -
Subject digest:
037dc5520721c86852b63e67f256cec3e703d35d56d5cf10cb09f02dfc1bcc2e - Sigstore transparency entry: 1235286391
- Sigstore integration time:
-
Permalink:
andi2345/llm-cache-lite@27da340e7ce19d1be0c3558e6f06807bdbe50945 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/andi2345
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@27da340e7ce19d1be0c3558e6f06807bdbe50945 -
Trigger Event:
push
-
Statement type: