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
| Concept | Content | Lifecycle |
|---|---|---|
| Session | Conversation history messages | Entire session |
| State | Custom key-value data | Defined 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.
