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
conversationId.Better responses
Ownership
Pluggable architecture
Available history stores
RedisHistoryStore
MongoHistoryStore
Custom HistoryStore
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
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
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
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.
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
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});