Build a Token Counter and Cost Estimator with Streamlit and OpenAI

Tony Esposito
3 min readSep 25, 2023

--

In the world of AI, keeping track of computational resources is crucial. When working with language models like OpenAI’s GPT-3.5 turbo, GPT-4 and Anthropic Claude V2 knowing the number of tokens processed can give you a good estimate of the cost involved. In this tutorial, we’ll build a simple web application using Streamlit to count the tokens in a given text and estimate the cost for different OpenAI models.

Setting Up The Environment

Before diving into the coding part, ensure you have the necessary libraries installed. Run the following command to install Streamlit and OpenAI’s Python library:

```bash
pip install streamlit openai
```

Also, make sure to have your OpenAI API credentials which you’ll get when you sign up for access to the GPT-4 API on OpenAI’s website.

Building The Application

Start by creating a new Python script (e.g., `app.py`). Below is the code snippet for our application:

```python
import streamlit as st
import openai

# Set your OpenAI API key
openai.api_key = ‘your-api-key-here’

def count_tokens(text):
# Estimate the number of tokens in the text without making an API call
return len(text.split())

def calculate_cost(tokens, cost_per_token):
return tokens * cost_per_token

# Define the models and their cost per token
models = {
‘Claude V2’: 0.002, #Please check the correct price per token
‘GPT-4’: 0.003, #Please check the correct price per token
‘GPT-3.5’: 0.001 #Please check the correct price per token
}

st.title(‘Token Counter and Cost Estimator’)

# Create a dropdown menu in the sidebar to select a model
model_choice = st.sidebar.selectbox(“Select a model:”, options=list(models.keys()))

# Get the cost per token for the selected model
cost_per_token = models[model_choice]

user_input = st.text_area(“Enter your text here:”)

if user_input:
tokens = count_tokens(user_input)
cost = calculate_cost(tokens, cost_per_token)

st.write(f’Estimated Tokens: {tokens}’)
st.write(f’Estimated Cost for {model_choice}: ${cost:.2f}’)
```

Understanding the Code

Here’s a breakdown of what the code does:

1. Import Necessary Libraries: We import `streamlit` and `openai`.
2. API Credentials: We set the OpenAI API key.
3. Helper Functions:
— `count_tokens`: A simple function to estimate the number of tokens based on spaces.
— `calculate_cost`: Calculates the cost based on the number of tokens and cost per token.
4. Model Selection: We define a dictionary with models and their respective costs per token, and create a dropdown menu for model selection.
5. User Input: We create a text area for user input.
6. Token Count and Cost Estimation: On user input, we estimate the number of tokens and the cost based on the selected model.

Running The Application

Now that our script is ready, it’s time to run the app. Execute the following command in your terminal:

```bash
streamlit run app.py
```

This command will launch a local server and open your web browser displaying the app. Now you can paste your text, select a model, and get an estimate of the number of tokens and cost!

Conclusion

This simple application demonstrates how to build interactive tools with Streamlit and integrate them with OpenAI’s models. Such tools can be very useful in managing and estimating the costs involved in processing text with language models. The sky’s the limit from here — you can extend this app with more features like real-time cost estimation, support for other language models, or even a live preview of the text generation!

Tony

--

--

Tony Esposito

Generative AI SME, Conversational AI systems specialist