Ai Engineering Core / Transformers, Tokens & Context Windows

Context Windows, API Costs and Token Limits in LLMs

๐Ÿง  What is a Context Window?

Think of the context window as the model's working memory โ€” it's the maximum amount of text the LLM can see and process at any one time. ๐Ÿ‘๏ธ

๐Ÿ“ฆ What Fits Inside It?

Everything gets counted toward the limit:

  • ๐Ÿ’ฌ Your current message

  • ๐Ÿ“œ The full conversation history

  • ๐Ÿค– The model's previous replies

  • โœ๏ธ Tokens being generated in the current response

โš ๏ธ What Happens When It's Full?

Once the total tokens exceed the limit, the model simply cannot process any further โ€” it's like trying to pour water into an already full glass. ๐Ÿฅ›

๐Ÿ’ก The Simple Way to Think About It

Just like humans can only hold so much information in their head at once, an LLM can only "see" a limited chunk of the conversation at any given moment. ๐Ÿงฉ


๐Ÿ”„ How LLMs Generate Responses

LLMs don't write responses all at once โ€” they build them one token at a time, like constructing a sentence brick by brick. ๐Ÿงฑ

๐Ÿ“– Example: Watch It Unfold

๐Ÿ‘ค User asks: "What is my name?"

Step

What the model sees

1๏ธโƒฃ

What is my name? โ†’ predicts "Your"

2๏ธโƒฃ

What is my name? Your โ†’ predicts "name"

3๏ธโƒฃ

What is my name? Your name โ†’ predicts "is"

4๏ธโƒฃ

...and so on until the response is complete โœ…

Each new token gets added to the sequence, which then feeds back in to predict the next one.


โš ๏ธ Why Context Window Size Matters

A bigger context window means the model can:

  • ๐Ÿ—ฃ๏ธ Remember longer conversations

  • ๐Ÿ” Reference details mentioned much earlier

  • ๐Ÿ“„ Handle large documents with ease

  • ๐Ÿง  Perform deeper, more accurate reasoning

  • ๐Ÿ› ๏ธ Support advanced prompting techniques

๐Ÿ’ก The bigger the window, the more the model can "hold in its head" at once!

๐Ÿงจ Important Use Cases

  • Multi-shot prompting

    • Giving several examples before the actual task

  • RAG (Retrieval-Augmented Generation)

    • Injecting external knowledge into prompts

  • Agent systems

    • Long-running workflows and memory chains


๐Ÿ’ฐ How API Pricing Actually Works

Chat products like OpenAI and Anthropic offer free and paid plans โ€” but APIs are different. With APIs, you pay for exactly what you use, based on token consumption. ๐Ÿงฎ

๐Ÿงพ What You're Actually Paying For

1๏ธโƒฃ Input Tokens โ€” Everything you send to the model:

  • ๐Ÿ“œ Full conversation history

  • โš™๏ธ System prompts

  • ๐Ÿง  Memory & RAG data

  • ๐Ÿ’ฌ Current user message

2๏ธโƒฃ Output Tokens โ€” Everything the model generates:

  • โœ๏ธ The visible response text

  • ๐Ÿ” Hidden reasoning traces (in reasoning models)

โš ๏ธ Even if the reasoning is invisible to you, the computation still happens โ€” and you still get billed for it!

๐Ÿ“ˆ Costs Grow With Conversation Length

The longer a conversation gets, the more tokens get reprocessed with every single message. This is simply how transformers work โ€” they look back at all prior tokens every time. ๐Ÿ”„

๐Ÿง  Reasoning Models Can Get Pricey

Reasoning models think deeply before responding โ€” generating large internal reasoning traces behind the scenes. You pay for:

  • โœ… The answer you see

  • ๐Ÿ’ธ The reasoning you don't

๐Ÿ’ก Always factor in hidden reasoning costs when budgeting for reasoning models!

โšก Prompt Caching โ€” A handy Cost Saver!

Some API providers offer prompt caching โ€” a smart optimization that kicks in when the same input is sent repeatedly.

Instead of recomputing everything from scratch, the provider reuses previous computations, making repeated inputs significantly cheaper ๐Ÿ’ฐ and faster โšก.

โœ… Best part? In some systems this happens completely automatically โ€” no extra setup needed! ๐Ÿ™Œ


๐Ÿ“Š Context Window, Pricing, Speed & More โ€” Model Comparison

๐Ÿ‘‰ https://www.vellum.ai/llm-leaderboard


โœ… Special Note

Understanding which model to use โ€” based on context window, pricing, and speed โ€” is an essential skill for any great AI engineer. Every use case is unique and demands a different solution.