I am building a Chat based AI Agent and want to limit the number of questions a user asks per day (e.g 10 questions)
If I put condition based on chat session id it does not work as if a user refreshes the chat he gets a new chat id.
I am building a Chat based AI Agent and want to limit the number of questions a user asks per day (e.g 10 questions)
If I put condition based on chat session id it does not work as if a user refreshes the chat he gets a new chat id.
I would just use a code node in order to track the number of messages and then an if block, for example if i was using the chat node as input this code would record the number of messages:
const currentDate = new Date().toISOString().split('T')[0];
const state = $session.get('questionState') || { count: 0, lastDate: currentDate };
// Reset count if the day changed
if (state.lastDate !== currentDate) {
state.count = 0;
state.lastDate = currentDate;
}
// Increment count
state.count += 1;
// Save updated state
$session.set('questionState', state);
// Check if user exceeded the limit
if (state.count > 10) {
return [{ json: { message: "You've hit your daily limit of 10 questions. Try again tomorrow!" }, endFlow: true }];
}
return items;
This will track the number of messages sent through the chat nod.
What if the same user initiates a new session after 10 questions?
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.