梦兽编程
AI_SUITE

State Read/Write: Managing Agent Internal State

A detailed explanation of the State concept in ADK Go, the read/write APIs, and how to maintain and share custom state data across multi-turn conversations.

Session manages conversation history; State manages the Agent’s internal state. Together, they let the Agent remember both what was discussed and its current state across multi-turn conversations.

State vs Session

ConceptContentLifecycle
SessionConversation history messagesEntire session
StateCustom key-value dataDefined as needed, can span Sessions

Session is managed by the framework; State is defined by the developer.

Basic State Operations

Writing State

session.State(ctx).Set("user_name", "Zhang San")
session.State(ctx).Set("preference", map[string]interface{}{
    "language": "Chinese",
    "notifications": true,
})

Reading State

name := session.State(ctx).Get("user_name")  // "Zhang San"
pref := session.State(ctx).Get("preference") // map[string]interface{}{...}

Deleting State

session.State(ctx).Delete("user_name")

Use Cases

Use Case 1: Remembering User Info

// Turn 1: user says "My name is Zhang San"
session.State(ctx).Set("user_name", "Zhang San")

// Later turns: the Agent knows the user's name
func agentInstruction() string {
    name := session.State(ctx).Get("user_name")
    if name != nil {
        return fmt.Sprintf("The user's name is %s, address them by name", name)
    }
    return "Ask the user for their name in a friendly way"
}

Use Case 2: Multi-Step Flow State

// User is in a ticket-booking flow
session.State(ctx).Set("booking_step", 1)  // choose destination
session.State(ctx).Set("destination", "Shanghai")

// User finishes step 1 and enters step 2
session.State(ctx).Set("booking_step", 2)  // choose date
session.State(ctx).Set("travel_date", "2026-06-01")

Use Case 3: Sharing Data Between Tools

When two Tools need to share data, use State as an intermediary:

// Tool A: query weather and cache the result
session.State(ctx).Set("cached_weather", weatherResult)

// Tool B: read the cache directly without re-querying
weather := session.State(ctx).Get("cached_weather")

State Persistence

By default, State is stored in memory and cleared when the Session ends. If you need persistence, use it together with Session persistence:

store := sessions.NewRedisStore(redisClient, "sessions:", time.Hour*24)

session, err := sessions.NewSession(ctx,
    sessions.WithUserID("user-123"),
    sessions.WithAgentID("my-agent"),
    sessions.WithStore(store),
)

// State is automatically persisted along with the Session
session.State(ctx).Set("persistent_data", "value")

FAQ

Q: Is State thread-safe? A: Yes. The object returned by Session.State() is thread-safe, so multiple Tools can read and write concurrently without issues.

Q: What happens if State data is too large? A: State data is transferred and stored along with the Session. Large data will affect performance. Keep State data in the KB range and avoid storing large raw payloads.

Q: If the Session expires, is State still there? A: State follows the Session. When the Session expires, State is also cleared. For long-term storage, save the data to an external database.


Next Step

State manages internal state. Next, learn about the Event system—understanding the event flow during Agent execution and how to listen for and handle events.

Session Management | Event System →


Want to learn more hands-on Go ADK? Follow the “Full Stack Summit — Dream Beast Programming” WeChat official account for weekly Go / AI programming tutorials.

Frequently Asked Questions

What is State in ADK Go?

State is custom data that an Agent can read and write during a conversation. Unlike Session context, which stores the conversation history, State holds structured application data that the Agent needs across turns.

How do I read or write State?

Use the State API exposed by ADK Go, typically through methods like session.State().Get(key) and session.State().Set(key, value).

What is the difference between Session and State?

Session maintains the conversation history and turn context, while State stores custom application data that the Agent uses to remember facts, settings, or progress between turns.

Can State be shared between different Agents?

Yes, if the Agents use the same State backend or storage, they can share State. Otherwise, State is scoped to the Agent or Session that created it.

When should I use State instead of Session context?

Use State when you have structured, reusable data such as user preferences, counters, or business entities that are referenced repeatedly, rather than burying them in free-form conversation text.