# Practical and Useful Patterns with ADK
State manages your agent's "current situation." With dynamic prompts and scoped data management, build truly intelligent conversations 🎯
📌 **Title**: State
🔗 **URL**:
## 🧩 Overview
State is the working memory accessible via `session.state`. It holds "data needed right now in this conversation" -- like shopping cart contents or information gathered step by step during a booking flow.
State's key feature is **prefix-based scoping**:
- `app:` - Data shared across the entire app (global settings for all users)
- `user:` - Data shared per user (user preferences, persisted across sessions)
- `temp:` - Temporary data valid only within the current session
- No prefix - Standard session scope
Additionally, embedding `{state_var}` in agent instructions enables dynamic prompts that adapt in real time.
## 🛠 How to Use
Set values on `session.state` using dictionary-style access -- for example, `session.state["booking_date"] = "2026-07-15"`, `session.state["party_size"] = 4`, and `session.state["seat_type"] = "window"` for standard session-scoped data. For scoped state, use the `user:` prefix (e.g., `"user:preferred_language"`) for per-user persistent data, `temp:` (e.g., `"temp:search_results"`) for data that disappears when the session ends, and `app:` (e.g., `"app:maintenance_mode"`) for data shared across all users. Additionally, embed placeholders like `{booking_date}`, `{party_size}`, and `{user:preferred_language}` in the `Agent`'s `instruction` parameter to dynamically inject state values into the agent's prompt at runtime.
## 🏗 Practical Usage
**Restaurant booking flow:**
Gather information progressively through conversation, accumulating in State:
1. "I'd like a reservation for 4 tomorrow evening" → Save `booking_date`, `party_size` to State
2. "Window seat please" → Add `seat_type`
3. "I'm allergic to nuts" → Add `allergies`
4. At final confirmation, pull all info from State to generate a confirmation message
With `{booking_date}` embedded in instructions, the agent always responds with awareness of the latest booking status.
**Persisting user preferences:**
Using the `user:` prefix, carry forward information like "this person prefers English" or "this person is vegetarian" even when sessions change.
## 💡 Use Cases
- 🛒 E-commerce: Manage cart contents in State. `temp:` for search filters, `user:` for shipping address
- ✈️ Travel booking: Progressively accumulate dates, party size, seat type in State
- 🤖 Personalization: Learn and persist user preferences via `user:` scope
- 📊 Dashboards: Share system-wide status via `app:` scope
## ⚠️ Caveats
- Storing large amounts of data in State degrades performance. Consider Artifacts for bulky data
- Changes to `app:` scope affect all users. Use with caution
- When embedding `{state_var}` in instructions, unset values may display the placeholder as-is. Provide defaults
- Establish key naming conventions to avoid collisions
✨ Leverage State's scoped management and dynamic prompts to build context-aware, intelligent agents. Perfect for booking flows and personalization!
#
ADK# #
AIAgent#