tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Agentic AI > ADK Agent Testing > Detecting Session State Bloat in ADK Agents

Detecting Session State Bloat in ADK Agents

Author: Venkata Sudhakar

ADK agents that accumulate unbounded session state during a conversation can cause memory spikes, serialization timeouts, or crashes during long ShopMax India customer sessions. State bloat happens when every tool call result, order lookup, or history entry is appended to the state dict without any pruning strategy. Testing state size growth across conversation turns catches bloat before it causes production incidents.

The test serializes the agent state dictionary to JSON and measures its byte size after each simulated turn. Two assertions enforce correctness: the total state size must stay under MAX_STATE_SIZE_BYTES at all times, and the growth rate in the second half of the conversation must not exceed the growth rate in the first half. A flat or declining late-growth rate confirms the pruning strategy is working as expected.

The example below simulates 20 turns of a ShopMax India customer session, applies a 10-message sliding window to keep history bounded, and asserts that state size stabilizes after the first 5 turns.


It gives the following output,

Size at turn 1: 239 bytes
Size at turn 10: 1502 bytes
Size at turn 20: 1502 bytes
Early growth: 1263, Late growth: 0
. (1 passed in 0.01s)

In production, call state_size_bytes after every agent turn and emit the value as a metric to your monitoring system. Set an alert if the size approaches MAX_STATE_SIZE_BYTES so the on-call team is notified before an actual overflow. If the pruning strategy involves more than just history, extend update_state to also cap the pending_actions list and evict stale cached tool results so the state stays lean across all fields.


 
  


  
bl  br