Telygent

History Store

What is a History Store?

A History Store is the SDK storage layer for conversation messages. The SDK reads from it before each request and appends new user/assistant messages after each response. This is what enables conversation continuity.

Why it matters

Without a history store, each prompt behaves like a fresh session. With a history store, users can resume threads naturally and your assistant can reason with prior context.

Benefits

Conversation resume

Users can leave and continue later with the same conversationId.

Better responses

Prior Q/A context improves coherence and follow-up handling.

Ownership

Conversation storage lives in your infrastructure (Redis/Mongo/custom DB), not in SDK memory.

Pluggable architecture

Swap storage backend without changing request flow in your controllers.

Available history stores

RedisHistoryStore

Fast, TTL-based storage. Good for active sessions and low-latency chat.

MongoHistoryStore

Persistent storage in your Mongo database for long-lived conversation history.

Custom HistoryStore

Implement HistoryStore to use Postgres, DynamoDB, or any internal system.

Note: Saving conversations to a db store instead of redis will allow you to save conversations and reload them on demand for your users. You can have a collection/table that saves the conversationId you generate and the user the conversation belongs to, when the user decides to start a new conversation you create a new entry with a new conversationId and when the user wants to continue an existing conversation, you call the getConversation function with the existing conversationId and the Telygent Ai-sdk will fetch the previous conversation from the 'telygent_conversations' or your specified collection/table.

Usage

Pick one store implementation and pass it to the AI client.

Redis store

history-redis.ts
1import {createRedisHistoryStore} from "@telygent/ai-sdk"; 2 3const historyStore = createRedisHistoryStore({ 4 url: process.env.REDIS_URL as string, 5 ttlSeconds: 3600, 6 prefix: "telygent:conv:", 7});

Mongo store

history-mongo.ts
1import {MongoClient} from "mongodb"; 2import {createMongoHistoryStore} from "@telygent/ai-sdk"; 3 4const mongoClient = new MongoClient(process.env.MONGO_URI as string); 5await mongoClient.connect(); 6const mongoDb = mongoClient.db(process.env.MONGO_DB_NAME as string); 7 8const historyStore = createMongoHistoryStore({ 9 db: mongoDb, 10 collectionName: "telygent_conversations", // optional 11}); 12 13// Recommended index: 14// db.telygent_conversations.createIndex({conversationId: 1, timestamp: 1});

Attach to client

client.ts
1import {createAiClient} from "@telygent/ai-sdk"; 2 3const client = createAiClient({ 4 apiKey: process.env.TELYGENT_API_KEY as string, 5 registry, 6 dbAdapter, 7 historyStore, // Redis, Mongo, or custom 8}); 9 10const response = await client.query({ 11 question: "Show me my recent transactions", 12 conversationId: "conv_123", 13 userContext: {userId: "user_1"}, 14});

Custom store contract

If you need another provider, implement the HistoryStorecontract. Minimum methods are getMessages and appendMessage.

custom-store.ts
1import {type ConversationMessage, type HistoryStore} from "@telygent/ai-sdk"; 2 3class CustomHistoryStore implements HistoryStore { 4 async getMessages(conversationId: string): Promise<ConversationMessage[]> { 5 // Load from your database and return timestamp ASC 6 return []; 7 } 8 9 async appendMessage(conversationId: string, message: ConversationMessage): Promise<void> { 10 // Persist role, content, timestamp and optional metadata 11 } 12}

Attach custom store to client

custom-client.ts
1import {createAiClient} from "@telygent/ai-sdk"; 2 3const historyStore = new CustomHistoryStore(); 4 5const client = createAiClient({ 6 apiKey: process.env.TELYGENT_API_KEY as string, 7 registry, 8 dbAdapter, 9 historyStore, 10});

Next steps