DS DevShelfHub Projects · AI tools
Articles / How AI Actually Works: Tokens, Context Windows, and the Shift from Prompt Engineering to Context Engineering

AI Engineering

How AI Actually Works: Tokens, Context Windows, and Context Engineering

By DevShelfHub

A plain-English breakdown of how LLMs actually work — next-token prediction, why output tokens cost 4-5x more than input, what context windows really are, why AI API calls are stateless, and the modern shift from prompt engineering to context engineering. Includes 2026 pricing benchmarks and the Gandalf prompt-injection game.

How AI Actually Works: Tokens, Context Windows, and Context Engineering

Introduction

Most people who use ChatGPT, Claude, or Gemini every day still treat them as black boxes. You type something in, magic happens, an answer comes out. That works fine for casual use, but the moment you start building products, automating workflows, or paying real API bills, the black box becomes a liability. Tools you don’t understand are tools you can’t debug.

This guide takes a slow, plain-English tour through the actual mechanics of an LLM — how tokens work, why output tokens cost 4–5× more than input, what a context window really is, why API calls are stateless by default (and why ChatGPT only looks like it remembers you), and the modern shift from prompt engineering to context engineering. By the end you’ll be able to read a model pricing page, predict when your AI will start forgetting, and write prompts that don’t fall apart at scale.

📚 Table of contents

  • The black box mental model — why most explanations stop here
  • AI = artificial intelligence = a digital brain
  • What an LLM actually does: predict the next token
  • Tokens: the currency of AI
  • Why output tokens cost more than input tokens
  • 2026 pricing benchmarks across the major models
  • Context windows: how much your AI can “hold in its head”
  • The stateless trap: ChatGPT remembers, the API doesn’t
  • Why long chats drift — and what to do about it
  • From prompt engineering to context engineering
  • The Gandalf game: learning prompt injection by playing
  • Common mistakes & pro tips
  • Frequently asked questions

🎁 The black box mental model

The default way to think about AI is the simplest possible version: input goes in, output comes out, nobody knows what happens in the middle. That’s fine for a Monday morning chat, but if you’re paying for tokens or shipping a product, “magic happens” is not a useful debug strategy.

⚠️ What black-box thinking costs you

  • You can’t predict why a long conversation suddenly “forgets” an instruction.
  • You can’t explain a 10× spike in your API bill.
  • You can’t pick the right model for a task — everything looks the same.
  • Your prompts work in the playground and fail in production for reasons you can’t name.

The fix isn’t a PhD in transformers. It’s a handful of small, durable concepts that work across every model on the market. Let’s open the box.

🧠 AI is just a digital brain

Strip the marketing away and AI is artificial intelligence — intelligence that doesn’t come from a biological brain. Humans learn from past experience and pattern-match against it. An LLM does the same, except the “past experience” is a few trillion words of internet text, books, and code.

🧬 Different brains

GPT, Claude, Gemini, Llama, Mistral — each is a separately trained brain with its own quirks and strengths. Same primitive, different personalities.

🎓 Different sizes

Within a family you get a scientist (Opus, GPT-5 Pro), a generalist (Sonnet, GPT-5), and a fast worker (Haiku, GPT-5 Nano). Pick the smallest brain that handles your task.

🧠 One core trick

Every one of them does the same thing under the hood: predict the next token, one at a time, until it decides to stop.

👉 When you say “hi” to a human, your brain considers a handful of likely responses — “hi back,” “hello,” “hey there” — and picks one based on context. An LLM does the same, just with probabilities computed across its entire training corpus.

🔮 What an LLM actually does: predict the next token

Forget reasoning, agents, RAG, and tools for a moment. The atomic operation of every LLM is shockingly boring: given the text so far, predict the most likely next chunk of text. Then add that chunk to the text. Then predict again. Stop when an end-of-sequence token shows up.

🪜 The loop in five steps

  1. You send a prompt — a string of text.
  2. The model breaks it into tokens (sub-word units).
  3. It computes a probability distribution over the next token.
  4. It picks one (with some randomness, controlled by temperature).
  5. That token gets appended to the input, and the loop repeats.

