Store and Search Memory

In this article, you will create an Agent Memory component, store both thread messages and durable memories, and then search them back.

Tip: For package setup, see Get Started with Agent Memory. If you need a local Oracle AI Database for this example, see Run Oracle AI Database Locally.

Store Messages and Memories

Create the Agent Memory component with an Oracle DB connection or pool, add a short thread, and persist one durable user memory alongside the messages.

Note: By default, OracleAgentMemory expects an LLM so OracleThread can periodically extract durable memories from recent thread messages added through add_messages(). If you do not want automatic memory extraction, set extract_memories=False either when constructing the client or when calling create_thread().

from oracleagentmemory.core.oracleagentmemory import OracleAgentMemory
from oracleagentmemory.apis.searchscope import SearchScope
from oracleagentmemory.core.embedders.embedder import Embedder
from oracleagentmemory.core.llms.llm import Llm

embedder = Embedder(model="YOUR_EMBEDDING_MODEL")
llm = Llm(model="YOUR_LLM")
db_pool = ...  #an oracledb connection or connection pool
memory = OracleAgentMemory(connection=db_pool, embedder=embedder, llm=llm)

messages = [
    {
        "role": "user",
        "content": (
            "Orange juice has become my favorite breakfast drink lately, "
            "what can I pair it with?"
        ),
    },
    {
        "role": "assistant",
        "content": (
            "Nice! Orange juice goes great with something savory. "
            "Try eggs and toast, avocado toast, or a breakfast sandwich."
        ),
    },
]

thread = memory.create_thread(user_id="user_123")
#add_messages will add messages to the DB and extract memories automatically
thread.add_messages(messages)
#add_memory adds memory to the DB
thread.add_memory("The user likes orange juice with breakfast.")

Search the Agent Memory

Search the stored memories and messages, scoped to that user.

results = memory.search(query="orange juice", scope=SearchScope(user_id="user_123"))
for result in results:
    print(f"- [{result.record.record_type}] {result.content}")

Output:

- [memory] The user likes orange juice with breakfast.
- [message] Orange juice has become my favorite breakfast drink lately, what can I pair it with?
- [message] Nice! Orange juice goes great with something savory.
        Try eggs and toast, avocado toast, or a breakfast sandwich.

Note: The output shown earlier is illustrative. Future versions may return additional result types, fields, or ordering pattern.

Conclusion

In this article we learned how to create an Agent Memory client for a single user, store thread messages alongside durable memories, and search those records back with user-scoped retrieval.

Tip: Having learned the core single-user memory workflow, you may also be interested in Use Agent Memory Short-Term APIs with LangGraph.

Full Code

#Copyright © 2026 Oracle and/or its affiliates.
#This software is under the Apache License 2.0
#(LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) or Universal Permissive License
#(UPL) 1.0 (LICENSE-UPL or https://oss.oracle.com/licenses/upl), at your option.

#Agent Memory Code Example - Quickstart
#---------------------------------------

##Add memories

from oracleagentmemory.core.oracleagentmemory import OracleAgentMemory
from oracleagentmemory.apis.searchscope import SearchScope
from oracleagentmemory.core.embedders.embedder import Embedder
from oracleagentmemory.core.llms.llm import Llm

embedder = Embedder(model="YOUR_EMBEDDING_MODEL")
llm = Llm(model="YOUR_LLM")
db_pool = ...  #an oracledb connection or connection pool
memory = OracleAgentMemory(connection=db_pool, embedder=embedder, llm=llm)

messages = [
    {
        "role": "user",
        "content": (
            "Orange juice has become my favorite breakfast drink lately, "
            "what can I pair it with?"
        ),
    },
    {
        "role": "assistant",
        "content": (
            "Nice! Orange juice goes great with something savory. "
            "Try eggs and toast, avocado toast, or a breakfast sandwich."
        ),
    },
]

thread = memory.create_thread(user_id="user_123")
thread.add_messages(messages)
thread.add_memory("The user likes orange juice with breakfast.")

##Search memories

results = memory.search(query="orange juice", scope=SearchScope(user_id="user_123"))
for result in results:
    print(f"- [{result.record.record_type}] {result.content}")
#- [memory] The user likes orange juice with breakfast.
#- [message] Orange juice has become my favorite breakfast drink lately, what can I pair it with?
#- [message] Nice! Orange juice goes great with something savory. Try eggs and toast,
#avocado toast, or a breakfast sandwich.