AI Explainers Agents

AI Agent Memory on Google Cloud: A Two-Tier Setup With AlloyDB and Valkey

Google Cloud describes AI agent memory split into a fast session cache and a durable database, with benchmark token use falling 72% over 45 turns.

Illustration of a two-tier AI agent memory design with a fast cache layer above a persistent database layer
AI-generated editorial illustration.

What happens when you tell a travel-planning assistant on Monday that you only fly non-stop and need gluten-free meals, then return on Thursday? Language models start each session with an empty context window, so unless the application stores those facts somewhere, the assistant forgets them. Google Cloud explains one way to build AI agent memory that survives across sessions: a short-term cache in Memorystore for Valkey paired with a long-term store in AlloyDB AI.

This article walks through that design in plain terms, using the travel example from Google Cloud’s post. It covers why the simpler shortcuts fail, how the two tiers divide the work, and what the company’s benchmark numbers do and do not show.

Why do AI agents forget between sessions?

Google Cloud notes that large language models (LLMs) are stateless across sessions. When a user comes back days later, the model sees nothing from before. The application has to rebuild past context itself, or users must repeat their goals every time.

The post describes two common workarounds and where each one breaks down.

  • Context stuffing. The application pastes everything that fits into the prompt on every turn, including raw chat history and tool logs. Google Cloud says token costs multiply with each message, simple prompts can take more than 30 seconds to answer, and the model can overlook key instructions buried in the middle of a very long prompt, a problem often called "lost in the middle."
  • Rolling summaries. An LLM periodically compresses older messages into a short paragraph. Google Cloud describes this as lossy: after several rounds of compression, subtle details get filtered out, and a few turns later the agent can quietly break a constraint the user set earlier.

In the travel scenario, the risk is concrete. The assistant must keep "non-stop flights only" intact even after dozens of messages about sightseeing and itinerary details.

What is a two-tier memory architecture?

A two-tier memory architecture splits what an agent remembers into a fast, temporary layer and a durable, searchable layer. Google Cloud recommends this split for enterprise agents that need to respect guardrails and constraints.

Tier one: the short-term session buffer

The first tier caches the current conversation in memory using a token-bounded sliding window. A sliding window keeps only the most recent messages up to a fixed size, so the prompt stays roughly the same length as the conversation grows. Google Cloud says this also lets a session carry across devices.

Every turn needs a quick read and write, so Google Cloud points to Memorystore for Valkey, an in-memory key-value service, which it describes as offering sub-millisecond, high-throughput lookups. Valkey is an open-source, Redis-compatible data store.

Tier two: long-term persistent memory

The second tier holds facts that should outlast any single conversation: user preferences, rules, and past events. Google Cloud says this layer needs transactional integrity, data governance, and hybrid retrieval, meaning one query that combines structured filters, keyword search, and vector similarity search. It names AlloyDB AI, a PostgreSQL-compatible database with built-in AI functions, as providing these capabilities natively.

Why not keep everything in the database?

Google Cloud acknowledges that both tiers could live in a relational database, but argues the split performs better. Session buffers change on every turn, and handling those constant writes in a relational database would require frequent row deletions and heavy cleanup, which the post calls write amplification and table bloat. The comparison Google Cloud draws is a cache placed in front of a database to absorb frequent lookups of hot rows. That leaves the database free for consistent transactions, hybrid vector search, and long-running queries.

Which four kinds of memory does the design use?

Google Cloud divides agent memory into four types spread across the two tiers.

  • Buffer memory: recent raw conversation turns, stored in Memorystore for Valkey for the length of the active session.
  • Summary memory: a compressed history of older turns, often called compaction, also kept in Memorystore for Valkey across a multi-turn window.
  • Episodic memory: past actions, events, and tool outputs, stored permanently in AlloyDB and retrieved with structured SQL, full-text search, and vector search combined.
  • Entity and rule memory: user preferences, constraints, and vetoes, also stored permanently in AlloyDB with the same hybrid retrieval.

The effect is that rules such as dietary needs live in a durable, queryable store, while the cache holds only the recent back-and-forth. The agent pulls in relevant stored facts when needed instead of carrying raw interaction logs in every prompt.

