feat(perf): content-keyed embedding cache to skip redundant per-turn embeds#1748
Open
github-actions[bot] wants to merge 2 commits into
Open
feat(perf): content-keyed embedding cache to skip redundant per-turn embeds#1748github-actions[bot] wants to merge 2 commits into
github-actions[bot] wants to merge 2 commits into
Conversation
…embeds Adds an LRU cache keyed on (model_id, dim, sha256(text)) so identical query text re-embedded across turns pays the Lemonade embed cost once. The key is the content, so a hit is never stale and a model swap invalidates by construction. Wired into MemoryMixin._embed_text and RAGSDK query encoding; stored memories and doc chunks already persist their vectors, so this targets repeated query embeds only. Closes #1743
2 tasks
kovtcharov-amd
approved these changes
Jun 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every chat turn re-embedded the query from scratch, so identical text — the same
recall(query=…)across turns, or hybrid search re-embedding input a tool call already embedded that turn — paid the Lemonade embed cost twice, adding latency and avoidable backend calls. This adds a content-keyed LRU cache so an identical embed is served from memory and makes zero backend calls.The cache key is the content —
(model_id, dim, sha256(text))— so a hit is never stale and swapping the embedding model invalidates by construction. It's wired into the two per-turn embed sites (MemoryMixin._embed_textand RAG query encoding); stored memories and doc chunks already persist their vectors, so this targets repeated query embeds only and leaves indexing untouched.Note: this does not by itself fix the NPU load loop (#1746) — a genuinely new query still embeds once.
Closes #1743
Test plan
python -m pytest tests/unit/test_embedding_cache.py tests/unit/test_memory_mixin.py tests/unit/rag/ -qpassespython util/lint.py --allpasses (Black, isort, Pylint, Flake8 clean on the changed files)