Skip to main content

langchain-hana-cache

Semantic caching for LLM responses on SAP HANA Cloud.

Stores prompt embeddings and LLM responses in HANA Cloud. When a semantically similar prompt comes in, it returns the cached response instead of calling the LLM — saving tokens and reducing latency.

How it works

  1. User sends a prompt to the LLM
  2. The cache embeds the prompt using the configured embedding model
  3. Searches HANA for cached entries using COSINE_SIMILARITY on a REAL_VECTOR column
  4. If similarity exceeds the threshold (default 0.95), returns the cached response — no LLM call
  5. If no match, calls the LLM normally, caches the prompt embedding + response, returns the response

Installation

pip install langchain-hana-cache

Usage

As LangChain global cache

import hdbcli.dbapi
from langchain_hana_cache import HANASemanticLLMCache
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_core.globals import set_llm_cache

connection = hdbcli.dbapi.connect(
    address="your-host.hanacloud.ondemand.com",
    port=443,
    user="DBADMIN",
    password="your-password",
    encrypt=True,
)

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")

cache = HANASemanticLLMCache(
    connection=connection,
    embedding=embeddings,
    table_name="LLM_CACHE",
    similarity_threshold=0.95,
    ttl_seconds=86400,
)

set_llm_cache(cache)

llm = ChatOpenAI(model="gpt-4o")
response1 = llm.invoke("What are the reporting requirements for article 12?")
response2 = llm.invoke("Tell me about article 12 reporting requirements")  # cache hit

Manual usage

from langchain_core.outputs import Generation

# Store a response
cache.update(
    "What is the capital of France?",
    "gpt-4o",
    [Generation(text="The capital of France is Paris.")],
)

# Look up a similar prompt
result = cache.lookup("Tell me the capital of France", "gpt-4o")
# result = [Generation(text="The capital of France is Paris.")]

Eviction

# Remove entries older than TTL
cache.evict_expired()

# Keep only the 1000 most recently accessed entries
cache.evict_lru(max_entries=1000)

# Clear all cached entries
cache.clear()

Parameters

Parameter Type Default Description
connection hdbcli.dbapi.Connection required HANA database connection
embedding Embeddings required LangChain embedding model for encoding prompts
table_name str "LLM_CACHE" Name of the cache table
similarity_threshold float 0.95 Minimum cosine similarity for a cache hit
ttl_seconds int | None None Time-to-live in seconds (None = no expiry)

Development

git clone https://github.com/stubborncoder/langchain-hana-cache.git
cd langchain-hana-cache
pip install -e ".[dev]"

# Run unit tests
pytest tests/test_utils.py tests/test_llm_cache.py -v

# Run integration tests (requires HANA credentials in .env)
pytest tests/test_integration.py -v

License

MIT

Metadata

Release files for langchain-hana-cache 0.1.0

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

Source distribution (sdist)

Source distribution for langchain-hana-cache 0.1.0
File Size Uploaded Provenance
langchain_hana_cache-0.1.0.tar.gz 11.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-hana-cache 0.1.0
File Interpreter ABI Platform
langchain_hana_cache-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.1 kB

Release files / langchain_hana_cache-0.1.0.tar.gz

Download URL langchain_hana_cache-0.1.0.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
826f19f21fe92110b3bbd27454fe31b39151d34ba6ac30c5071ca9ce84472119
BLAKE2b-256 checksum
How to use checksums
867fa7bac88905aada76d7c94ed269e28eaa437bf90a2433939a02d3267a63c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Release files / langchain_hana_cache-0.1.0-py3-none-any.whl

Download URL langchain_hana_cache-0.1.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d936d10f97b5aa7ca2ede231e92609e785ed0f143610998a6058235e605e9f89
BLAKE2b-256 checksum
How to use checksums
5c72b6c58921cda2007ef8b0ff86296e86d2dd12a9834fc3c142160f39349543
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.4

Security for langchain-hana-cache 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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