WEBVTT

00:00:08.030 --> 00:00:11.810
Why your courier can deliver to a customer's old address.

00:00:12.160 --> 00:00:16.210
This is the second of seven in Can You Trust Your Agent's Memory in

00:00:16.290 --> 00:00:17.250
Production?

00:00:17.320 --> 00:00:21.410
Written by Delali, and read by Metatron, his AI voice.

00:00:21.970 --> 00:00:26.340
Your customers can give your AI agent a new delivery address and then find

00:00:26.420 --> 00:00:28.780
the old one in a later reply.

00:00:28.820 --> 00:00:33.100
Suppose that a customer gives your agent an address in Glasgow in October.

00:00:33.140 --> 00:00:37.913
In November, the customer moves to Brighton and gives your agent the new address.

00:00:37.967 --> 00:00:42.100
When the customer asks in December where their armchair is going, the model behind

00:00:42.180 --> 00:00:45.660
your agent can generate a reply with the Glasgow address.

00:00:45.700 --> 00:00:49.460
If your agent has a tool that books deliveries, the model can also pass

00:00:49.500 --> 00:00:51.190
the Glasgow address to that tool.

00:00:51.930 --> 00:00:56.715
In Do Returning Customers Have to Repeat Themselves to Your AI Agent?, the code

00:00:56.820 --> 00:01:00.635
gives the model a remember tool that stores each fact as a row in

00:01:00.675 --> 00:01:05.075
the customer's file, which is a separate database file for each customer.

00:01:05.115 --> 00:01:09.755
Each row has a subject, such as 'delivery address', and a value, such as

00:01:09.795 --> 00:01:11.222
the Glasgow address.

00:01:11.248 --> 00:01:14.955
When the customer gives the Brighton address, the code adds a second row with

00:01:15.035 --> 00:01:19.795
the same subject, after which it loads both rows into every request.

00:01:19.835 --> 00:01:23.675
Because the code marks neither row as the current one, the model can generate

00:01:23.715 --> 00:01:25.435
a reply with either address.

00:01:26.210 --> 00:01:30.504
The LangChain team warns about the same failure in its guide to memory.

00:01:30.530 --> 00:01:34.317
For an agent that saves each memory as a separate item, the team writes

00:01:34.400 --> 00:01:39.197
that 'The model must now delete or update existing items in the list, which

00:01:39.277 --> 00:01:43.735
can be tricky', and that 'some models may default to over-inserting'.

00:01:49.120 --> 00:01:53.650
Check whether your agent provides the old value after the customer changes it.

00:01:54.450 --> 00:01:57.415
Carry out this check on your agent as it is today.

00:01:57.710 --> 00:02:02.255
Use a test customer for whom your code stores nothing so that every value

00:02:02.335 --> 00:02:04.895
in a later reply comes from an earlier chat.

00:02:05.710 --> 00:02:07.840
Picture the check as four steps.

00:02:08.130 --> 00:02:12.720
As the test customer, give the agent a delivery address in one chat, such

00:02:12.840 --> 00:02:17.600
as 41 Kelvin Road in Glasgow, and a new address in a second chat,

00:02:17.660 --> 00:02:20.240
such as 9 Marine Parade in Brighton.

00:02:20.780 --> 00:02:24.880
In a third chat, ask which delivery address the agent has for you.

00:02:25.390 --> 00:02:29.920
If the reply contains the old address, or both addresses, your code puts the

00:02:30.080 --> 00:02:31.760
old address into the request.

00:02:32.300 --> 00:02:36.400
Finally, look at the rows that your database stores for that fact, because a

00:02:36.480 --> 00:02:40.831
reply with only the new address can come from a request that contains both.

00:02:46.220 --> 00:02:50.215
Your code can send both addresses to the model in every later chat.

00:02:50.750 --> 00:02:56.920
On Sirannon 0.4.2, which is my database, the code with that remember tool puts

00:02:57.040 --> 00:03:00.280
two lines at the end of the instructions for the third chat of the

00:03:00.370 --> 00:03:01.080
check.

00:03:01.130 --> 00:03:04.360
Each line starts with the subject delivery address.

00:03:04.450 --> 00:03:09.640
The first line gives 41 Kelvin Road in Glasgow, while the second gives 9

