The pattern

  1. User asks a question.
  2. Embed the question.
  3. Retrieve top-k similar entries from vector store.
  4. Prepend retrieved context to the prompt.
  5. Model answers using the retrieved context.
Advertisement

Wiring in ADK Java

public class MemoryEnabledAgent extends LlmAgent {
  private final VectorStore memory;
  @Override
  protected void beforeModelCall(AgentContext ctx) {
    String q = ctx.currentUserMessage();
    List hits = memory.search(q, TOP_K);
    ctx.prependContext(formatAsContext(hits));
  }
}
Advertisement

What to store

Not every message. Distill each session down to key facts, user preferences, and outcomes. Store those.