Sub topic 2
Context Window Limits and Why They Matter
Assume you can read Python and have used an API that accepts prompts and returns model output. This chapter connects token limits, cost, memory, speed, deployment, and privacy.
Context window limits
A context window is the maximum number of tokens a model can consider in one request. It includes instructions, conversation history, retrieved documents, and often the requested output. A long document can therefore leave too little room for the answer.
When the input exceeds the limit, the request may fail or your application may need to remove older content. Truncation can silently remove facts that the model needs.
Estimating cost from token prices
Hosted APIs usually charge separately for input and output tokens:
cost = input_tokens / 1,000,000 × input_price + output_tokens / 1,000,000 × output_price
Multiply the per-request cost by expected monthly requests. Include system prompts, conversation history, and retrieved text in the input estimate.
Estimating memory and quantization
Parameter count is the number of learned values in a model. Weight memory is approximately:
parameters × bytes per parameter
For example, a 7-billion-parameter model needs about 14 GB for weights at 16-bit precision, 7 GB at 8-bit, or 3.5 GB at 4-bit. Runtime overhead and the key-value (KV) cache require additional memory.
def weight_memory_gb(params_billion, bits):
return params_billion * bits / 8
for bits in (16, 8, 4):
print(bits, weight_memory_gb(7, bits), "GB")Quantization stores weights with fewer bits. It reduces memory and can improve speed, but may reduce accuracy or output quality. Test the quantized model on your real tasks.
Latency versus throughput
Latency is the time one request takes. Throughput is the number of requests handled per unit of time. A larger model may improve quality but increase both latency and hardware cost. Batching can increase throughput while making individual requests wait longer.
Local and server deployment
You can run an open-weight model on a workstation, a private server, or a managed inference service. Local execution provides control and can avoid sending data elsewhere. Server deployment centralizes hardware and scaling, but requires capacity planning and monitoring.
Privacy and compliance
Check where prompts and outputs are processed, stored, and logged. Remove unnecessary personal data, configure retention settings, encrypt traffic, and verify the provider's contractual terms. A local model does not automatically guarantee compliance; access control, logging, and backups still matter.
Exercise
A request has 2,000 input tokens and 300 output tokens. Why should both counts appear in a cost estimate?
Show answer
Hosted pricing commonly charges input and output tokens at separate rates, so excluding either count produces an incomplete estimate.