00:03:09.720 --> 00:03:11.390
Marine Parade in Brighton.

00:03:12.140 --> 00:03:15.460
Both lines have the same subject and no date.

00:03:15.540 --> 00:03:19.540
The order of the lines is the only clue to which address is current,

00:03:19.590 --> 00:03:23.140
which means that the model can generate a reply with the Glasgow address in

00:03:23.220 --> 00:03:26.160
one chat and with the Brighton address in the next.

00:03:26.900 --> 00:03:31.400
Chat products with memory can provide wrong answers about a changed fact as well.

00:03:31.630 --> 00:03:38.720
In August 2024, the authors of LongMemEval tested ChatGPT and Coze, two chat products

00:03:38.850 --> 00:03:41.520
that saved facts about their users at that time.

00:03:41.860 --> 00:03:48.960
With GPT-4o as the model in both, the testers marked 83.3% of ChatGPT's answers

00:03:49.050 --> 00:03:53.440
as right on the questions about a detail that changed in an earlier chat,

00:03:53.530 --> 00:03:56.800
against 20.8% of Coze's answers.

00:03:56.920 --> 00:04:01.040
Since both products used the same model, the difference came from the memory of

00:04:01.210 --> 00:04:01.940
each product.

00:04:02.670 --> 00:04:06.370
The illustration that comes next lets you choose what your code does when a

00:04:06.640 --> 00:04:11.010
customer gives a new address, and its slider changes how many times the customer

00:04:11.230 --> 00:04:11.580
moves.

00:04:12.340 --> 00:04:16.333
Picture the customer's file beside the request for the armchair question.

00:04:16.387 --> 00:04:20.200
When your code adds a row for each new address, both the file and

00:04:20.280 --> 00:04:24.520
the request gain a row with every move, and the reply gives the Glasgow

00:04:24.600 --> 00:04:25.453
address.

00:04:25.507 --> 00:04:29.880
When your code overwrites the row, the file and the request contain only the

00:04:29.960 --> 00:04:31.213
newest address.

00:04:31.267 --> 00:04:35.800
A third choice marks each old row as replaced, so that the file contains

00:04:35.880 --> 00:04:39.293
every address while the request contains only the newest.

00:04:44.670 --> 00:04:47.050
Is overwriting the old row enough?

00:04:47.550 --> 00:04:51.710
When your code overwrites the row, the model generates a reply with the right

00:04:51.790 --> 00:04:54.990
address, because the file contains only one address.

00:04:55.520 --> 00:04:59.950
However, the remember tool stores whatever value the model passes to it.

00:05:00.410 --> 00:05:04.750
Since the model generates that value from the customer's words, it can generate a

00:05:04.830 --> 00:05:05.550
wrong one.

00:05:06.280 --> 00:05:10.560
Suppose that the customer writes 'Please send this one gift to my sister at

00:05:10.640 --> 00:05:15.760
27 Elm Grove, Cardiff CF24 3RB.'

00:05:15.800 --> 00:05:19.280
The model can pass the sister's address to the tool as the new delivery

00:05:19.490 --> 00:05:24.600
address, at which point your code replaces the Brighton address with the Cardiff one.

00:05:24.640 --> 00:05:29.040
Your code will then load the Cardiff address into every later request, until the

00:05:29.120 --> 00:05:32.440
customer notices the mistake and gives the Brighton address again.

00:05:33.200 --> 00:05:37.660
In the same guide, the LangChain team writes that other models 'may default to

00:05:37.740 --> 00:05:38.940
over-updating'.

00:05:38.940 --> 00:05:45.580
The LongMemEval authors also report that ChatGPT 'tended to overwrite crucial information as the

00:05:45.700 --> 00:05:46.940
chat continues'.

00:05:46.940 --> 00:05:51.260
To guard against this, have your code mark the old row as replaced in

00:05:51.380 --> 00:05:53.020
place of overwriting it.

00:05:53.140 --> 00:05:56.940
After a wrong save, your code can then make the Brighton row the current

00:05:57.020 --> 00:05:57.560
one again.

00:05:58.340 --> 00:06:02.449
You will also need the old rows when a customer complains, as in 'I

