Skip to main content
Practice

Understanding the Role of Tokens to Reduce Costs

In a previous lesson, we discussed tokens, which are the meaningful units obtained by splitting a sentence into words, punctuation, numbers, and more.

Reference: How Generated AI Understands Prompts

AI models, like ChatGPT, process text at the token level to comprehend and generate sentences.

For instance, the sentence Hello, my name is CodeFriends! is tokenized as follows:

Example of Sentence Tokenization
- Hello
- ,
- my
- name
- is
- Code
- Friends
- !

AI models statistically understand the relationships between these tokens, generating new text based on the input prompt.


Cost Structure Calculation in Generative AI Models

Generative AI calculates costs by considering both input tokens for prompt understanding and output tokens for response generation.

Suppose the cost structure of a particular model is $0.02 per 1,000 tokens.

If 400 tokens are needed to understand a specific prompt and 1,100 tokens to generate a response, the total cost is calculated as follows:

  • Input Tokens: 400 tokens x $0.02 / 1,000 tokens = $0.008

  • Output Tokens: 1,100 tokens x $0.02 / 1,000 tokens = $0.022

Therefore, the total cost is $0.008 (input) + $0.022 (output) = $0.030.


Prompt Writing Techniques to Reduce Costs

To decrease the number of tokens needed for desired responses, it’s advisable to write prompts as follows:

Generally, more output tokens are consumed compared to input tokens, so optimizing output tokens is particularly important.

  1. Control Output Length: Limit the output length of AI's response to ensure it contains only the necessary information within the restricted length.
Example of Controlling Output Length
Explain the causes of accelerating global low birthrates in under 200 characters.
  1. Specify Response Format: Without specifying the response format, AI may generate responses that are too long for your needs.

Therefore, it is beneficial to provide the desired response format with examples.

Example Including Response Format
Please explain the causes of accelerating global low birthrates according to the format below:

1. Economic Factors: [within 100 characters]

2. Social Change Factors: [within 100 characters]

3. Policy Factors: [within 100 characters]

Practice

Send a prompt example and compare the AI's responses.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.