Sub topic 2
Output Limits and the Combined Token Budget
Assume you can read basic Python, call an LLM API, and do simple arithmetic.
Output Limits and the Combined Token Budget
A model has a context window, which is the maximum number of tokens it can process in one request. The request usually contains both the input and the requested output:
input tokens + maximum output tokens ≤ context window
The output limit is a ceiling, not a promise. If you request 1,000 output tokens, the model may generate fewer. However, the requested maximum must still fit with the input.
def fits_in_window(input_tokens, max_output_tokens, context_window):
return input_tokens + max_output_tokens <= context_window
print(fits_in_window(2000, 500, 8000)) # True
print(fits_in_window(7600, 500, 8000)) # FalseSome APIs also reserve space for hidden reasoning or other internal tokens. Check the provider's documentation when exact limits matter.
Why Long Conversations Get Expensive
In a typical chat API, your application sends the relevant conversation history again on every turn. Earlier user messages and model replies therefore become new input tokens.
If a system prompt has 500 tokens, each user message has 100 tokens, and each reply has 300 tokens, ten turns can send 24,000 input tokens in total. Only 1,000 tokens were newly written by the user, but the repeated history creates most of the input.
How Providers Price Tokens
Providers commonly price input and output separately, usually as dollars per million tokens.
| Usage | Example rate |
|---|---|
| Input tokens | $3.00 per million |
| Output tokens | $15.00 per million |
For 2,000 input tokens and 500 output tokens:
(2,000 × 3 + 500 × 15) ÷ 1,000,000 = $0.0135
Actual rates differ by model. Cached input and hidden reasoning tokens may have separate billing rules.
> callout note > Use the tokenizer for the specific model family when counting. Word counts are only rough estimates, and different tokenizers split the same text differently. > ```
Reading Usage from API Responses
Successful responses often include a usage object with fields such as input_tokens, output_tokens, and total_tokens. Use these returned counts for billing records because they reflect what the provider processed.
Exercise
A request uses 4,000 input tokens and generates 600 output tokens. The context window is 4,096 tokens. What must change?
Show answer
The request does not fit because 4,000 + 600 = 4,600. Reduce the input, lower the output limit, or use a larger-context model.