How do the read and write paths work?

Google Cloud describes two coordinated paths between the application, the cache, and the database.

The read path

When a user asks a question, the application first fetches the active sliding window from Memorystore for Valkey. It then runs query normalization inside the database using an AlloyDB function called ai.generate, and calls ai.hybrid_search to retrieve the matching entity rules and relevant episodic facts. Only those results join the prompt.

The write path

After the agent responds, the turn goes straight into the Valkey cache. A background queue worker then runs asynchronously: it extracts structured entities from the exchange and writes them into AlloyDB. There, Google Cloud says, transactional auto-embeddings compute and store the vector representations immediately. An embedding is a numeric fingerprint of text that lets the database find items with similar meaning.

Google Cloud adds that entity extraction, memory compaction, cross-session memory, and hybrid retrieval can all run inside the database tier, which keeps active prompts short.

A worked example: the travel agent across three days

Applying the design to the scenario in Google Cloud’s post shows how the pieces fit together. This is an illustration of the architecture, not a reported deployment.

  • Day one: a user says they only want non-stop flights and need gluten-free dining. Each message lands in the Valkey buffer. The background worker extracts two rules, a flight preference and a dietary restriction, and stores them in AlloyDB as entity and rule memory.
  • Day one, later: the conversation drifts into sightseeing. Older turns fall out of the sliding window or get compacted into a summary, but the two rules remain in the database untouched.
  • Day three: the user returns with an empty context window. The application normalizes the new question, runs a hybrid search in AlloyDB, and retrieves the stored rules along with relevant past bookings or itinerary decisions. The prompt starts with those facts rather than a replay of the whole history.

The point of the example is where each fact lives. Constraints sit in a transactional store, so they cannot be lost to repeated summarization.

What do the benchmark numbers show?

Google Cloud reports internal benchmark results from multi-turn development dialogues of 45 or more turns with heavy tool use, comparing naive context stuffing against the tiered design. The post’s own note says the figures come from a simulated developer workload that mimics an enterprise AI pair-programming assistant.

  • Prompt size at turn 45: 747,033 tokens with context stuffing versus 83,262 with tiered memory, which Google Cloud calls 88.9% smaller.
  • Response time at turn 45: 33.5 seconds versus 6.7 seconds, described as 80.0% faster.
  • Per-turn wait: 4.2 to 6.7 seconds with the tiered design, a 36% to 80% reduction.
  • Cumulative session tokens: 17.9 million versus 4.09 million, a 72.0% saving in tokens and cost.
  • Rule and constraint recall: Google Cloud says recall degrades over turns with context stuffing and does not degrade with tiered memory.

These results come from one vendor’s own test of a coding-assistant workload. The headline claim of up to 70% lower token spend matches the 72% cumulative figure, but the post does not describe results for travel, customer support, or other agent types, so savings elsewhere may differ.

How does this compare with other approaches?

The comparison Google Cloud draws is against the two shortcuts it describes: context stuffing and rolling summaries. Context stuffing is simple but grows costly and slow. Rolling summaries trim the prompt but lose detail. The tiered design instead keeps raw recent turns in a cache and moves durable facts into a database that can be queried precisely.

The post presents the design as a matter of pairing an in-memory cache with a database, a pattern it likens to ordinary application caching. It does not compare AlloyDB and Memorystore against other vendors’ databases or against dedicated agent-memory products, and this article does not either.

Where to stay cautious in practice

The benchmark is vendor-run and tied to a developer-assistant workload, so token and latency savings in a different application should be measured rather than assumed. The design also adds moving parts: a cache, a database, and a background worker that extracts facts, and any of them can drift out of step if extraction misses or misreads a rule. Teams considering this pattern should check current Google Cloud documentation for service availability, pricing, and the exact behavior of the AlloyDB AI functions before relying on them.

The open question: extraction quality

The design’s durability depends on the background worker correctly pulling structured rules out of messy conversation. Google Cloud’s post reports that recall does not degrade in its benchmark, but a missed or mistaken extraction would mean a constraint never reaches the permanent store. How often that happens outside the tested workload is not something the published figures answer.

Sources