Skip to main content

motleycache

Disk-based http caching for LLM apps and beyond.

Motleycache caches LLM and tool calls, as well as other web requests. It works with all requests made using most popular Python HTTP clients: requests, HTTPX, and Curl CFFI.

Installation

pip install motleycache

Usage

from motleycache import enable_cache, disable_cache

enable_cache()
# Now all requests made inside your code or any libraries you use will use cache if available

# To turn caching off, call this:
disable_cache()

Whitelist and blacklist

If you want to only cache certain URLs, you can use a whitelist:

from motleycache import set_cache_whitelist
set_cache_whitelist(["*://api.openai.com/*"])
# Now only requests to OpenAI API will be cached

Alternatively, you can specify a blacklist. Note that these two options are mutually exclusive.

from motleycache import set_cache_blacklist
set_cache_blacklist(["*://api.openai.com/*"])
# All requests except those made to OpenAI API will be cached

Cache location

By default, the cache is stored in the default user cache location, which depends on the platform.
It's possible to set the cache location manually:

from motleycache import set_cache_location
set_cache_location("/path/to/cache/dir")  # relative or absolute

Advanced options

There is an option to block all non-cached requests. It is particularly useful for testing your applications.
Motleycache will raise a StrongCacheException in case of a cache miss:

from motleycache import set_strong_cache
set_strong_cache(True)  # turn on
set_strong_cache(False)  # turn off

If you want to only use existing cache, you can disable updating the cache with new requests:

from motleycache import set_update_cache_if_exists
set_update_cache_if_exists(False)  # disable
set_update_cache_if_exists(True)  # enable

Motleycrew and Lunary integration

Motleycache was first designed to be a part of our multi-agent framework motleycrew (check it out!), but we soon decided to make it a separate lightweight library instead. Because of that, motleycache also provides integration with Lunary observability platform, so that cached calls are indicated in your traces.
To find out more, see our Caching and observability docs page.

How it works

When enable_cache is called, we decorate the request functions of supported clients with our caching wrapper. When a request is made to a cacheable URL, a hash of all its parameters is computed, the corresponding cache file (containing pickled response) is retrieved if it exists, and its unpickled contents are returned to the caller.
In case of a cache miss, the request is passed on to the original client function (unless strong caching is enabled), and the response is then added to the cache (unless cache updating is turned off).

If you use some other HTTP client and want us to support it, please raise an issue in this repo. Contributions are also welcome!

Metadata

Release files for motleycache 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for motleycache 0.0.4
File Size Uploaded
motleycache-0.0.4.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for motleycache 0.0.4
File Interpreter ABI Platform
motleycache-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 16.9 kB

Release files / motleycache-0.0.4.tar.gz

Download URL motleycache-0.0.4.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f796b079680d882b5cbbd653e441590b25ade4dab4729928968ccda63efd14bb
BLAKE2b-256 checksum
How to use checksums
df5197c520c5020fb3a413dfcd1de4ccd1deb5f8675d989b6550e96d1b6832ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.10.8 Windows/10

Release files / motleycache-0.0.4-py3-none-any.whl

Download URL motleycache-0.0.4-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1dacdb24a4c64c29c392dba4990f2ce9f8cad4078ae54a56a25a34ddda17b8b
BLAKE2b-256 checksum
How to use checksums
16d1439b689619bdae0dc1ec313098ecc6c4e50846d27bce65804427309c9ec6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.10.8 Windows/10

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page