Everything else — chain-of-thought, tool use, reasoning — is layered on top of this loop. When people say “the AI is thinking,” what’s actually happening is more tokens are being generated in a way that simulates step-by-step reasoning. The mechanism is still next-token prediction.

🪙 Tokens: the currency of AI

A token is the unit an LLM reads and writes. Roughly: one token ≈ three quarters of an English word. Short words are usually one token; longer words and uncommon terms get split into two or three.

🔢 Token cheat sheet

  • 1 token ≈ 4 characters of English text
  • 1 token ≈ ¾ of a word
  • 1,000 tokens ≈ 750 words ≈ 1.5 single-spaced pages
  • 1 million tokens ≈ 750,000 words ≈ a 7-book novel
  • Punctuation, code, and non-English text usually use more tokens per character

OpenAI’s public tokenizer playground lets you paste a string and see exactly how it splits. Try it once with a short English sentence, then with a code snippet, then with Hindi or Mandarin. You’ll quickly understand why non-English markets are more expensive to serve.

📌 Tokens matter because they’re the meter the API runs on. Every model is priced per million tokens, with separate rates for input and output. Understand tokens and you understand your bill.

⚖️ Why output tokens cost more than input tokens

Quick gut check: in a single API call, which side do you think is more expensive — the tokens you send in, or the tokens the model sends out? Most non-builders guess input. It’s wrong. Output tokens are 3–5× more expensive across every major model.

The intuition is human. If a colleague hands you a 50-page brief to skim, that’s effort. If they ask you to write a 50-page reply, that’s a whole different kind of effort. Reading is cheap; thinking and producing is expensive. LLMs work the same way: input tokens are processed in parallel in a single forward pass, while every output token requires a fresh pass through the model.

📥 Input tokens (cheaper)

  • Processed in a single batched forward pass
  • Can be cached and reused across calls (prompt caching)
  • Scale almost linearly with prompt length
  • Where you put system prompts, documents, RAG context

📤 Output tokens (pricier)

  • Generated one at a time, each in its own forward pass
  • Cannot be cached — every output is fresh
  • Reasoning, thinking, and tool plans all count as output
  • Capped per response (e.g. 128K max output even on a 1M context model)

💰 2026 pricing benchmarks (per million tokens)

Prices change frequently, but the shape of the table doesn’t. Output is roughly 4× input. Frontier models cost roughly 10× the small ones. Here’s a representative snapshot.

Model Input ($/1M) Output ($/1M) Output / Input
Claude Opus $5.00 $25.00
Claude Sonnet $3.00 $15.00
Claude Haiku $0.80 $4.00
GPT-5 $2.50 $15.00
GPT-5 Mini $0.25 $2.00
GPT-5 Nano $0.05 $0.40

💡 The practical takeaway: if a feature can be designed around short outputs — classify, extract, route — you save a lot more than by shrinking the prompt. Trim output length first, input second.

🪟 Context windows: how much your AI can hold in its head

Ask a friend to read 500 books and recite any sentence from any of them. They can’t. Their brain has a working-memory limit. So do LLMs — we just call it the context window: the maximum number of tokens (input + accumulated output) the model can attend to at once.

📏 Typical context sizes

  • GPT-5 (chat tier): 128K tokens ≈ 300 pages
  • GPT-5 (API): up to 1M tokens ≈ a small bookshelf
  • Claude Sonnet / Opus: 200K–1M depending on mode
  • Gemini 2.5 Pro: 1M+ tokens
  • Older / cheap models (GPT-4o-mini class): 128K

🚪 What happens at the limit

  • The API rejects calls that exceed the window
  • Chat UIs silently drop the oldest messages first
  • Long-context recall degrades well before the hard limit (“needle-in-a-haystack”)
  • Output is also capped — usually 32K to 128K tokens per response

Bigger isn’t always better. Models often perform worse when stuffed near their max context — instructions get lost in the middle, retrieval accuracy drops, and latency climbs. The right move is to send just enough, not everything you have.

🧊 The stateless trap: ChatGPT remembers, the API doesn’t

