Merk
Tilgang til denne siden krever autorisasjon. Du kan prøve å logge på eller endre kataloger.
Tilgang til denne siden krever autorisasjon. Du kan prøve å endre kataloger.
Use AgentSession to keep conversation context between invocations.
When a session uses service-managed storage, it can contain an opaque service-side session ID. OpenAI Responses and Conversations IDs are scoped to the backing API key or project by default; if a hosted agent uses the same key or project for multiple end users, store those IDs server-side and verify the authenticated user or tenant before resuming. For details, see Session.
Core usage pattern
Most applications follow the same flow:
- Create a session (
CreateSessionAsync()) - Pass that session to each
RunAsync(...) - Rehydrate from serialized state (
DeserializeSessionAsync(...)) - Continue with a service conversation ID (varies by agent, e.g.
myChatClientAgent.CreateSessionAsync("existing-id"))
- Create a session (
create_session()) - Pass that session to each
run(...) - Rehydrate by service conversation ID (
get_session(...)) or from serialized state
- Create a session (
CreateSession(...)) - Pass that session to each
RunText(...)withagent.WithSession(session) - Rehydrate from serialized state with
json.Unmarshal(...)intoagent.Session
The Go agent package provides the core types for conversation state: agent.Session for key-value state tied to a conversation and agent.ContextProvider for context injection and persistence.
// Create and reuse a session
AgentSession session = await agent.CreateSessionAsync();
var first = await agent.RunAsync("My name is Alice.", session);
var second = await agent.RunAsync("What is my name?", session);
// Persist and restore later
var serialized = agent.SerializeSession(session);
AgentSession resumed = await agent.DeserializeSessionAsync(serialized);
# Create and reuse a session
session = agent.create_session()
first = await agent.run("My name is Alice.", session=session)
second = await agent.run("What is my name?", session=session)
# Rehydrate by service conversation ID when needed
service_session = agent.get_session(service_session_id="<service-conversation-id>")
# Persist and restore later
serialized = session.to_dict()
resumed = AgentSession.from_dict(serialized)
session, err := a.CreateSession(ctx)
if err != nil {
panic(err)
}
Use a session for multi-turn conversations
resp, _ := a.RunText(ctx, "My name is Alice.", agent.WithSession(session)).Collect()
resp, _ = a.RunText(ctx, "What is my name?", agent.WithSession(session)).Collect()
Persist sessions
Sessions can be serialized to JSON for storage and later resumed:
data, err := json.Marshal(session)
if err != nil {
panic(err)
}
// store data...
// later:
var resumed agent.Session
if err := json.Unmarshal(data, &resumed); err != nil {
panic(err)
}
resp, err := a.RunText(ctx, "Continue from where we left off.", agent.WithSession(&resumed)).Collect()
Guide map
| Page | Focus |
|---|---|
| Session | AgentSession structure and serialization |
| Context Providers | Built-in and custom context/history provider patterns |
| Context Compaction | Efficiently manage conversation growth |
| Storage | Built-in storage modes and external persistence strategies |