Eliminate Agent Accuracy Loss with Long-Term Cortex Memory

Reinforcement Learning from Human Feedback (RLHF) is a widely used approach for aligning AI systems with human preferences. Traditional RLHF uses human preference data to update model behavior through training. The same feedback-driven principle can also be applied at the application layer, without touching model weights.
In Snowflake, you can use an RLHF-inspired approach to build long-term memory for Cortex Agents. Human feedback determines which lessons, preferences, and corrections are worth keeping. In the demo below, the agent retrieved approved memories in later conversations and answered new questions more accurately, without retraining the underlying model.
This post shows how to implement that pattern with Cortex Agents, persistent memory, Cortex Search, and feedback-driven updates. All of the code is open source at github.com/haebichan/rlhf_memory.
What you'll learn
- How to apply an RLHF-inspired feedback loop at the application layer
- How to turn agent feedback and traces into long-term memory with
AI_COMPLETE - How to retrieve the memory with Cortex Search at inference time
How RLHF-inspired agent memory works
Instead of updating model weights, this approach updates the context the agent retrieves at inference time:
- Capture. A user submits negative feedback and an optional comment through the Feedback API.
- Diagnose.
AI_COMPLETEcombines the feedback with the relevant agent trace and drafts a structured case file. - Review. A domain owner approves, edits, or rejects the proposed case file as a reusable lesson.
- Store. Approved lessons are persisted and indexed with Cortex Search.
- Retrieve. For each new question, the agent retrieves relevant lessons and uses them in its response or decision process.
No model weights are updated. The human preference signal still shapes future agent behavior, but it enters through retrieved context rather than gradient descent.

Walking through the end-to-end demo
To ground this, we built a retail sales agent on a deliberately naive semantic view. It doesn't know three business rules:
- Revenue means net.
- The fiscal year starts in February.
INTERNALorders are test data.
Let's follow one piece of feedback through the loop.
Step 1: Capture the feedback
A user asks:
How many orders did we have in 2025?

The agent says 11,450. The right answer is 10,891, because the agent counted test orders.
The user clicks thumbs-down and writes: "Order count looks too high."

The comment is vague, but the feedback carries the request ID of the answer it rates. That ID links to the full agent trace: the question, the SQL, and the results.
Step 2: Diagnose with AI_COMPLETE
AI_COMPLETE reads the feedback and the trace, then drafts a case file. It suspected test orders, but it didn't invent the rule. It flagged an open question instead:
Should
ORDER_COUNTexclude certain channels (e.g. test orders)?
The principle: draft, don't decide.
Step 3: Review in a Streamlit app
A domain owner answers the question and approves the lesson in a Streamlit in Snowflake app:
Rows with
channel = 'INTERNAL'are test orders. Exclude them from all metrics.

Step 4: Store approved lessons
Approved lessons are stored as unique rows in a Snowflake table.

Step 5: Retrieve lessons at inference time
Approved lessons are indexed by Cortex Search. On each new question, the agent retrieves only the relevant ones.

A different user then asks:
How many orders did we have from January through June 2026?

Without memory the agent returns 5,734. With memory it returns 5,451, which is correct. Different question, same fix.
Step 6: Measure the effect
To stress-test whether these lessons actually work, we asked 33 new questions to the agent with memory and to an identical agent without it.

In this synthetic retail test, all three lessons helped the agent answer new questions involving different periods, regions, and metrics. The test showed similar mean latency.
Why retrieval-based memory works
In this demo, the agent's errors came from missing business context. Approved lessons helped it apply that context to later questions.
This loop turns each thumbs-down into a lesson a human approves, and the agent retrieves that lesson the next time a similar question comes in. With three lessons, accuracy on new questions went from 48% to 100%.
It's RLHF without the training step. Feedback is the label, a human is the reward model, and retrieval takes the place of the weight update.
Try it yourself
The code is open source: github.com/haebichan/rlhf_memory.
Resources
This content is provided as is, and is not maintained on an ongoing basis. It may be out of date with current Snowflake instances