On-Device Adaptation and Personalization › Local Retrieval: A Store You Can Trust
Language AI HandbookAll chapters Local retrieval is straightforward to describe but surprisingly subtle to implement well enough to ship. The subtlety is that on-device retrieval must be correctable, private, small, and honest about misses. Each property deserves attention.
What to store
An item might come from a deliberate “remember this” action, a correction, or an extractor’s suggestion. Treat provenance and permission as separate fields. A correction can fix this draft without giving the app permission to keep it for later. In the schema below, every record starts with consent disabled unless the caller supplies it explicitly.
PythonIn [5]Expand
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
class Provenance(Enum):
USER_EXPLICIT = "user_explicit"
USER_CORRECTION = "user_correction"
MODEL_EXTRACTED = "model_extracted"
@dataclass
class MemoryItem:
text: str
provenance: Provenance
created: datetime
last_used: datetime | None = None
uses: int = 0
consent: bool = False
tags: list[str] = field(default_factory=list)Use provenance to show where the item came from. Use consent to decide whether it can be stored or used under this design. Keep a bounded expiry policy even for unconfirmed candidates. Tags can restrict which tasks may see a record, and the last-use timestamp can support eviction. A pin can exempt an item from ordinary eviction without overriding revocation or any hard store limit.
Indexing under a tight footprint
For an exact identifier, try a normalized key lookup. It depends on correct indexing, normalization and access to the record. A lexical index handles token overlap, such as a term that appears in both the request and note. SQLite FTS5 is one example with a built-in BM25 ranking function.
An embedding can help connect a paraphrase to a note that uses different words. Similarity is not authority. For a name or ID, confirm the exact field instead of silently choosing a near match. On a small collection, compare a linear scan against an approximate index before spending memory on the index. A scan over vectors of width computes component products for dot-product scoring. Index overhead and latency depend on data size and hardware.
The miss problem
If the app cannot find an eligible record for a personal fact, ask the user or omit the detail. An explicit miss is easier to correct than a plausible invention. Keep candidate access controls active before ranking and again when assembling the context. A pin changes sorting priority within the scope for which the user authorized the entry.
The injection budget
Reserve space for the system prompt, the user’s request, and the planned answer before adding optional records. With a 4,000-token context, a 2,000-token prompt and a 500-token answer allowance leave at most 1,500 tokens for records and their separators under that accounting. Item count alone is a poor measure because records vary in length. Use the target model’s tokenizer for the actual count.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!