Expedition. Free, virtual, Nov 3–6.

Technical tracks for practitioners, outcomes for leaders.

Snowflake for Developers/Developer Blog/Eliminate Agent Accuracy Loss with Long-Term Cortex Memory

Eliminate Agent Accuracy Loss with Long-Term Cortex Memory

Cortex AI
Haebichan Jung
A Cortex Agent reading from a long-term memory of human-approved lessons at inference time.

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:

  1. Capture. A user submits negative feedback and an optional comment through the Feedback API.
  2. Diagnose. AI_COMPLETE combines the feedback with the relevant agent trace and drafts a structured case file.
  3. Review. A domain owner approves, edits, or rejects the proposed case file as a reusable lesson.
  4. Store. Approved lessons are persisted and indexed with Cortex Search.
  5. 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.

Pipeline diagram showing how negative feedback and agent traces flow through AI_COMPLETE to produce case files, which a human reviews, approves, and stores for retrieval by Cortex Search.

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.
  • INTERNAL orders 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?

Agent response reading 11,450 orders 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."

Feedback dialog capturing a thumbs-down and the comment '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_COUNT exclude 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.

Streamlit in Snowflake approval app showing a proposed case file with Approve, Edit, and Reject actions.

Step 4: Store approved lessons

Approved lessons are stored as unique rows in a Snowflake table.

LESSONS table with approved lesson rows including the INTERNAL channel exclusion rule.

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.

RLHF_MEMORY_AGENT configured with a LESSONS_MEMORY Cortex Search tool.

A different user then asks:

How many orders did we have from January through June 2026?

Agent trace showing a call to the lessons_memory Cortex Search service before answering the question.

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.

Chart comparing accuracy on 33 new questions, with-memory versus without-memory, across the three lessons.

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

Updated Oct 19, 2026

This content is provided as is, and is not maintained on an ongoing basis. It may be out of date with current Snowflake instances