chapter seven

7 Adding Agent Memory with Context and Chat Message History

 

This chapter covers

  • Inject external context to the agent via context providers
  • Provide and store agent interactions with ChatHistoryProviders
  • Implement file-based, in-memory, and vector store providers

Agents need memory to be useful. Without it, every interaction starts from scratch. The agent forgets what happened two messages ago. This chapter shows how Agent Framework handles memory through two distinct but complementary systems: context for injecting external knowledge and chat history for maintaining conversational flow.

7.1 Understanding Agent Memory: Context and Chat History

Agents maintain knowledge across interactions through two complementary mechanisms, each addressing different aspects of memory. Chat History captures the conversational flow within a session, while Context injection brings external knowledge into each interaction (figure 7.1). Understanding both mechanisms is essential for building agents that resume conversations after interruptions and reason over information beyond what appears in the dialogue.

Figure 7.1 Chat Client Agent augmented with memory providers (AIContextProvider and ChatHistoryProvider)

7.1.1 External Knowledge using Context

7.1.2 Conversational State using Chat History

7.2 Introduction to Context

7.2.1 What is AIContextProvider

7.2.2 Custom File-Based Context Provider

7.2.3 Chat History Memory Provider using In-Memory Vector Store

7.2.4 Text Search Provider using Keyword Search Adapter

7.3 Introduction to Chat History

7.3.1 What is ChatHistoryProvider

7.3.2 In-Memory ChatHistoryProvider

7.3.3 File-Based ChatHistoryProvider

7.3.4 Vector Store ChatHistoryProvider

7.4 Combining Both Providers

7.5 Conclusion

7.6 Summary