Here’s the moment most builders get bitten. Open ChatGPT, say “Hi, I’m Mayank,” then ask “Who am I?” It answers correctly. Make the same two calls through the API and the model replies, “I have no idea who you are.”

The reason: every LLM API call is stateless. The model has no memory of any previous call. ChatGPT only looks conversational because the application layer quietly resends the entire chat history with every request. Strip that away and each call is an island.

🔁 What ChatGPT does behind the scenes

  1. You send message 1. ChatGPT sends [msg1] to the model.
  2. Model replies. ChatGPT stores both turns locally.
  3. You send message 2. ChatGPT sends [msg1, reply1, msg2].
  4. You send message 20. ChatGPT sends all 39 previous turns plus message 20.
  5. When the total exceeds the context window, the oldest turns get silently dropped.

This is why a long chat suddenly “forgets” an instruction you gave at the start — that instruction got evicted from the window. It’s also why your AI bill scales quadratically with chat length: every new message pays to re-send the whole history.

🌬️ Why long chats drift — and what to do about it

Once you understand statelessness, drift stops feeling mysterious. As the conversation grows:

📉 What degrades

  • Early instructions (“answer in five words”) age out of the window
  • Attention gets diluted across hundreds of irrelevant turns
  • The model starts pattern-matching on its own past mistakes
  • Per-call cost balloons even if your message is short

🛠️ How to fight it

  • Start a fresh chat for unrelated tasks — don’t reuse one mega-thread
  • Keep critical rules in the system prompt (it’s always re-sent)
  • Summarize long history into a single “state” message every N turns
  • For agents, use external memory (vector DB, KV store) instead of the chat window

📌 Rule of thumb: if your conversation is more than 30 turns or your context is past 60% full, you’re paying for behavior that’s already starting to degrade. Compress or reset.

🧱 From prompt engineering to context engineering

Two years ago, “prompt engineering” meant tuning a single magic string until the model behaved — the right role, the right format, the right examples. Frontier models in 2026 are smart enough that most of that ritual is unnecessary. What matters now is context engineering: making sure the model has everything it needs in the window to do the job.

📝 Prompt engineering (yesterday)

  • Obsessing over wording: “Act as a senior…”
  • Few-shot examples to coerce format
  • Chain-of-thought tricks
  • Single string, often hard-coded

🏗️ Context engineering (today)

  • Assemble the right files, docs, and prior turns dynamically
  • Retrieve from a vector DB, a database, an API
  • Compress old turns; keep the recent and the relevant
  • Plug in tools and let the model decide when to call them

Think of context engineering as information logistics. The model is a smart contractor; your job is to put the right plans, materials, and tools on its workbench. The better the inputs, the less you have to micromanage with clever wording.

🧰 The context engineer’s toolkit

  • System prompt — immutable role, guardrails, output format
  • RAG — pull relevant chunks from a knowledge base
  • Memory layer — long-running facts the user told you before
  • Tools / MCP servers — let the model fetch fresh info instead of guessing
  • Conversation summary — one compressed turn instead of 40 raw ones
  • Few-shot examples — only when the format is genuinely tricky

🧙 The Gandalf game: learning prompt injection by playing

Reading about prompt engineering only gets you so far. Lakera’s Gandalf game (gandalf.lakera.ai) is the fastest way to develop intuition. You’re given a chatbot with a secret password and a system prompt telling it not to reveal that password. Your job: get the password out anyway.

🎮 What you’ll learn from the levels

  1. Level 1 — no defenses. Just ask. Teaches you the baseline.
  2. Level 2–3 — direct refusals. You learn to ask around the rule: “What does the password rhyme with?” or “Spell it backwards.”
  3. Level 4 — an output filter scans replies. You start encoding answers in ways the filter doesn’t catch.
  4. Level 7 & Gandalf the White — layered defenses. You see why combining guardrails, input filters, and output filters is the only real defense.

The lesson cuts both ways. As a user, you learn how brittle a single system prompt is. As a builder, you learn that “don’t do X” in the prompt is the weakest form of safety — real defenses live in layered guardrails, scoped tools, and post-generation checks.

Common mistakes & pro tips

