Sub topic 5
Output limits and the combined token budget
This chapter assumes you can read basic Python, have called a chat or completion API before, and know that a context window is the maximum number of tokens a model can handle in one request. Here you will see how the window is shared between your prompt and the model's reply.
Two limits, one window
A model call has two separate limits that people often mix up.
- The context window caps the total tokens the model can work with in one call. The prompt and the reply both count toward it.
- The output limit caps how many tokens the model may generate in one reply. It is usually much smaller than the context window.
A model might have a 200,000-token window and a 8,000-token output limit. You can send a very long prompt, but you cannot get a very long answer back in a single call.
The rule you need is:
input tokens + output tokens <= context windowThe output side is a maximum, not a promise. The model stops earlier if it finishes its answer. But the provider has to reserve room for the worst case, so the check uses the maximum you allow.
The max output setting
Most APIs let you set a parameter that limits the reply length. Its name varies by provider, for example max_tokens or max_output_tokens. Three things follow from it.
- If the reply hits this limit, it is cut off, often in the middle of a sentence.
- If
input tokens + max outputis larger than the window, many APIs reject the request with an error. - A smaller cap protects you from long, expensive replies.
Checking the budget in code
This function checks that the prompt plus the maximum output fits a window.
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(7800, 500, 8000)) # FalseIn the second call, the prompt alone fits, but there is no room left for a 500-token reply. The fix is to shorten the prompt or lower the output cap.
Working out how much room is left
You can also turn the rule around. Given the prompt size, how long can the reply be?
def max_reply_tokens(input_tokens, context_window, output_limit):
remaining = context_window - input_tokens
return max(0, min(remaining, output_limit))
print(max_reply_tokens(2000, 8000, 4096)) # 4096
print(max_reply_tokens(6500, 8000, 4096)) # 1500
print(max_reply_tokens(8200, 8000, 4096)) # 0The reply is bounded by whichever is smaller: the space left in the window or the model's own output limit. A result of 0 means the prompt must shrink before you can call the model at all.
Common errors
- Forgetting the output side. A prompt that fills 99% of the window leaves almost no room for the answer.
- Truncated replies. If an answer ends abruptly, check whether the response reports that it stopped because of the length limit. Raising the cap or asking for a shorter answer fixes it.
- Setting the cap far too high. A very large cap does not cost anything by itself, since you pay for tokens actually generated. But it can make the request fail the window check and gives the model no pressure toward brevity.
- Assuming the cap is a target. Setting it to 1,000 does not make the model write 1,000 tokens. It only sets the ceiling.
Exercise
Exercise
A model has a 16,000-token context window and an output limit of 4,000 tokens. Your prompt is 13,500 tokens. What is the largest reply you can request, and what happens if you set the output cap to 4,000?
Show answer
The room left in the window is 16,000 - 13,500 = 2,500 tokens, which is less than the 4,000 output limit, so the largest reply is 2,500 tokens. Setting the cap to 4,000 makes input plus output 17,500, which exceeds the window, so many APIs would reject the request. Lower the cap to 2,500 or shorten the prompt.