How to track usage per user across sessions?

Hi, I am currently working on an app using Langchain and OpenAI API. I am planning to deploy it publicly later.

In the app, I let the user have the choice between:

  • using my API key
  • using their own API key

I did this so that people (friends, family, non-tech people) can try my app without having to enter an API key.

This is working well. However, I want to avoid users using all my credits, so I would like to set a limit per user. For example, a widget could be triggered at most x times per minute, y times per hour, z times per day, for a given user.

Do you have any idea how I could set this up?

I was first thinking of putting these limits per session (storing the user history in the session state), but I believe people could just reload the app to reset the session.

Thank you!

Hello @daltunay, welcome to our forums! :raised_hands:

I’ve created a Streamlit app that counts OpenAI credits and records the daily credit count to a Google Sheet:

Here’s the app:

Feel free to have a play and fork the repo to adapt it to your own needs:

Feedback is also welcome! :slight_smile:

Best wishes,
Charly

Thank you, I will try it out!