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.
  • 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

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"))

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.2.2.tar.gz (8.1 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.2.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file llm_cache_lite-0.2.2.tar.gz.

File metadata

  • Download URL: llm_cache_lite-0.2.2.tar.gz
  • Upload date:
  • Size: 8.1 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.2.2.tar.gz
Algorithm Hash digest
SHA256 13e57e3a220f48ff7c0b8110f41e4530325673a4553b99c8906a978fa9203da9
MD5 98849ef15415826d71d4cfbd07fc6828
BLAKE2b-256 68da22f48e23665b5051b694b85bd0e3cfae220f9340ce53e606f3b63d30a7b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_cache_lite-0.2.2.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.2.2-py3-none-any.whl.

File metadata

  • Download URL: llm_cache_lite-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9b04d5fb077a6f98238e205e6279dc0e24ebd16a23e58f937284e571e52d3f3b
MD5 b3e846a6e3a0d0fc4f9d2867c497a960
BLAKE2b-256 4d85bdbed3be9f2f1aa47253fd619549c2756aaa1ad8eaa566639cd0f5e214be

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_cache_lite-0.2.2-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