typescriptadvanced
AI Chat Conversation Memory
Manage conversation history with token limits, summarization, and sliding window for LLM chat apps.
typescriptPress ⌘/Ctrl + Shift + C to copy
interface Message {
role: 'system' | 'user' | 'assistant';
content: string;
}
class ConversationMemory {
private messages: Message[] = [];
private maxMessages: number;
private systemPrompt: string;
constructor(systemPrompt: string, maxMessages = 20) {
this.systemPrompt = systemPrompt;
this.maxMessages = maxMessages;
}
add(role: 'user' | 'assistant', content: string): void {
this.messages.push({ role, content });
if (this.messages.length > this.maxMessages) {
this.messages = this.messages.slice(-this.maxMessages);
}
}
getContext(): Message[] {
return [
{ role: 'system', content: this.systemPrompt },
...this.messages,
];
}
summarize(summary: string): void {
this.messages = [
{ role: 'system', content: `Previous conversation summary: ${summary}` },
...this.messages.slice(-4),
];
}
clear(): void {
this.messages = [];
}
get length(): number {
return this.messages.length;
}
}
const memory = new ConversationMemory('You are a helpful assistant.', 20);
memory.add('user', 'What is TypeScript?');
memory.add('assistant', 'TypeScript is a typed superset of JavaScript.');
console.log(memory.getContext());Use Cases
- Building chatbot applications with context
- Managing token limits in LLM conversations
- Multi-turn dialogue systems
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
LangChain Conversation with Memory
Maintain conversation context across turns using LangChain memory modules.
Best for: Multi-turn chatbots
#ai#langchain
pythonintermediate
LangChain Conversation with Memory
Build a stateful chatbot that remembers conversation history using LangChain memory.
Best for: stateful chatbots
#langchain#memory
pythonbeginner
Stream LLM Chat Responses
Stream OpenAI chat completions token-by-token for real-time UI updates.
Best for: Chat UIs
#ai#streaming
pythonadvanced
LangChain Agent with Persistent Memory
Build an AI agent that persists conversation context across sessions using LangChain memory stores.
Best for: persistent agents
#langchain#agent