Using the Paid Version of ChatGPT Affordably via API#

Background/Problem#

To use the paid version of Chat GPT offered by OpenAI, a monthly subscription fee of $20 is required.
(As of June 2024, this amounts to approximately 30,000 KRW when applying the exchange rate for South Korea.)
Paying $20 every month can feel expensive for those with low usage.

Improvement#

To address this issue, I will explain a simple coding method that allows for pay-as-you-go usage.
OpenAI provides an API for the Chat Bot, which operates on a pay-as-you-go basis.
Therefore, if your monthly usage is low, using the API can be more cost-effective than subscribing.

1. Loading Funds and Obtaining an API Key#

You need to add funds first before using the API, and the usage will be deducted from your balance.
Please visit the following website to load funds and obtain your API key.

2. Implementing the chat() Function#

Install the package by running pip install openai.
You can create an OpenAI instance with client = OpenAI(api_key='YOUR_API_KEY'), and enter the API key you obtained.
The chat_history list is where the question/answer history is stored. If you do not save the history here, previous questions will not be remembered.
If you want to clear the previous questions, call chat_clear().
The chat(model, prompt) function contains the implementation for posing questions to the ChatGPT model.

from openai import OpenAI

client = OpenAI(api_key='YOUR_API_KEY')

chat_history = []

def chat_clear():
    global chat_history
    chat_history = []

def chat(model, prompt):
    msg=[ {"role": "system", "content": "You are a helpful assistant."} ]

    for element in chat_history:
        msg.append(element)

    msg.append({"role": "user", "content": prompt})
    
    response = client.chat.completions.create(
      model=model,
      messages=msg,
    )

    print(response.usage)
    print('\n')
    print(response.choices[0].message.content)

    chat_history.append({"role": "user", "content": prompt})
    chat_history.append({"role": "assistant", "content": response.choices[0].message.content})

3. Asking Questions and Receiving Answers Using the chat() Function#

Let’s ask Chat GPT a question using the chat() function as follows.
First, select the model to use as the first argument, and enter your question as the second argument.
You can check the types of models available on the pricing page.

chat('gpt-4-turbo', '1+1 ?')

If you ask a question as shown above, you can get an answer like the one below.

CompletionUsage(completion_tokens=8, prompt_tokens=21, total_tokens=29)

1 + 1 equals 2.

The information displayed on the first line indicates the API usage.

  • completion_tokens = output tokens
  • prompt_tokens = input tokens
  • total_tokens = input + output tokens

Therefore, you can calculate the usage cost by comparing the token usage above with the rates specified on the pricing page.
If you continuously accumulate unnecessary question/answer history in the chat_history list, you will incur unnecessary charges. So, it’s important to use chat_clear() appropriately.