Register and share your invite link to earn from video plays and referrals.

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture 投稿は個人の意見です。
Joined May 2026
258 Following    228 Followers
# Useful but Little-Known Features of ADK 2.0 🌍 How do you keep track of user preferences, intermediate results, and shared data across agent conversations? ADK 2.0's State system makes it straightforward. ADK 2.0's State feature is a key-value scratchpad for persisting and sharing data within and across sessions. Different prefixes control the scope of each piece of state. 📌 Title: State Management 🔗 URL: 🧩 Overview State is a key-value data store with four scopes determined by prefixes. No prefix means session scope (valid only within the current session). The user: prefix shares state across multiple sessions for the same user. The app: prefix makes state global across all users and sessions. The temp: prefix creates temporary state that is discarded after the invocation ends. You can reference state values in agent instructions using the {key} syntax, enabling dynamic prompt construction. 🛠 How to use it There are several ways to write state values. ```python # 1. Auto-save agent output with output_key agent = Agent( name="summarizer", output_key="last_summary", ... ) # 2. Explicitly set via EventActions.state_delta from import EventActions actions = EventActions(state_delta={"user:preference": "dark_mode"}) # 3. Set from within tools via ToolContext def my_tool(query: str, tool_context: ToolContext) -> str: tool_context.state["app:global_counter"] = 42 tool_context.state["temp:intermediate"] = "temporary_value" return "done" ``` Reference state in instructions like this: ```python agent = Agent( instruction="User preference is {user:preference}. Previous summary: {last_summary}", ... ) ``` 🏗 Building it into production ・Choose the right scope for each piece of data — use temp: for throwaway intermediate results ・Design user: and app: scoped state carefully, as changes affect multiple sessions ・Always read and write state through CallbackContext or ToolContext to ensure event tracking ・Establish consistent naming conventions for state keys across your team 💡 Use cases 👤 Persist user preferences across sessions with the user: prefix 📊 Track application-wide statistics and counters with app: prefix 🔄 Automatically reference the previous agent output in the next step via output_key 🧹 Store intermediate computation results temporarily with temp: to keep sessions clean ⚠️ Watch out Do not directly modify session.state outside of a context. Bypassing CallbackContext or ToolContext skips event tracking, meaning state changes won't be recorded in the event history. This can break rewind functionality and make debugging significantly harder. ✨ By leveraging State's four scopes, you can flexibly manage agent memory and context. Proper state management is the foundation of high-quality agent experiences. #ADK# #AIAgent#
Show more