Giving AI Agents Memory That Learns — Jake Broekhuizen, LangChain

AI Engineer · 16 min · 163 sentences · from YouTube's caption track

Each timecode opens YouTube at the start of that sentence. Line anchors (#s42) are the cue ids in the WebVTT, and every line carries its start and end seconds. All transcripts has every talk, and the whole corpus as one file.

  1. 00:01[music]
  2. 00:12Testing.
  3. 00:14Awesome.
  4. 00:17Good morning everybody.
  5. 00:18Uh I know it's the last day of the conference, so super excited to have you here.
  6. 00:22Hopefully hopefully I can keep you engaged for the next uh 15 to 18 minutes or so.
  7. 00:27My name's Jake and I lead our labs team here at LangChain.
  8. 00:31And today I want to talk to you about a topic that I have uh been speaking a lot about with with our team and and with a lot of teams that I speak with who are building agents, and that's how to give your agents memory.
  9. 00:43Whether you're building a coding agent, a support agent, a uh a research agent, most teams arrive at the general same instinct that it should get better from one run to the next.
  10. 00:56So, let me give you an example to hold onto for the rest of this talk today.
  11. 00:59Recently my my the the labs team at LangChain have been working with a company in the financial services sector who's building a uh spending, budgeting, and non-investment advice agent.
  12. 01:11Given the highly regulated nature of the financial services sector, obviously you would imagine that it would need to follow some strict tone guidelines.
  13. 01:18And so, the way that it speaks and and the and its vocabulary and the way that it interacts with different users is obviously the the tell on whether or not it's following and abiding by the correct tone.
  14. 01:32It will derive the way that it generates this tone from the context and from the memories and from the references that it has to its rules and policies.
  15. 01:42And you can imagine in some contexts, and actually in some contexts that we experience, its tone would slip.
  16. 01:48And so, it would move from being advisory and helpful to actually pointed and directive, saying things like users in your similar situation would generally set a cap and it would actually say, "Oh, well, actually it looks like you need to cancel some subscriptions and move some money into savings."
  17. 02:04The company that we're working with thought that this is a violation of tone and there were similar instances of this, too.
  18. 02:09And so, to correct that, the review process is manual.
  19. 02:12You have to have someone understand the traces and understand where its tone slipped, make those changes to the context that it needs to reference when it's giving its tone,
  20. 02:21and then make sure that there are no regressions, as well.
  21. 02:24Obviously, that that doesn't scale, or rather it's challenging to scale.
  22. 02:27And so, the system should be or or the system around the agent should be a place where it can continuously learn and benefit from its interactions and its experiences.
  23. 02:38And that's the version of memory that I want to talk about today.
  24. 02:43To get there, I want to start with a tension that I think a lot of teams have run into.
  25. 02:46I think all of us here today, the reason why we're at this conference is we're curious, we're building agents.
  26. 02:51I think we've gotten very good at observing them, but observing them and then turning those observations and those agent experiences into lessons for future runs are obviously very different things.
  27. 03:03And so, that's where memory And so, that's where memory comes into play.
  28. 03:10Agents are producing more signal and more traces than ever before.
  29. 03:13And this is going to be something that's obviously that will obviously increase as more agents are built.
  30. 03:18That's fantastic for us because every run produces a trace and that trace is stored somewhere.
  31. 03:23That's incredibly useful.
  32. 03:24It means that we can understand the tools that were called, the reasoning that the agent made as it was on its way to making its decision, the artifacts that it referenced.
  33. 03:34Observability is obviously a critical foundation for building very reliable very reliable agents.
  34. 03:40But I think the challenge really is after the trace is stored or after the log or after the transcript of what the agent did is stored.
  35. 03:48If the future context that that agent references never changes, then a misguided skill or instruction will mean that the agent The mistake that the agent makes today will still persist in future runs.
  36. 04:01The goal or the system that we want to work towards is where a trace stops just being a log and becomes a source of signal.
  37. 04:09Memory.
  38. 04:11Agents that are observable tell you what happened.
  39. 04:13Agents that are adaptive are able to change are able to change and guide themselves on the following runs.
  40. 04:20So, I've mentioned memory.
  41. 04:21I think it will really help to sort of define what I mean when I say memory.
  42. 04:25It's the durable context that an agent references that will guide future runs.
  43. 04:31A trace, a transcript, or a log is evidence of what happened.
  44. 04:36It only becomes memory when that lesson is converted into durable context the agent can reference on future runs.
  45. 04:45Now, you'll see six things on this slide behind me.
  46. 04:48Facts, preferences, patterns, previous examples, skills, and instructions.
  47. 04:54I think that it's helpful to group them into three distinct buckets, a taxonomy that we borrow from cognitive science, and actually that applies a lot when we're talking about agents that are language-based.
  48. 05:07First, there's what the agent knows.
  49. 05:08This is often referred to as semantic memory.
  50. 05:11This includes things like the facts and the preferences that guide the way that it responds.
  51. 05:19Oops.
  52. 05:20Then there's the Then there's what the agent has experienced.
  53. 05:24This is the episodic memory.
  54. 05:25This is the learned patterns that it's seen, past interactions, and examples.
  55. 05:31And finally, there's how the agent should behave.
  56. 05:33This is also known as procedural memory.
  57. 05:36And that's the instructions, the skills that you're all probably very familiar with, and the rules that guide how it should how it should behave.
  58. 05:45Procedural memory is where actually most of the visible gains come from.
  59. 05:49So, in that financial assistance that I the example that I gave at the beginning, tone changes that we wanted to to promote to the agent and and make it follow came in the form
  60. 05:58of updating its procedural memory.
  61. 06:00We weren't giving it a new fact or we weren't telling it not to use a bad word.
  62. 06:04We were giving it a rule to follow as it engages with its users.
  63. 06:09That's the procedural memory that guides our agent.
  64. 06:15Another distinction that that I want to make and I think this is very important when thinking about the general agent agent memory paradigm is the separation between working memory or short-term memory and long-term memory.
  65. 06:27Working memory is what the agent is referencing in its current run.
  66. 06:30So, these might be things like intermediate scratch pads.
  67. 06:33It might be tool results.
  68. 06:34It might be the files retrieved as part of its operation.
  69. 06:38Everything that it needs to do to perform its current job.
  70. 06:43Long-term memory is what exists typically in some data structure or some store somewhere else that has instructions and skills and things that it needs to reference as part of
  71. 06:54future turns and and continuous behavior.
  72. 06:58Different agent harnesses will leverage and inject long-term memory in different ways, whether you're building your own SDK or or using a variety of different popular agent frameworks.
  73. 07:09Some inject it directly into the prompt.
  74. 07:11Some will use tools to retrieve that.
  75. 07:13Some will Some will pull it directly from a store.
  76. 07:17Some load it through files or some change the run time state.
  77. 07:20The The implementation can vary, but the distinction is really important.
  78. 07:24Some of the memory or some of the context that an agent references is temporary while some of it is durable and long-term and is useful to guide future runs.
  79. 07:33And I think that distinction is really important when we think about traces and logs and artifacts that the agent produces as it runs.
  80. 07:42A trace isn't just a record of the final output.
  81. 07:45It's a record of how our working memory changed over the course of the run.
  82. 07:50Some part of that should stay as history.
  83. 07:53Some part of that should become durable long-term context.
  84. 08:00And so that's what gives Oh, that's what lends itself to this really helpful mental model that I have or that I and and the team at LangChain and generally I think
  85. 08:10is is a very useful way to sort of think about the relationship between working or short-term memory and long-term memory.
  86. 08:16It's a read-write cycle.
  87. 08:18And so you might have heard of sleep-time compute or dreaming.
  88. 08:21Those terms refer to the way that memory is updated or pulled from long-term into short-term whilst the agent is doing work or actually in some cases whilst the agent is also not doing work.
  89. 08:33That's the sleep-time compute.
  90. 08:36If we start somewhere at the beginning of an agent run, it needs to read relevant context from long-term memory into its short-term memory.
  91. 08:43So this is anything This is anything important for the current run that it has It might be skills, it might be instructions.
  92. 08:50You've probably seen progressive disclosure with agents that actually pull in the relevant skills for a certain task.
  93. 08:55That's that phase moving from long-term memory into short-term memory.
  94. 09:00Then when the agent runs, it produces a trace.
  95. 09:03This evidence is really important to us because now we can understand how it retrieves context, the the tool calls that it made or the decisions or the sub-agents or anything that's part of its working job.
  96. 09:16As I mentioned, most evidence should stay as history.
  97. 09:18If we turned everything that was from a If we turned all of that evidence into long-term memory, then it would make the agent struggle and it would make it harder to reason over future tasks.
  98. 09:26And so what I think is really interesting is the filter step.
  99. 09:31How do you decide How do you decide what is useful signal and what should become durable long-term context and how do you decide what should stay as history.
  100. 09:39Once you've made that decision and we'll get into that in a moment.
  101. 09:42Once you've made that decision, that is that signal is then written back into durable long-term context.
  102. 09:48So, in the case of our financial assistant, that was be measured and not pointed or not directive, so that it can update the way that it behaves on future turns.
  103. 09:58And that's the core loop.
  104. 09:59Memory informs the run.
  105. 10:01The run makes the evidence.
  106. 10:03We filter that evidence to make signal, and then we use that signal to update memory.
  107. 10:08If you can build that flywheel, then we trend towards a place where we can build very reliable and very powerful powerful agents that just feel like they get better with time.
  108. 10:18And so, once we have that mental model, I think, you know, an even more simple way to break this down or to to abstract this away is is capturing what happened,
  109. 10:27analyzing what might matter, and then updating the context so that future runs can use this.
  110. 10:33The implementations around how different how how different people building different frameworks are still evolving, but this three-part shape is a really useful starting point.
  111. 10:44One implementation of this, and I won't I won't claim it's the only implementation, but just as just a kind of like put some concrete structure to what I'm talking about is the way that we do this with LangSmith.
  112. 10:55The capture step, to be able to understand and see the trajectory and the things that the agent did, is where our observability comes in.
  113. 11:04You're able to see the tool calls.
  114. 11:05You're you're able to see the way that the agents called sub-agents.
  115. 11:08You're you're able to see the context that was retrieved.
  116. 11:11The analyze step in the middle is something new that I'm really excited about, but basically, it is that intelligent process that's doing the background analysis and doing the background work
  117. 11:19to extract signal from traces based on the things that matter to you, and then promoting that signal to the memory store or the point of reference that the agent will use for future runs, and that is LangSmith's context hub.
  118. 11:32So, you can imagine now if there is a place where this analysis can be done, signal can be in track extracted, a remote store where you can now go and update what's in that so that the agent can pull that down the next time it runs.
  119. 11:45That is what LangChain's context hub does.
  120. 11:48And the abstract loop it maps cleanly.
  121. 11:50You capture the experience, you extract the signal, and then you store that durable context for future runs.
  122. 11:58To kind of put that in some more detail and and also to frame it in the in the the context of the financial assistant that I was speaking about at the beginning and and sort of the way that we we built this loop.
  123. 12:11When that agent runs, its context is loaded from the context hub and so that might include its instructions, its skills, relevant markdown files that matter to it, policies, and rules.
  124. 12:21When it functions or when we invoke that agent, then the a trace is produced that shows us the the uh failed tool calls or user corrections or in this case it flagged where our tone strayed from what we wanted it to be.
  125. 12:34Engine then looks for patterns and then identifies where different static skill markdown files that are based in context hub need to be changed or promoted, and it'll make that change.
  126. 12:47And then the next time our agent runs, we're able to actually go and and understand and see that its tone changes when it interacts with engages with users.
  127. 12:59So, some things that I and my team have currently uh or kind of always think about when when designing memory and and this should also as well ideally guide
  128. 13:09the the way that you design and build some of these abstractions is that agents produce so much we call it exhaust or we call it kind of like feedback.
  129. 13:16Not everything should become a memory update.
  130. 13:18And actually the devil is in the details around what should become one as as we've sort of discussed over the course of this presentation.
  131. 13:26It's understanding and deciding what matters to you.
  132. 13:29You know, most trace data actually should stay as history as referencable history.
  133. 13:32Some some might become data sets to do offline evaluations and things like that.
  134. 13:37A small a small segment of that or or subset should end up becoming what changes your agent's memory.
  135. 13:44Now, this second one is sort of like a gotcha that I I ran into a lot and I think that you sort of tread the fine line of trying to build a system that's optimized, but then also one that has
  136. 13:55a memory store or updates available to it in the hot path.
  137. 13:58And what I found was uh for particularly for long-running background agents, right?
  138. 14:03Imagine if you have an agent that's running over long time horizons and you make some memory updates in the beginning of its operation, but because of the run time state and because of the way that it's accessing memory, you don't actually
  139. 14:13access that or that's not available to it for future runs.
  140. 14:16And so, you know, understanding what you can cache, what you can't cache to be able to make sure that memory updates are available for future runs, very important uh and sort of I was I was banging my head on a wall for a while while while I was trying to sort of understand why my behavior wasn't changing
  141. 14:31and I needed to recognize and think about that second principle.
  142. 14:36And then the third one as well.
  143. 14:37I think that uh open core and uh you know, Hermes agent and and a lot of agents that are uh becoming widely popular that sort of do this self-update process,
  144. 14:47right?
  145. 14:47I think that it's another really important consideration of like what do you make self-updateable as opposed to what do you make something that needs human review?
  146. 14:54And so, if we're talking about procedural memory with instructions and policies and tone guidelines, those are probably going to drive about 75% of the behavior of my agent.
  147. 15:04And so, that actually probably should have some level of human in the loop and human interaction.
  148. 15:08You should have somewhere to surface it.
  149. 15:10The changes that you make to the parts of the agent's memory that are procedural before actually committing those and and putting those in the in the in the agent's sort of like hot reload path.
  150. 15:21So, protecting important behavior with the is, I guess, another really important third third kind of like level to this that I find really important.
  151. 15:31So, I'm going to come back to this initial refrain.
  152. 15:33The next run should be better.
  153. 15:35Memory allows us to do this.
  154. 15:36The word memory is thrown around a lot.
  155. 15:39I think in the context of building agents and working with harnesses and working with like providing context to your agent, memory really does give us the ability to do this.
  156. 15:48And it's more than just a place to store information.
  157. 15:51It's how experience becomes context.
  158. 15:54And then that context is how our next runs get the chance to improve.
  159. 16:00If you're curious about an actual implementation of this, by the way, that QR code has a video of me walking through how I built an agent that does exactly what I've just described.
  160. 16:09Uh hopefully that'll kind of like make it tangible and and will sort of connect some of those dots.
  161. 16:15Uh and if you want to have any further discussions about this, please come and see me.
  162. 16:18We're at booth Eugene 19.
  163. 16:20Uh I'd love to