Kmemo Guards Semantic LLM Cache
- •Kmemo adds ten textual guards after semantic similarity to reduce wrong cached LLM responses
- •Kotlin library requires JDK 17+ and ships as kmemo-core 1.0.0 with one dependency
- •Blind validation rejects 67% of near misses and keeps 88% of paraphrases
Kmemo, an open-source Kotlin library published on July 25, 2026, offers a semantic cache for LLM calls that uses similarity search plus textual guards to reduce wrong cached answers. The author says exact-match caches miss prompts such as "how do I reverse a list in Python" after seeing "python list reverse," while semantic caches embed prompts and replay the closest prior answer to cut API calls and latency. The problem, according to the article, is that prompts such as "Convert 100 USD to EUR" and "Convert 250 USD to EUR" can score cosine similarity around 0.99, so a threshold-only cache may return the wrong currency conversion with no visible error.
Kmemo uses similarity as the first filter, then sends surviving candidates through a chain of ten guards that look for concrete differences between prompts. The guards check swapped numbers, mismatched units, different entities, different time references, negation, flipped antonyms, reversed comparisons and a different kind of requested answer. The library defaults to rejecting uncertain matches because a wrong rejection costs one extra API call, while a wrong acceptance returns a wrong answer.
Kmemo requires JDK 17+ and is distributed as `io.github.nacode-studios:kmemo-core:1.0.0`. `kmemo-core` declares `kotlinx-coroutines-core` as its only dependency, ships no embedding provider and accepts any `String` to `FloatArray` embedder supplied by the user. `getOrPut` embeds a prompt once for both lookup and write, while concurrent callers asking the same thing are coalesced so the first computes and the rest wait for its answer.
The library reports misses with reasons, including `BELOW_THRESHOLD` and `REJECTED_BY_GUARD`, because a 4% hit rate is hard to tune without knowing why lookups failed. A read-only `cache.explain(prompt)` call shows every candidate and every guard verdict. Kmemo also supports scopes for values that change a correct answer, including model, temperature, system prompt, tenant and language, with the article giving `gpt-4o|t=0.0|v3` as an example scope.
Kmemo offers `MatchGuards.standard()`, `MatchGuards.strict()` and `MatchGuards.none()`, where strict mode trades hit rate for margin and none is the similarity-only baseline. The guards also work outside English, with curated packs for Italian, Spanish, German and French measured against localized near-miss corpora. For cases needing world knowledge, such as puppy versus adult dog deworming or ethanol versus methanol boiling points, an optional `Verifier` can make a cheap model call after threshold and guards pass; timeouts or errors reject the candidate.
The article says Kmemo's guards are tested against three labelled corpora with a blind validation split used as a CI regression gate on every build. On the blind split, near misses are rejected 67% of the time and paraphrases are kept 88% of the time. The author says remaining near misses are mostly world-knowledge cases covered by the verifier and gives `./gradlew :kmemo-core:test --tests 'CorpusTest'` for reproduction.
Kmemo includes `ThresholdCalibrator` to measure the right threshold for each embedding model instead of copying one from another setup. Storage options include in-memory use, Redis with RediSearch KNN, Postgres with pgvector and an opt-in in-process HNSW store. The library also includes retrying embedder support with `maxAttempts = 4`, `EmbedFailurePolicy.FALL_BACK_TO_COMPUTE`, `negativeCacheSize = 10_000`, FAQ warming, event-stream observability, `kmemo-micrometer`, a `Slf4jCacheListener`, a Spring Boot starter, a Spring AI `Advisor`, a LangChain4j `ChatModel` wrapper, a Ktor server plugin, examples needing no API key, Apache-2.0 licensing and `1.0` SemVer stability.