CertSafari
    Databricks Certified Context Engineer Associate· Lessons

    Domain 4 · Lesson 23/45

    Lakebase Memory Retrieval: AI Search vs Structured Queries

    Given an agent retrieving memories persisted in Lakebase, identify whether Databricks AI Search or structured query retrieval is the appropriate mechanism for a specified query type.

    17 min read
    2.25% of exam
    7 sources
    Published 3 Oct 2026
    Docs as of 30 Sep 2026

    What you will be able to do

    • Tell keyed (structured) memory lookups apart from relevance-ranked (search) memory recall, using the query's shape
    • Pick a structured query tool (memory list, Unity Catalog function, SQL or Genie MCP) when the agent already knows the fields and values to look up
    • Pick AI Search, or Lakebase Search inside Postgres, for natural-language or fuzzy recall, and choose vector, keyword or hybrid mode based on the query's wording
    • Explain why an AI Search index cannot read a Lakebase Postgres table directly, and what the sources say about bridging the two

    Key concept

    Retrieval follows the shape of the query — If the agent knows exactly which memory it wants (whose it is, which session, which path or ID), use a structured lookup that filters on those fields. If it only knows what the memory is about, in words that may not match how the memory was stored, use a ranked search such as BM25, vector or hybrid.

    1.Two ways to read a memory back

    Once an agent has stored memories in Lakebase, it needs a way to get them back. Databricks keeps two kinds of agent state there. Session state is the transcript of a single interaction, and the agent loads it by the session's ID. Memory is the set of durable facts that should still matter in a later conversation that has nothing to do with this one. Sessions are a keyed read: the agent "reads it at the start of a turn and appends to it as the interaction runs." Nothing gets ranked, because the thread ID already identifies the right state. Memory is different. The agent "recalls them whenever they are relevant, regardless of which conversation surfaced them." Here the agent knows the topic, but not where the fact was stored.

    Managed agent memory, which Databricks stores in Lakebase, offers both styles of retrieval. Each memory entry has an actor_id (whose memory it is), an optional session_id (where it was captured), and a required filesystem-style path such as /preferences/response-style.md. Together those three fields uniquely identify the entry. On top of this, the store exposes two read operations: list, which filters on the known fields, and search, which takes a natural-language query.

    The two operations answer different questions. List returns the entries for an actor, optionally narrowed by session_id or a path prefix, and the docs suggest using it "to browse or render an index of what an agent knows." It is a structured query: the fields to filter on are part of the request. Search returns entries ranked by relevance to free text, scored with BM25. That fact matters later in this lesson. A memory search ranks by the words it shares with the stored text and description, not by embedding distance. This is why each entry's description is described as improving retrieval: the description adds words the query can match.

    Memory search wrapped as an agent tool. The actor_id comes from the application and the model supplies only the query text.python
    def make_memory_tools(memory_store, actor_id: str):
        @function_tool
        def search_memory(query: str) -> str:
            """Search long-term memory for relevant facts about the user."""
            results = memory_store.search(actor_id=actor_id, query=query, limit=10)
            return "\n\n".join(f"{r.memory.path}: {r.memory.content}" for r in results) or "No memory found."

    Look at what this code fixes and what it leaves open. actor_id is passed in when the tools are built, and the model never chooses it. The docs say to set it "in trusted application code from the verified end-user identity." This is the pattern used even when the retrieval is a search: the structured part (whose memory) is set by the application, and only the open-ended part (what about) goes to relevance ranking.

    Checkpoint 1 of 9· Check yourself

    A support app's settings screen needs to show every memory saved for user-123 under /preferences/. Which managed memory operation fits?

    Sources12

    2.Structured query retrieval: when the agent already knows the key

    The managed memory API is one case of a general rule. Some memories are records with typed columns: an account's tier, a decision tied to a ticket ID, the last order a user placed. Questions about them are lookups, filters or aggregates, so SQL answers them exactly. A Lakebase database can be registered in Unity Catalog "for unified governance and cross-source queries", and Databricks provides several tools that let agents query Unity Catalog tables.

    Structured retrieval tools for agents, and the query each one is built for
    ToolWho writes the queryFits when
    Unity Catalog SQL function (e.g. lookup_customer_info)You, ahead of time; the agent supplies only parameter valuesThe question is always the same lookup, such as "get this customer's email and ID"
    Databricks SQL MCP server (/api/2.0/mcp/sql)The model generates SQL that runs on a SQL warehouseAd-hoc filters and aggregates over tables, with access governed by Unity Catalog permissions
    Genie Agent via managed MCP (/api/2.0/mcp/genie/{genie_space_id})Genie turns a natural-language question into a queryNatural-language questions about up to 25 Unity Catalog tables

    The most deterministic option is a Unity Catalog function. The docs say to use one "when the query is known ahead of time and the agent provides the parameters." The function below takes a single input, filters a table with an equality WHERE clause, and returns at most one row. No ranking or embedding is involved: the agent gets the exact record or nothing.

    A Unity Catalog function used as a structured retrieval tool: fixed SQL, with a value supplied by the agentsql
    CREATE OR REPLACE FUNCTION main.default.lookup_customer_info(
      customer_name_input STRING COMMENT 'Name of the customer whose info to look up'
    )
    RETURNS STRING
    COMMENT 'Returns metadata about a particular customer, given the customer''s name, including the customer''s email and ID. The
    customer ID can be used for other queries.'
    RETURN SELECT CONCAT(
        'Customer ID: ', customer_id, ', ',
        'Customer Email: ', customer_email
      )
      FROM main.default.customer_data
      WHERE customer_name = customer_name_input
      LIMIT 1;

    Note that "natural language" does not mean "use AI Search". Genie accepts plain-English questions, but it answers them by querying tables, so it is still structured retrieval. Questions like "What were the top 10 customers by revenue last quarter?" need the query to be computed (sort, aggregate, filter by date). Returning text that sounds similar would not answer them. So the deciding question is not whether the user typed English. It is whether the answer is a precise selection from records.

    Checkpoint 2 of 9· Check yourself

    An agent saves each user's subscription tier in a Lakebase table keyed by user ID. On every turn, it must fetch the tier for the current verified user. Which retrieval mechanism fits best?

    Checkpoint 3 of 9· Exam question

    An agent's long-term memories are stored as rows in a Lakebase Postgres table, each tagged with a `user_id` column. At the start of every turn, the agent needs to load the single memory row belonging to the current signed-in user, identified by an exact `user_id` match, with no ranking or similarity involved. Which retrieval mechanism is most appropriate for this lookup?

    Sources34

    3.Databricks AI Search: when the agent only knows what the memory is about

    Structured retrieval fails when the agent cannot name the key. Suppose a user says "remember how I wanted those reports formatted?" The stored memory might say "prefers bullet summaries, no tables". The agent has no ID or path, and the two phrasings share almost no words. This is the problem Databricks AI Search (formerly Vector Search) addresses: it returns relevant rows "by finding the embeddings most similar to a query." Embeddings capture the meaning of text, so a paraphrase can still be retrieved.

    AI Search supports more than one query mode, and the right one depends on how the query is worded. Pure similarity search handles paraphrase but can miss exact tokens. The docs recommend hybrid search for source data that contains "unique keywords such as SKUs or identifiers that are not well suited to pure similarity search." Hybrid search combines the vector results and the BM25 keyword results with Reciprocal Rank Fusion. For queries that are purely exact terms, full-text (BM25) search needs no embeddings at all. AI Search also supports filtering, so a search can be limited to structured attributes, for example one user's rows, before ranking.

    AI Search query modes and the kind of memory query each one fits
    ModeHow it ranksMemory query it fits
    Similarity (vector) searchNearest embeddings by L2 distance, using HNSW approximate nearest neighbour searchParaphrased recall: "what did we decide about the report layout?"
    Hybrid keyword-similarity searchVector and BM25 results merged with Reciprocal Rank FusionIntent plus an exact token: "notes on the issue with SKU 4471"
    Full-text keyword search (Beta)Okapi BM25 over text columns; no embeddings neededExact terms or identifiers in stored text

    Checkpoint 4 of 9· Match them up

    Match each memory query to the retrieval mechanism that fits it

    Tap a term, then the definition that fits it.

    Checkpoint 5 of 9· Exam question

    A support agent stores a running log of past customer interactions as memory entries persisted in Lakebase. When a new ticket arrives, the agent wants to surface prior interactions that are conceptually related to the new issue, even if they use different wording and share no exact keywords with the new ticket. Which mechanism should retrieve these memories?

    Sources5

    4.Memories live in Postgres, AI Search indexes read lakehouse tables

    There is a practical limit here that exam scenarios can test. An AI Search index is built from a source table, which must be "a Delta Lake table, streaming table, or managed table using Iceberg v3 or above." A Lakebase Postgres table is none of these. If memories are written to Lakebase and the agent needs semantic recall over them, there are two documented options.

    Option 1: search inside Lakebase. Lakebase Search "adds hybrid vector and keyword search to Lakebase projects" through Postgres extensions. lakebase_vector adds an approximate nearest-neighbour index type (lakebase_ann) that is compatible with pgvector. lakebase_text adds BM25 indexes (lakebase_bm25). The same decision rule applies: use vector search for natural-language questions, keyword search for "names, codes, and exact-term lookups where wording matters", and hybrid search when a query mixes the two. A benefit of this option is that the memory row, its structured columns and its search index all live in one database. One detail to note: BM25 builds corpus statistics when the index is created, "not incrementally".

    Lakebase Search: a vector similarity query and a BM25 keyword query against the same Postgres tablesql
    -- Vector similarity search
    SELECT id, title
    FROM documents
    ORDER BY embedding <=> '[0.1, 0.2, 0.3]'
    LIMIT 5;
    
    -- BM25 keyword search (lower score = more relevant)
    SELECT id, title,
      body_tsv <@> to_bm25query(to_tsvector('english', 'database'), 'documents_body_bm25') AS score
    FROM documents
    ORDER BY score
    LIMIT 5;

    Checkpoint 6 of 9· Fill the gap

    Which index type gives this Lakebase column approximate nearest-neighbour (vector) search?

    -- Create a vector search index
    CREATE INDEX documents_embedding_idx ON documents
      USING  ?  (embedding vector_cosine_ops);

    Option 2: move the data to the lakehouse first. Lakebase Change Data Feed captures every insert, update and delete, and each change is "stored as a new row in a Unity Catalog managed Delta table, batched and flushed every ~15 seconds." These Delta tables hold change history, with one row per change, not the current state of each memory. The sources provided here do not describe an end-to-end pipeline from Lakebase memory to an AI Search index. Treat that bridge as something you would design yourself, not as a documented pattern.

    Checkpoint 7 of 9· Check yourself

    A team stores long-term agent memories in a Lakebase Postgres table and wants semantic recall over them without running a separate pipeline. What is the most direct option?

    Checkpoint 8 of 9· Exam question

    A personal-assistant agent has accumulated thousands of free-text memory notes in Lakebase over many months. Given a new user request, the agent wants to find the handful of notes that are most topically similar in meaning to the request, without knowing any exact identifiers, categories, or dates to filter on in advance. Which mechanism is the appropriate choice?

    Sources567

    5.Deciding from the query type

    On the exam, a scenario usually gives you a memory store and one specific question the agent must answer. Work through it in order. First, can the agent name the record by actor, session, path, ID or another typed column? If so, use a structured query. Second, does the answer require computation over records (counts, ranges, latest, top-N)? That is also a structured query, through SQL or Genie, even if the question is phrased in English. Third, does the agent know only the topic? Then use ranked search, and choose vector search for paraphrase, keyword search for exact tokens, or hybrid for a mix. Fourth, where does the data live? Lakebase Search ranks rows inside Postgres, while AI Search indexes lakehouse tables. Many real requests combine these. The usual pattern is to fix the structured part (whose memory) in application code and run the search only over that subset.

    Checkpoint 9 of 9· Check yourself

    An agent must recall "what we noted about error code E404 when logins were slow" from memories in a Lakebase Postgres table with Lakebase Search enabled. The agent has no ID or path. Which mode fits the wording?

    Sources62

    Exam traps

    Each one states something that sounds right. Open it to see what is actually true.

    1. 1.Managed agent memory's search operation finds entries by embedding (vector) similarity.Why is that wrong?

      Managed memory search ranks entries by a full-text BM25 relevance score. It matches on shared words, which is why the description on each entry improves retrieval.

      Covered in Two ways to read a memory back

    2. 2.If the user asks in natural language, the agent should retrieve with AI Search.Why is that wrong?

      Genie answers natural-language questions by querying Unity Catalog tables, which is structured retrieval. What decides the mechanism is whether the answer is an exact selection or computation over records, not whether the input is English.

      Covered in Structured query retrieval: when the agent already knows the key

    3. 3.Pure vector similarity search is the best choice for memories identified by SKUs, error codes or other identifiers.Why is that wrong?

      Exact identifiers are poorly served by similarity alone. Use hybrid or full-text search, or a structured lookup if the identifier is a key column.

      Covered in Databricks AI Search: when the agent only knows what the memory is about

    4. 4.An AI Search index can use a Lakebase Postgres memory table as its source.Why is that wrong?

      AI Search index sources are lakehouse tables (Delta, streaming or Iceberg v3+). For semantic search directly over Postgres rows, use Lakebase Search.

      Covered in Memories live in Postgres, AI Search indexes read lakehouse tables

    Sources

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “The agent recalls them whenever they are relevant, regardless of which conversation surfaced them.”
      ↩︎ Two ways to read a memory back
      “The agent reads it at the start of a turn and appends to it as the interaction runs.”
      ↩︎ Two ways to read a memory back
    2. 2.
      “An entry is uniquely identified by the combination of actor_id, session_id, and path.”
      ↩︎ Two ways to read a memory back
      “Set the actor_id in trusted application code from the verified end-user identity.”
      ↩︎ Two ways to read a memory back
      “Never let the model choose whose memory to read or write.”
      ↩︎ Deciding from the query type
      “List entries for an actor, optionally filtered by session_id or a path prefix.”
      ↩︎ Key concept
      “Search returns the most relevant entries ranked by a full-text (BM25) relevance score.”
      ↩︎ Exam trap 1
      “Search returns the most relevant entries ranked by a full-text (BM25) relevance score.”
      ↩︎ Prediction
      “Use this to browse or render an index of what an agent knows.”
      ↩︎ Checkpoint
    3. 3.
      “when the query is known ahead of time and the agent provides the parameters”
      ↩︎ Structured query retrieval: when the agent already knows the key
      “The server exposes SQL execution as a tool, and access is governed by Unity Catalog permissions.”
      ↩︎ Structured query retrieval: when the agent already knows the key
      “If your agent needs to query data in Unity Catalog tables, Databricks recommends using Genie Agents.”
      ↩︎ Exam trap 2
    4. 4.
      “Register your Lakebase database in Unity Catalog for unified governance and cross-source queries.”
      ↩︎ Structured query retrieval: when the agent already knows the key
    5. 5.
      “by finding the embeddings most similar to a query”
      ↩︎ Databricks AI Search: when the agent only knows what the memory is about
      “The similarity search and keyword search results are combined using the Reciprocal Rank Fusion (RRF) function.”
      ↩︎ Databricks AI Search: when the agent only knows what the memory is about
      “This is useful for searching exact terms, identifiers, or keywords in text data.”
      ↩︎ Databricks AI Search: when the agent only knows what the memory is about
      “a Delta Lake table, streaming table, or managed table using Iceberg v3 or above”
      ↩︎ Memories live in Postgres, AI Search indexes read lakehouse tables
      “unique keywords such as SKUs or identifiers that are not well suited to pure similarity search”
      ↩︎ Exam trap 3
      “a Delta Lake table, streaming table, or managed table using Iceberg v3 or above”
      ↩︎ Exam trap 4
      “unique keywords such as SKUs or identifiers that are not well suited to pure similarity search”
      ↩︎ Checkpoint
    6. 6.
      “Use it for names, codes, and exact-term lookups where wording matters.”
      ↩︎ Memories live in Postgres, AI Search indexes read lakehouse tables
      “Use it when queries mix intent with specific terms, the most common case in real-world search.”
      ↩︎ Deciding from the query type
      “Lakebase Search adds hybrid vector and keyword search to Lakebase projects.”
      ↩︎ Checkpoint
    7. 7.
      “stored as a new row in a Unity Catalog managed Delta table, batched and flushed every ~15 seconds.”
      ↩︎ Memories live in Postgres, AI Search indexes read lakehouse tables

    Ready to test yourself?

    Practise Databricks Certified Context Engineer Associate in quiz mode.

    Spotted a mistake, or was something unclear? Tell us.