00:06:02.529 --> 00:06:05.089
gave you my new address on 20 November.

00:06:05.129 --> 00:06:07.649
Why is my armchair going to Glasgow?'

00:06:07.720 --> 00:06:11.409
Each row that this code saves contains the time of the save and the

00:06:11.489 --> 00:06:14.742
ID of the chat in which the customer gave the fact.

00:06:14.796 --> 00:06:18.529
While the replaced rows remain in the file, you can find which address your

00:06:18.590 --> 00:06:23.369
code loaded on any past date, and in which chat the customer changed it.

00:06:23.409 --> 00:06:27.597
After an overwrite, however, your code stores only today's address.

00:06:32.970 --> 00:06:36.110
How your code can mark the old row as replaced.

00:06:36.650 --> 00:06:40.930
Add a superseded by column to the table of facts, under the same name

00:06:41.010 --> 00:06:44.050
as in Sirannon's guide to memory and forgetting.

00:06:44.160 --> 00:06:48.050
In each old row, that column will contain the ID of the row that

00:06:48.130 --> 00:06:49.330
replaces it.

00:06:49.430 --> 00:06:53.730
To save a new value, have your code add the new row first, then

00:06:53.810 --> 00:06:58.370
write the new row's ID into the superseded by column of every other current

00:06:58.460 --> 00:07:00.370
row with the same subject.

00:07:00.500 --> 00:07:04.850
When your code loads facts, have it load only the rows whose superseded by

00:07:05.010 --> 00:07:06.370
column is empty.

00:07:06.480 --> 00:07:10.770
The customer's file will then contain the replaced rows as well, while the request

00:07:10.950 --> 00:07:12.850
will contain only the current one.

00:07:13.140 --> 00:07:18.050
In the illustration, the option called marks the old row as replaced shows each

00:07:18.130 --> 00:07:21.280
replaced row with the ID of the row that replaces it.

00:07:22.140 --> 00:07:26.460
Make both writes in one transaction so that a failure between them leaves the

00:07:26.490 --> 00:07:28.190
old address as the current one.

00:07:28.890 --> 00:07:32.760
Your code finds the old row by its subject, which comes from the model's

00:07:32.800 --> 00:07:33.480
tool call.

00:07:33.960 --> 00:07:38.280
Suppose that the model generates 'home address' as the subject of the Brighton address.

00:07:38.810 --> 00:07:43.800
The Glasgow row under 'delivery address' will then remain a current row as well.

00:07:43.880 --> 00:07:48.280
To prevent that, give the tool a fixed list of subjects, in which 'delivery

00:07:48.480 --> 00:07:50.886
address' is the only subject for an address.

00:07:56.250 --> 00:07:59.130
From a short description to the change in your project.

00:07:59.730 --> 00:08:03.230
Paste the brief that comes next into your coding agent as it is.

00:08:03.660 --> 00:08:07.950
From it, your coding agent can generate code with the same design for every

00:08:08.010 --> 00:08:12.590
kind of fact that your AI agent saves, in whichever database your AI agent

00:08:12.750 --> 00:08:13.230
uses.

00:08:14.040 --> 00:08:17.635
The brief asks your coding agent to put only the new value of a

00:08:17.715 --> 00:08:22.515
saved fact into later requests, and to record the earlier values as replaced.

00:08:22.920 --> 00:08:27.795
Under the brief, every fact takes a subject from a fixed list, each save

00:08:27.875 --> 00:08:32.435
marks the current value as replaced in the same transaction, and the code loads

00:08:32.540 --> 00:08:33.875
only current values.

00:08:34.250 --> 00:08:38.435
Finally, the brief asks your coding agent to show you that the next request

00:08:38.510 --> 00:08:42.995
contains only the new value, while the store contains the old one, marked as

00:08:43.100 --> 00:08:43.715
replaced.

00:08:44.530 --> 00:08:49.400
The code that follows is one correct result of the brief on Sirannon 0.4,

00:08:49.440 --> 00:08:53.360
for a project whose saved facts already use the subjects in the list.

00:08:53.580 --> 00:08:58.200
A new migration adds the superseded by column and marks the older rows of

