Semantic search in long-term memory
Semantic search in the store finds saved items by meaning, not exact words, by giving the store an embedding index and searching it with a query.
Last updated: 27 Sep, 2026 · LangChain 1.4
The store lesson looked items up by key. When you do not know the key, you want the items closest in meaning to a question. That needs an embedding index. An embedding is a list of numbers that stands for a piece of text, made so that texts with similar meaning get similar numbers.
Giving the store an embedding index
Pass index with an embed function and its dims, the number of values in each embedding. The store embeds each saved value so it can compare by meaning. The embed here is a two-number stand-in, so the matching is easy to follow. Save some items, then call search with a query: the store returns the closest ones, not an exact-key match.
from langgraph.store.memory import InMemoryStore
def embed(texts):
# a tiny stand-in embedding: [mentions refund, mentions ship]
return [[float("refund" in t.lower()), float("ship" in t.lower())] for t in texts]
store = InMemoryStore(index={"embed": embed, "dims": 2})
store.put(("faqs",), "a", {"text": "Refunds take 5 working days."})
store.put(("faqs",), "b", {"text": "Orders ship within 2 days."})
hits = store.search(("faqs",), query="how long for a refund", limit=1)
print(hits[0].value["text"])Refunds take 5 working days.
How the query matched
- The query mentioned a refund, so its embedding was close to the refund item.
searchranked the items and returned the nearest one, not an exact key.- A real embedding model replaces the stand-in; the call shape is the same.
- In an agent, a tool calls
runtime.store.search(("faqs",), query=question, limit=1)the same waylookup_ordercalledruntime.store.getin Memory that outlives the conversation.
Key lookup vs semantic search
| get by key | search by query | |
|---|---|---|
| You need | The exact key | Only a question |
| Matches | One item | The closest items by meaning |
| Setup | None | An embedding index |
When to search by meaning
- Recalling the memories relevant to what a user asked.
- Finding a saved fact when you do not know its key.
- Ranking many notes by how close they are to a question.
Related
- Previous: Memory that outlives the conversation
- Next: State beyond messages
- Reference: LangChain docs
- Add a third item and a query that should match it.
- Raise
limitto 2 and read both hits in order. - Add a third number for the word "cancel" to
embed, setdimsto 3, save a cancellation answer and search for it.
You understood something today that you didn't yesterday.