tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > Google Gemini API > ADK Stateful Agents - Managing State Between Turns

ADK Stateful Agents - Managing State Between Turns

Author: Venkata Sudhakar

ADK session state lets your agent store and retrieve structured data across conversation turns within the same session. This goes beyond the conversation history (which is plain text) � session state is a typed key-value store where you can persist numbers, lists, dictionaries, and any serialisable Python object. A shopping cart agent can add items to state on turn 2, check the cart on turn 4, and process checkout on turn 6, all without the user repeating what they added. State is scoped to the session so different users get separate isolated state stores.

Tools access session state via the ToolContext object passed as the last parameter. Reading state uses tool_context.state.get(key, default). Writing state uses tool_context.state[key] = value. State changes are automatically persisted by the session service between turns � no manual save needed. When deployed to Agent Engine, session state persists in managed cloud storage. Locally it persists in InMemorySessionService for the lifetime of the process.

The below example builds a multi-turn shopping cart agent where the agent accumulates items across turns, calculates running totals, and processes checkout � all using session state to remember what has been added.


Running a multi-turn shopping session to show state persisting across turns,


It gives the following output showing cart state persisting across all turns,

Turn 1: Added Samsung 65-inch QLED TV to your cart! Cart total: Rs 89,990.

Turn 2: Added 2x AirPods Pro to your cart. Running total is now Rs 1,39,780.

Turn 3: Your cart has 2 items:
        1. Samsung 65-inch QLED TV x1 - Rs 89,990
        2. AirPods Pro 2nd Gen x2    - Rs 49,800
        Total: Rs 1,39,790

Turn 4: Added iPhone 15 Pro to your cart.
        Order confirmed! Order ID: ORD-84291
        Items ordered: 3 | Total paid: Rs 2,74,690
        Thank you for shopping with ShopMax!

# Cart state persisted across all 4 turns automatically
# tool_context.state["cart"] survived between each runner.run() call
# Clearing state on checkout prevents stale data in the next session

Session state best practices: keep state values small and serialisable � lists and dicts of strings and numbers work best. Avoid storing large objects or binary data in state. Always provide a default when reading state with .get(key, default) since the key may not exist on the first turn. Clear state explicitly when a workflow completes (like clearing the cart after checkout) rather than letting it accumulate indefinitely. For production on Agent Engine, state is persisted in managed cloud storage automatically � the same ToolContext pattern works without any code changes between local and cloud deployment.


 
  


  
bl  br