chapter four
4 The Session Service: Teaching your AI to remember
This chapter covers
- Designing the Session Service to store and retrieve conversation history
- Understanding what a session contains: messages, roles, tool calls, and metadata
- Defining the gRPC contract that other services use to interact with sessions
- Abstracting session storage so teams can choose their backend
- Implementing a complete PostgreSQL backend for session persistence
- Connecting the Session Service to the SDK
- Extending the Session Service with model-managed memory
- Managing context windows with strategies like summarization, hierarchical memory, and retrieval-augmented approaches
In this chapter we will build the Session Service, one half of what Chapter 1 called "context-aware intelligence" (the Data Service, which handles organizational knowledge, is the other half). The Session Service provides conversation memory: the ability to remember what's been said so that follow-up questions make sense and the assistant can reference earlier parts of the conversation. This capability transforms a stateless AI system into something genuinely useful. When a patient asks, "What documents do I need?" and then follows up with "What about for my child?", the assistant understands that "what" refers to documents because it remembers the previous exchange.