Store and Search Memory

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

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

Store Messages and Memories

Create the Oracle 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 memory_extraction_config=MemoryExtractionConfig(extract_memories=False) either when constructing the client or when calling create_thread().

from oracleagentmemory.core import SchemaPolicy
import oracledb

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 = oracledb.SessionPool(
    user="YOUR DB USER",
    password="YOUR DB PASSWORD",
    dsn="localhost:1521/...",
)
memory_store_id = "T_QUICKSTART"
memory = OracleAgentMemory(
    connection=db_pool,
    embedder=embedder,
    llm=llm,
    schema_policy=SchemaPolicy.CREATE_IF_NECESSARY,
    memory_store_id=memory_store_id,
)

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.")
API Reference: OracleAgentMemory OracleThread

Long messages and memories are chunked automatically for retrieval indexing when needed. Chunking does not change the stored message or memory text. Search uses the chunks internally, then returns the original logical records.

Note: Replace "YOUR DB USER", "YOUR DB PASSWORD", and "YOUR DB CONNECT STRING" with the credentials and connect string for your Oracle AI Database. If you need a local database for this example, follow Run Oracle AI Database locally.

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}")
#- [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 behavior.

API Reference: SearchScope OracleSearchResult

For configurable direct-result limits, reranking, pruning, and token budgets, see Improve Search Result Relevance.

Conclusion

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

→ Having learned the core single-user memory workflow, you may now proceed to Use Oracle Agent Memory Short-Term APIs with LangGraph.

Full Code

Copy the complete code that follows.

#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.

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

##Add memories

from oracleagentmemory.core import SchemaPolicy
import oracledb

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 = oracledb.SessionPool(
    user="YOUR DB USER",
    password="YOUR DB PASSWORD",
    dsn="localhost:1521/...",
)
memory_store_id = "T_QUICKSTART"
memory = OracleAgentMemory(
    connection=db_pool,
    embedder=embedder,
    llm=llm,
    schema_policy=SchemaPolicy.CREATE_IF_NECESSARY,
    memory_store_id=memory_store_id,
)

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 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.