CertSafari
    Databricks Certified Context Engineer Associate· Lessons

    Domain 4 · Lesson 23/45

    Retrieving agent memories from Lakebase: AI Search or a structured query

    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.

    15 min read
    2.25% of exam
    9 sources
    Published 22 Oct 2026
    Docs as of 17 Oct 2026

    What you will be able to do

    • Decide from what an agent knows at query time whether a memory lookup should be a structured Postgres query or a Databricks AI Search query
    • Explain why vector or hybrid search and Genie are the wrong tools for a lookup on an exact key
    • Explain why equality filters, date ranges and joins can't find memories that are related only by meaning
    • Describe what has to happen before AI Search can reach memories that are stored in Lakebase Postgres

    Key concept

    Query type decides the retrieval mechanism — If the agent knows the key of the memory it wants (whose it is, what kind, which ID or which time range), it runs an exact structured query against Lakebase. If it only knows what the memory is about, it needs semantic search, and on Databricks that means AI Search.

    1.Two questions an agent can ask its memory

    An agent's long-term memories hold the facts, preferences and decisions it should still have in a later, unrelated conversation. The agent can ask for one of these in two ways, and each way needs a different retrieval mechanism. The first way is "give me the memory with this key": this user's preferences, everything captured in session 42, the notes written last week. The second is "give me the memories about this": past tickets like the one that just arrived, notes related to what the user just asked. Here the agent knows a topic but has no key.

    Databricks' managed memory API has the same split. An agent can list entries for an actor, filtered by session_id or a path prefix. That is a lookup on known fields. It can also search an actor's entries with a natural-language query, which ranks results by relevance. Each memory entry is organized by actor_id (who it belongs to), an optional session_id, and a filesystem-like path such as /preferences/response-style.md. These fields are what make keyed lookups possible.

    The query type is what decides the mechanism. How the data is stored doesn't. One Lakebase table of memory rows can answer both kinds of question. A keyed question goes to Postgres as an exact query. A topical question goes to an index built for similarity. Before choosing, ask one thing about the request: does the agent already hold a value it can put in a WHERE clause?

    Matching what the agent knows to the mechanism that retrieves the memory
    What the agent knowsExample requestMechanism
    Owner and path of the entryLoad actor_id user-123's /preferences/communication.mdStructured query (exact match)
    The session the memory came fromEvery entry with session_id from yesterday's support callStructured query (equality filter)
    A category encoded in the pathAll entries under the /preferences/ path prefixStructured query (prefix filter)
    Only the topic, in any wordingPast interactions conceptually like this new ticketAI Search similarity query

    Checkpoint 1 of 8· Check yourself

    A support agent is serving user-123 when a new ticket arrives about a failed invoice export. The agent wants that user's earlier memories about similar problems, however those memories were worded. Which retrieval fits this request?

    Sources12

    2.Exact keys go to a structured query

    When the agent holds the key, use an exact Postgres query against Lakebase. A user ID, a memory type, a session ID or a date range all translate directly into WHERE user_id = ..., WHERE path LIKE '/preferences/%' or WHERE created_at BETWEEN ..., with ORDER BY when the agent wants the most recent memories first. The database returns exactly the matching rows, and it can do so because Lakebase is a Postgres OLTP database. Self-managed memory on Lakebase is the option Databricks positions for teams that need direct SQL access to their memories or a custom schema.

    For an agent, the cleanest version of this is a tool whose SQL is written in advance and whose parameters the agent fills in. Databricks recommends a Unity Catalog SQL function for exactly this case: the query is known ahead of time and the agent provides the parameters. The example below uses a customer table, but the pattern carries over directly to memory rows: a single typed input, an equality WHERE clause and LIMIT 1.

    A Unity Catalog SQL function as a structured retrieval tool: fixed query, equality filter, one row backsql
    CREATE FUNCTION my_catalog.my_schema.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 my_catalog.my_schema.customer_data
      WHERE customer_name = customer_name_input
      LIMIT 1;

    Similarity search is the wrong fit here because it is approximate and ranked. AI Search finds approximate nearest neighbours by distance between embeddings. A user ID carries no meaning for an embedding to capture, and "closest" isn't the same as "equal". Hybrid search doesn't change this. It adds keyword scoring but still returns a ranked list, by default up to 200 results, when the agent wanted one row it could already name.

    Genie is also the wrong choice for a known lookup. Databricks recommends Genie One for agents that answer business questions across the workspace in natural language. Sending a memory lookup through Genie adds a natural-language interpretation step to a query that is already fully specified.

    Checkpoint 2 of 8· Fill the gap

    This Unity Catalog SQL function is a structured retrieval tool. It takes one typed input, matches it exactly and should hand back a single row. Which keyword completes the last line?

    CREATE FUNCTION my_catalog.my_schema.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 my_catalog.my_schema.customer_data
      WHERE customer_name = customer_name_input
       ?  1;

    Checkpoint 3 of 8· Check yourself

    Your team is building a tool that fetches the signed-in user's memory at a given path, such as /preferences/communication.md, from a Lakebase table. The SQL never changes. Only the path value changes from call to call. Which design fits best?

    Checkpoint 4 of 8· 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?

    Sources3451

    3.Meaning-only questions go to AI Search

    When the agent knows only what a memory is about, use AI Search. Take a support agent that gets a ticket about failing exports. Last month a similar case was logged as "CSV download times out". The two texts share no keywords, there is no ticket ID linking them, and the agent can't know which category the old note was filed under. AI Search can still find it: it finds the embeddings most similar to the query, and embeddings represent the semantic content of the text, not its exact words.

    In this case each structured alternative needs a value the agent doesn't have. An equality filter on a category column needs the category. A BETWEEN on creation date needs to know when the related note was written. A foreign-key join to a lookup table needs a relationship someone recorded in advance. A personal assistant with thousands of free-text notes, asked to find the few that are topically closest to a new request, has none of these values. Only similarity ranking over embeddings can bring those notes up.

    A filter can still be part of a semantic query. An AI Search query can include metadata filters, so the agent can restrict the search to one actor's memories and then rank them by meaning. The filter limits which rows are searched, and similarity ranking picks among them. That is still semantic retrieval. Running a filter doesn't make a request a structured lookup. What matters is whether the result set is defined by a key or by relevance.

    Hybrid search is useful when free text contains identifiers. If memory notes mention SKUs or error codes inside their prose, hybrid search combines vector similarity with keyword matching, so an exact token isn't lost to pure similarity. This differs from the case in the previous section. There the identifier was a column the agent could filter on. Here it is a word inside text that is also being searched by meaning.

    Checkpoint 5 of 8· Match them up

    An agent's memories sit in one Lakebase table, and the notes are also indexed for AI Search. Match each retrieval mechanism to the request it fits best.

    Tap a term, then the definition that fits it.

    Checkpoint 6 of 8· 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?

    Checkpoint 7 of 8· 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?

    Sources6

    4.Getting Lakebase memories into an AI Search index

    The two mechanisms read from different places. Memories are written to Postgres in Lakebase, which is the agent's operational store (see Lakebase as the agent state store). A structured query reads those rows directly. An AI Search index can't, because it is built from a source table that has to be a Delta Lake table, a streaming table or a managed Iceberg v3 table. A memory row that exists only in Postgres can't be found by semantic search.

    Lakebase Change Data Feed connects the two stores. It captures each insert, update and delete on a Lakebase table from the write-ahead log. It writes each change as a new row in a Unity Catalog managed Delta table, flushed about every 15 seconds. You can then build an AI Search index on Delta data and set it to sync automatically whenever its source table changes. A memory saved during a conversation therefore becomes searchable after a short delay. It is not searchable at the moment it is written.

    The CDF table is a history of changes, not a current snapshot. Each update produces two rows, an update_preimage with the old value and an update_postimage with the new one, next to the original insert. An index built directly on that history would include outdated versions of the same memory. A safer design turns the history into a current-state table first, for example with a materialized view, and builds the index on that table.

    Checkpoint 8 of 8· Check yourself

    Memories are written to a Postgres table in Lakebase during each conversation, and the team now wants the agent to find them by meaning with Databricks AI Search. What setup makes those memories searchable without returning outdated versions of edited memories?

    Sources6789

    Sources

    (9)

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

    1. 1.
      “List entries for an actor, optionally filtered by session_id or a path prefix.”
      ↩︎ Two questions an agent can ask its memory
      “Search entries for an actor with a natural-language query.”
      ↩︎ Two questions an agent can ask its memory
      “Never let the model choose whose memory to read or write.”
      ↩︎ Exact keys go to a structured query
      “List entries for an actor, optionally filtered by session_id or a path prefix.”
      ↩︎ Key concept
    2. 2.
      “Agent memory stores durable facts, preferences, and decisions that an agent recalls in later, separate conversations, retrieved by semantic search.”
      ↩︎ Two questions an agent can ask its memory
    3. 3.
      “Use this approach when you need direct SQL access to memories”
      ↩︎ Exact keys go to a structured query
    4. 4.
      “Create a structured retrieval tool using Unity Catalog SQL functions when the query is known ahead of time and the agent provides the parameters.”
      ↩︎ Exact keys go to a structured query
      “Databricks recommends the Genie One MCP for agents that answer business questions across the workspace in natural language.”
      ↩︎ Exact keys go to a structured query
      “Create a structured retrieval tool using Unity Catalog SQL functions when the query is known ahead of time and the agent provides the parameters.”
      ↩︎ Exam trap 2
    5. 5.
      “By default, hybrid search includes all text metadata columns and returns a maximum of 200 results.”
      ↩︎ Exact keys go to a structured query
    6. 6.
      “Embeddings are mathematical representations of the semantic content of data, typically text or image data.”
      ↩︎ Meaning-only questions go to AI Search
      “by finding the embeddings most similar to a query”
      ↩︎ Meaning-only questions go to AI Search
      “Calculate query embeddings. Query can include metadata filters.”
      ↩︎ Meaning-only questions go to AI Search
      “a vector-based similarity search to capture the semantic relationships and context of the query”
      ↩︎ Meaning-only questions go to AI Search
      “With Databricks AI Search, you create an AI Search index from a source table, which can be a Delta Lake table”
      ↩︎ Getting Lakebase memories into an AI Search index
      “You can structure the index to automatically sync when the underlying source table is updated.”
      ↩︎ Getting Lakebase memories into an AI Search index
      “source data has unique keywords such as SKUs or identifiers that are not well suited to pure similarity search”
      ↩︎ Exam trap 1
      “With Databricks AI Search, you create an AI Search index from a source table, which can be a Delta Lake table”
      ↩︎ Exam trap 3
      “Calculate query embeddings. Query can include metadata filters.”
      ↩︎ Exam trap 4
      “AI Search uses the Hierarchical Navigable Small World (HNSW) algorithm for its approximate nearest neighbor (ANN) searches”
      ↩︎ Prediction
      “particularly useful in RAG applications where source data has unique keywords such as SKUs or identifiers that are not well suited to pure similarity search”
      ↩︎ Checkpoint
      “AI Search index from a source table, which can be a Delta Lake table, streaming table, or managed table using Iceberg v3 or above”
      ↩︎ Checkpoint
    7. 7.
      “stored as a new row in a Unity Catalog managed Delta table, batched and flushed every ~15 seconds”
      ↩︎ Getting Lakebase memories into an AI Search index
    8. 8.
      “Row id=2 now appears three times: the original insert, an update_preimage with the old value, and an update_postimage with the new value.”
      ↩︎ Getting Lakebase memories into an AI Search index
      “Turn the history table into a live aggregate with a materialized view, Lakeflow pipelines, or Structured Streaming.”
      ↩︎ Getting Lakebase memories into an AI Search index
    9. 9.
      “Lakebase Search adds hybrid vector and keyword search to Lakebase projects.”
      ↩︎ Getting Lakebase memories into an AI Search index

    Ready to test yourself?

    Practise Databricks Certified Context Engineer Associate in quiz mode.

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