What is ctx.memory?
ctx.memory is a per-session key-value store backed by Redis. It is automatically available when the request comes from an active conversation (has a socketId) and the company has a workflow API key configured.
Typical use cases:
- Multi-step flows — remember which step the user is on
- Shopping carts — accumulate products during the conversation
- Counters — limit login attempts, retry tracking
- Temporary preferences — language, format, filters during the session
Quick start
index.ts
Check availability
Primitives vs JSON
Useset()/get() for simple values and setJson()/getJson() for objects:
get() matches the type of the default value:
TTL (time to live)
All values expire automatically. TTL is specified in seconds.| Method | TTL | Maximum |
|---|---|---|
set() | Optional | 86,400 (24h) |
setJson() | Required | 86,400 (24h) |
Limits
| Restriction | Value |
|---|---|
Max length of set() | 255 characters |
| Max TTL | 86,400 seconds (24h) |
| Scope | Per session (socketId) |
set() that exceed 255 characters throw an Error. For larger data, use setJson().
Common patterns
- Multi-step flow
- Shopping cart
- Attempt limit
Error handling
When to use ctx.memory vs a database?
ctx.memory | External database | |
|---|---|---|
| Scope | Per session (socketId) | Global |
| Persistence | Temporary (max 24h) | Permanent |
| Setup | Zero — included | Requires connection string in secrets |
| Ideal for | Conversation state, temporary cache | User data, history, config |