Certainly! Let me explain the pay-as-you-go API model with a focus on token-based pricing, which is how OpenAI charges for usage of its language models like GPT-3.5 and GPT-4.
What is a Token?
- A token is a piece of text that the model processes. It can be as short as one character or as long as one word or part of a word.
- Tokens are the basic units the model uses to understand and generate language.
- For example:
- The word "ChatGPT" might be split into two tokens:
"Chat" and "GPT".
- The sentence "Hello, how are you?" might be split into tokens like
"Hello", ",", " how", " are", " you", "?".
- On average, 1 token ≈ 4 characters of English text, or roughly 0.75 words.
How Does Token-Based Pricing Work?
OpenAI charges based on the number of tokens processed during an API call. This includes:
- Input tokens: The tokens in the prompt or request you send to the model.
- Output tokens: The tokens generated by the model in response.
Total tokens = Input tokens + Output tokens
Example of Token Usage
Suppose you send the prompt:
"Translate the following English text to French: 'Hello, how are you?'"
- This prompt might be about 15 tokens.
- The model generates the French translation: "Bonjour, comment ça va ?" which might be about 7 tokens.
Total tokens used = 15 (input) + 7 (output) = 22 tokens
Pricing Calculation
OpenAI sets a price per 1,000 tokens. For example (these are approximate and can vary by model):
| Model | Price per 1,000 tokens (input) | Price per 1,000 tokens (output) |
|---|
| GPT-4 | $0.03 | $0.06 |
| GPT-3.5 | $0.0015 | $0.002 |
- Input tokens are usually cheaper than output tokens because generating text requires more compute.
- You pay for both input and output tokens.
How to Calculate Your Cost
- Count the number of input tokens.
- Count the number of output tokens.
- Multiply input tokens by the input token rate.
- Multiply output tokens by the output token rate.
- Add the two amounts to get the total cost.
Example Cost Calculation
If you use GPT-4 and send 500 input tokens and receive 1,000 output tokens:
- Input cost = 500 tokens × (0.03/1,000)=0.015
- Output cost = 1,000 tokens × (0.06/1,000)=0.06
- Total cost = 0.015+0.06 = $0.075
Why Token-Based Pricing?
- Fairness: You pay for exactly how much you use.
- Scalability: Works well for both small and large requests.
- Transparency: Easy to estimate costs based on prompt length and expected output.
Summary
- A token is a chunk of text (roughly 4 characters or 0.75 words).
- You pay for both input tokens (your prompt) and output tokens (model’s response).
- Pricing is per 1,000 tokens, with different rates for input and output.
- Total cost = (input tokens × input rate) + (output tokens × output rate).
If you want, I can also help you estimate costs for specific use cases or show how to count tokens programmatically!