New
Session Memory
Automatic conversation persistence within a session. Your AI remembers the entire conversation context.
Overview
Session memory automatically stores and retrieves conversation history within a session. When you provide a sessionId, the AI has access to all previous messages in that session.
Basic Usage
session-memory.ts
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://www.superagentstack.com/api/v1',
apiKey: process.env.OPENROUTER_KEY,
defaultHeaders: { 'superAgentKey': process.env.SUPER_AGENT_KEY },
});
// Generate a unique session ID (e.g., UUID)
const sessionId = crypto.randomUUID();
// First message
await client.chat.completions.create({
model: 'openai/gpt-4o-mini',
messages: [{ role: 'user', content: 'I am working on a React project.' }],
sessionId: sessionId,
saveToMemory: true,
});
// Second message - AI remembers the context
const response = await client.chat.completions.create({
model: 'openai/gpt-4o-mini',
messages: [{ role: 'user', content: 'What testing library should I use?' }],
sessionId: sessionId,
saveToMemory: true,
});
// AI knows you're working on React and suggests React Testing LibraryParameters
| Parameter | Type | Required | Description |
|---|---|---|---|
sessionId | string (UUID) | Yes | Unique identifier for the session. Use UUID format. |
saveToMemory | boolean | No | Whether to save this message to memory. Default: true when sessionId is provided. |
Session ID Format
Use UUID format for session IDs (e.g.,
crypto.randomUUID()). Non-UUID formats may cause memory operations to fail.Best Practices
- Use consistent session IDs for the same conversation
- Generate new session IDs for new conversations
- Store session IDs in your application state or database
- Consider session expiration for long-running applications
With Streaming
streaming-memory.ts
// Session memory works with streaming too
const stream = await client.chat.completions.create({
model: 'openai/gpt-4o-mini',
messages: [{ role: 'user', content: 'Tell me a story' }],
sessionId: sessionId,
saveToMemory: true,
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}