|
|
ADK Agents with Long-Term Memory (Firestore)
Author: Venkata Sudhakar
ADK's built-in InMemorySessionService loses all context when the process restarts. For ShopMax India, this means the agent forgets a customer's preferred city, past complaints, or frequent purchases between sessions. Firestore solves this by persisting agent memory indefinitely - the agent can greet a returning customer by name and reference their last order.
The pattern below writes key facts to a Firestore memory document after each session and reads them back at the start of the next. The agent receives this memory as part of its context, making responses feel personalised and continuous.
It gives the following output,
Session 1: Hello Rajesh Kumar! Here are your recent ShopMax India orders:
1. OnePlus 12 - Rs 64,999
2. Sony WH-1000XM5 - Rs 29,999
Session 2: Hi Rajesh! As a OnePlus fan in Hyderabad, you might be interested
in the latest OnePlus 12R and OnePlus Open available on ShopMax India.
Shall I check stock and pricing for you?
In session 2, the agent greets Rajesh by name and references his preferred brand without being told again - this came from the Firestore memory. The key is injecting the memory facts into the system instruction at agent construction time, so they are part of every model call in that session.
For ShopMax India production, extend the memory schema to include last_query_date, total_spend_rs, loyalty_tier (Silver/Gold/Platinum), and flagged_complaints. Update the memory document after every session using a Firestore transaction to avoid race conditions when a customer has concurrent sessions. Set a Firestore TTL policy to auto-delete memory documents for customers inactive for more than 2 years to manage storage costs.
|
|