Session & MemoryStore
The Session and MemoryStore primitives in Kikoyu SDK manage multi-turn conversation turn history across multiple separate Runner.run() invocations.
Why Storage-Agnostic Memory?
AI agents in production need to retain user turn history across HTTP requests, server restarts, or distributed serverless instances (Next.js, AWS Lambda, Cloudflare Workers).
Kikoyu SDK provides a storage-agnostic architecture:
Session: The session wrapper representing a unique user conversation turn log.MemoryStore: The pluggable storage engine interface (InMemoryStore, Redis, PostgreSQL, DynamoDB).
Basic Usage with InMemoryStore
typescript
Building a Custom Database MemoryStore (Redis / Postgres)
To persist session state in external databases like Redis or PostgreSQL, implement the simple MemoryStore interface:
typescript
Managing Turn History & Sliding Windows
To prevent hitting LLM context window limits during long conversations, you can trim or clear turn history using Session helpers:
typescript
Best Practices
- Use Unique Session IDs: Key session IDs by composite tenant keys (e.g.
tenantId:userId:chatId) to prevent multi-tenant data bleed. - Implement Database TTLs: Automatically expire inactive sessions after 7–30 days in your storage database.
- Pass Session to Runner: Always pass
{ session }inrunner.run(agent, prompt, { session })for continuous multi-turn flows.