openai_sqlite_cache
openai_sqlite_cache is an unofficial, transparent SQLite cache for the
OpenAI Python SDK. When the same API endpoint and model receive exactly the
same input again, the cached response is returned without another upstream
request.
The PyPI distribution is named openai_sqlite_cache (normalized as
openai-sqlite-cache). The Python import package remains cached_openai so
existing code only needs an import replacement.
Installation
Install from PyPI:
pip install openai_sqlite_cache
Install from a local checkout:
pip install ./openai_sqlite_cache
Package conflict: this project and the existing
cached-openaidistribution both provide thecached_openaiimport package. Do not install both distributions in the same Python environment.
Usage
Keep the local openai alias and replace only the import:
# Before: import openai
import cached_openai as openai
client = openai.OpenAI()
response = client.responses.create(
model="gpt-5",
input="Explain SQLite in one sentence.",
)
print(response.output_text)
Using the import package directly also works:
import cached_openai
client = cached_openai.OpenAI()
completion = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": "Hello"}],
)
The following official SDK patterns remain available:
OpenAI,AsyncOpenAI,AzureOpenAI, andAsyncAzureOpenAI;- module-level calls such as
openai.chat.completions.create(...); with_options(),with_raw_response, andwith_streaming_response;- custom
base_urlvalues and custom synchronous or asynchronous HTTP clients; - official SDK response models and exception types.
A streaming response is cached after it has been fully consumed. If a stream is closed early, its incomplete contents are not cached. Realtime/WebSocket traffic does not use ordinary HTTP requests and is not cached.
Cache keys and endpoint scope
Cache keys use SHA-256 and include:
- the HTTP method and normalized full API URL;
- the request body (JSON keys are sorted, while multipart boundaries are ignored and uploaded file contents are hashed);
- the model name;
- semantic request headers such as organization, project, and beta headers;
- a one-way credential-scope hash that prevents responses from being shared across API keys.
Request bodies and API keys are never stored in the database. The database contains only request hashes, successful response bodies, required response headers, and hit statistics. Entries do not expire by default, and only 2xx responses are cached.
To avoid incorrectly deduplicating operations with side effects, caching is limited to inference and generation endpoints: Responses, Chat Completions, Completions, Embeddings, Moderations, Images, Audio, and Videos. Management APIs such as Files, Uploads, Batches, Fine-tuning, and Vector Stores always reach the upstream service.
Configuration
By default, the database is created in the current working directory:
./llm_api_cache.sqlite3
Environment variables can override the defaults:
export CACHED_OPENAI_CACHE_PATH=/path/to/cache.sqlite3
export CACHED_OPENAI_TTL_SECONDS=86400
export CACHED_OPENAI_DISABLE=0
Global configuration must be applied before creating a client:
import cached_openai as openai
openai.configure_cache(
path="./.cache/openai.sqlite3",
ttl_seconds=24 * 60 * 60,
)
client = openai.OpenAI()
Individual clients can override the global configuration:
client = openai.OpenAI(
cache_path="./project-cache.sqlite3",
cache_ttl=3600,
cache_enabled=True,
)
Cache maintenance helpers are also available:
print(openai.cache_info())
removed = openai.clear_cache()
The cache may contain sensitive model responses and should be protected like
other local application data. The package attempts to set directory and
database permissions to 0700 and 0600, respectively.
Development
cd openai_sqlite_cache
python -m unittest discover -s tests -v
python -m pip wheel --no-deps . -w dist
Metadata
Release files for openai-sqlite-cache 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| openai_sqlite_cache-0.0.2.tar.gz | 19.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| openai_sqlite_cache-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 36.7 kB
Release files / openai_sqlite_cache-0.0.2.tar.gz
| Download URL | openai_sqlite_cache-0.0.2.tar.gz |
|---|---|
| Size | 19.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f1dcac5821ec40e22fecff205e2e5c199fc79f553a8e49c777ab0ed0fb151bbd
|
|
BLAKE2b-256 checksum How to use checksums |
e1612b3fa8d2c07e674b04e233cb88d9a66ccb645e0da4849de33ebf03a22953
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.3
|
Release files / openai_sqlite_cache-0.0.2-py3-none-any.whl
| Download URL | openai_sqlite_cache-0.0.2-py3-none-any.whl |
|---|---|
| Size | 17.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
941ecfa679feb8db3de61dd9c86d40e26dbad99e0fdda927bbd09e77b2a98932
|
|
BLAKE2b-256 checksum How to use checksums |
2d545fb07867ee151e71f894ea1a1a24d652230a55755119709429ff9050292b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.3
|