00:08:58.260 --> 00:08:59.960
each subject as replaced.

00:09:00.160 --> 00:09:05.160
The remember function makes both writes, while recall current takes the place of recall

00:09:05.290 --> 00:09:05.780
all.

00:09:06.480 --> 00:09:11.030
In the mock model file, the support model function returns the scripted replies from

00:09:11.070 --> 00:09:14.470
the first fix in place of a real model so that you see the

00:09:14.550 --> 00:09:17.580
same replies every time that you start the support file.

00:09:18.310 --> 00:09:21.650
The code for the brief comes in four files, which you can copy from

00:09:21.690 --> 00:09:22.730
the written version.

00:09:22.940 --> 00:09:27.410
When the main file runs, the customer gives the Glasgow address in one chat,

00:09:27.450 --> 00:09:31.630
the Brighton address in the next, and then asks where the armchair is going.

00:09:32.350 --> 00:09:37.290
In the output, the instructions for the armchair chat contain only the Brighton address,

00:09:37.350 --> 00:09:41.610
while the stored rows include the Glasgow row as well, marked as replaced by

00:09:41.770 --> 00:09:42.471
row 2.

00:09:47.810 --> 00:09:50.000
Repeating the check after your change.

00:09:50.620 --> 00:09:53.635
Repeat the check from the start with a new test customer.

00:09:54.080 --> 00:09:58.355
This time, the reply in the third chat will contain the new value, while

00:09:58.435 --> 00:10:02.435
your database contains the old value as well, marked as replaced.

00:10:03.740 --> 00:10:07.760
The rows that your code saves before the change can contain two values of

00:10:07.840 --> 00:10:10.000
one fact for the same customer as well.

00:10:10.590 --> 00:10:14.640
To test that case, give a second test customer an old value and a

00:10:14.720 --> 00:10:17.200
new value before you put the change live.

00:10:17.740 --> 00:10:21.200
After the change, repeat the third step as that customer.

00:10:21.700 --> 00:10:26.240
The reply will contain the newer value, because the new migration marks every value

00:10:26.280 --> 00:10:29.200
of a subject except the newest as replaced.

00:10:34.630 --> 00:10:36.630
Other facts that customers change.

00:10:37.260 --> 00:10:40.735
The written version ends with a short quiz, in which you can try the

00:10:40.815 --> 00:10:44.640
same design on the facts that other businesses save about their customers.

00:10:45.910 --> 00:10:51.630
The quiz takes five made-up businesses: a florist in Auckland, a bakery in Accra,

00:10:51.690 --> 00:10:56.750
a bank in Kuala Lumpur, a gym in Vienna, and a pharmacy in Montreal.

00:10:57.300 --> 00:11:02.510
Each one asks what the business's code saves, loads, or can restore after a

00:11:02.590 --> 00:11:04.123
customer changes a fact.

00:11:09.480 --> 00:11:11.197
When the new value is wrong.

00:11:11.920 --> 00:11:15.820
Your code marks the old row as replaced even when the new value is

00:11:15.900 --> 00:11:16.700
wrong.

00:11:16.780 --> 00:11:20.700
Suppose that the model sends the Brighton address to a postcode lookup tool that

00:11:20.940 --> 00:11:22.620
returns the wrong street.

00:11:22.710 --> 00:11:26.620
If the model then passes that street to remember, your code will save it

00:11:26.700 --> 00:11:31.020
as the newest delivery address and mark the Brighton row as replaced.

00:11:31.100 --> 00:11:35.200
From that save onwards, it will load the wrong street into every chat.

00:11:35.960 --> 00:11:40.265
Your code can make the Brighton row current again to repair that one fact.

00:11:40.350 --> 00:11:44.905
Before anyone notices the wrong street, however, the model can include it in replies

00:11:44.990 --> 00:11:48.345
and in more tool calls, which are wrong whether or not you repair the

00:11:48.425 --> 00:11:49.225
fact.

00:11:49.310 --> 00:11:53.465
In One Wrong Tool Result Is Enough to Corrupt Later Replies, I will show

00:11:53.545 --> 00:11:57.385
you how you can undo a wrong value along with everything that the model

00:11:57.465 --> 00:11:58.342
generates from it.