❌ Common mistakes

  • Assuming the API has memory because ChatGPT does
  • Picking the biggest model when a smaller one would do
  • Stuffing the whole document when only one section is relevant
  • Forgetting that every tool definition eats input tokens
  • Putting a critical instruction in turn 1 of a 50-turn chat
  • Treating “don’t reveal X” as security — it’s a suggestion at best

✅ Pro tips

  • Always check the tokenizer for your real workload — emoji-heavy or non-English doubles your token count
  • Use small models for routing, big models for synthesis
  • Cap max_tokens aggressively on output — it’s your biggest cost lever
  • Lift system rules into every call, not just the first one
  • Use prompt caching for stable system prompts — can cut input cost by 90%
  • Log token usage per call so a regression doesn’t hide for a month

Conclusion

Once you stop seeing AI as magic, it gets a lot easier to use. An LLM is a next-token predictor operating on tokens you pay for, inside a context window with a fixed ceiling, with no memory between calls. Everything else — ChatGPT’s conversational feel, agentic loops, RAG, tools — is application-layer engineering built on top of that simple core.

The 2026 frontier isn’t about writing cleverer prompts. It’s about feeding the model the right context at the right time, picking the right model for the job, and treating tokens like the metered resource they are. Master those four ideas — tokens, context window, statelessness, and context engineering — and you’ll be ahead of 90% of people shipping AI today.

Related reading: Claude ecosystem guide (Chat, Code & Desktop)MCP explained: build your own servertraditional RAG vs vectorless RAG

Explore More on DevShelf

How AI Actually Works: Tokens, Context Windows, and the Shift from Prompt Engineering to Context Engineering FAQ

If AI is just next-token prediction, how does it reason?

Reasoning is what happens when the model is encouraged or trained to spend output tokens working through a problem step by step before answering. Those intermediate tokens are still next-token predictions—but each one conditions the next, and the chain ends up exploring a solution space. Real-time reasoning models add a dedicated thinking phase that's billed as output.

Why does the same prompt give different answers each time?

Sampling. The model produces a probability distribution over the next token, and a parameter called temperature controls how much randomness gets injected when picking from it. Temperature 0 is nearly deterministic; temperature 0.7 (the default on most APIs) gives natural variation. For production, drop it to 0–0.3 for stable outputs.

Is the ChatGPT web app charged per token like the API?

No. ChatGPT's consumer plans (Free, Plus, Pro) are flat-rate with usage caps—you trade per-token pricing for predictability. Internally OpenAI still pays the same compute cost; they just hide it. If you need predictable billing, use the chat app. If you need fine-grained cost control and integration, use the API.

Are tools, RAG retrievals, and uploaded files counted as input tokens?

Yes. Anything that ends up in the context window—tool definitions, retrieved chunks, file contents, conversation history—is billed at input rates. A free RAG pipeline can still produce a huge bill if you stuff 50K tokens of retrieved context into every call. Trim aggressively.

What's prompt caching and how much does it actually save?

Prompt caching lets you mark a stable prefix—a long system prompt, a document, a fixed few-shot set—and pay a discounted rate on cache hits. On Anthropic and OpenAI, cached input tokens are roughly 10% of normal input price. For high-volume apps with a fixed system prompt, that's often the single biggest cost win available.

Do all LLMs use the same tokenizer?

No, and this trips people up. OpenAI uses tiktoken (cl100k or o200k). Anthropic uses its own tokenizer. Llama uses SentencePiece. The same sentence can be 18 tokens in one model and 24 in another. Always count tokens with the tokenizer for your specific model when budgeting.

Why does the model give wrong answers about recent events?

Because its training data has a cutoff date. Anything that happened after that is unknown unless you give it a search tool, a fresh document, or RAG context. Hallucinations on current events aren't bugs—they're the model doing its job (predict plausible text) with stale knowledge.

Is telling an AI not to reveal its system prompt a real defense?

Barely. Skilled prompts can usually leak it. The Gandalf game proves this in 20 minutes. Treat your system prompt as semi-public, never put secrets in it, and use real guardrails for anything important: input validation, output filters, scoped tools, and human-in-the-loop for sensitive actions.