Skip to main content

Lite library for local caching of LLM API requests

Project description

llm-cache-lite

PyPI version Python 3.8+ License: MIT

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/await functions
  • Zero Boilerplate: Just add the @llm_cache decorator. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_cache_lite-0.3.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_cache_lite-0.3.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

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

Hashes for llm_cache_lite-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d892e077345e369c5ccc0fec2ef61bcd57979092255ece244f61ebc2188138e9
MD5 baf13d5f83793c4b7c1af0316d6016d4
BLAKE2b-256 1a30c0c17bc593637e5509b0d2de171beae955e47636298413c7e4e178e650d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_cache_lite-0.3.0.tar.gz:

Publisher: publish.yml on andi2345/llm-cache-lite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for llm_cache_lite-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 037dc5520721c86852b63e67f256cec3e703d35d56d5cf10cb09f02dfc1bcc2e
MD5 53ed5bc7002b0dc5837b3a1ca964ef9a
BLAKE2b-256 665fc2c223cfe807a040aea0ff2cd68ea9434ae1e44157ca4a34d4151811277b

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page