Session Management
Sessions persist conversation history across multiple Chat() calls.
Setup
import "github.com/joakimcarlsson/ai/agent/session"
myAgent := agent.New(llmClient,
agent.WithSystemPrompt("You are a helpful assistant."),
agent.WithSession("conversation-id", session.FileStore("./sessions")),
)
Built-in Stores
// Persistent JSON files
store := session.FileStore("./sessions")
// In-memory (ephemeral, lost on restart)
store := session.MemoryStore()
Database Stores
Ready-to-use stores for production backends:
- PostgreSQL —
postgres.SessionStore(ctx, connString) - SQLite —
sqlite.SessionStore(ctx, db)
Store Interface
Implement this interface to use any backend:
type Store interface {
Exists(ctx context.Context, id string) (bool, error)
Create(ctx context.Context, id string) (Session, error)
Load(ctx context.Context, id string) (Session, error)
Delete(ctx context.Context, id string) error
}