By Shakil Ahmed, Android developer & software engineer
How LLMs Work: Tokens, Context Windows and Temperature Explained
A plain-English guide to how LLMs work: next-token prediction, tokens, context windows, temperature, hallucinations and token pricing for developers.
If you use ChatGPT, Claude or Gemini every day, you have probably wondered how LLMs work behind that friendly chat box. The short answer is surprisingly simple: a large language model (LLM) is a very powerful autocomplete that predicts the next small piece of text, again and again.
In this guide I will explain the ideas every developer should know: tokens, context windows, temperature, hallucinations, memory and cost. No maths degree needed. When I started adding AI features to my Android apps, these few concepts explained almost every strange bug and surprising bill I saw.
How LLMs Work: Next-Token Prediction in Plain English
Think about the keyboard on your phone. You type "See you" and it suggests "tomorrow" or "soon". It guesses the next word based on what usually comes next.
An LLM does the same thing, but at a huge scale. It reads your text and asks one question: "What is the most likely next piece of text?" It picks one piece, adds it to the text, and asks the same question again. It repeats this until the answer is complete.
So when you ask, "What is the capital of Bangladesh?", the model does not look up a database. It predicts that "The", then "capital", then "is", then "Dhaka" are likely next pieces. Because it has seen so much text during training, those predictions are usually correct.
Tip: Remember this one idea. Almost every LLM behaviour, good or bad, comes from "predict the next piece of text".
Training vs Inference: Two Different Phases
People often mix up these two words. They are very different.
| Phase | What happens | Who does it | How often |
|---|---|---|---|
| Training | The model reads a huge amount of text and adjusts billions of internal numbers (parameters) to get better at predicting the next token | The AI company | Rarely, takes weeks or months and huge computing power |
| Inference | The trained model receives your prompt and generates an answer | You, every time you call the API | Every request, takes seconds |
Training is like a student studying for years. Inference is the student answering one exam question. Calling an API is only inference; you are not teaching the model anything.
After the first training phase (called pretraining), companies also fine-tune the model with human feedback so it behaves like a helpful assistant, as described in Anthropic's glossary.
What Are Tokens? (Tokens Are Not Words)
LLMs do not read words or letters directly. They read tokens. A token is a small chunk of text. It can be a whole word, part of a word, a space plus a word, a punctuation mark or even a single byte.
Here is an illustrative example of how a sentence might be split. The exact split depends on the tokenizer each model uses.
Sentence: "Unbelievable! I'm learning Kotlin."
Tokens: ["Un", "believ", "able", "!", " I", "'m", " learning", " Kotlin", "."]
Count: 9 tokens for 5 words
Notice a few things:
- Common words like " learning" are often one token.
- Long or rare words like "Unbelievable" are split into pieces.
- Spaces and punctuation count too.
A rough rule for English is that one token is a few characters. Anthropic's docs say that on its older tokenizer, a token was about 3.5 English characters, and its newer tokenizer produces roughly 30% more tokens for the same text. Different companies use different tokenizers, so the same text can have a different token count on different models. Always use the provider's token counting tool if you need exact numbers.
Why Bangla and Other Non-English Text Often Uses More Tokens
This part matters a lot for developers in Bangladesh. Tokenizers are built from training data, and much of that data is English. Common English words get their own efficient tokens. Text in Bangla, Hindi, Arabic and many other languages is often split into smaller pieces, so the same meaning can need more tokens.
A research paper, Language Model Tokenizers Introduce Unfairness Between Languages (Petrov et al., 2023), found that the same text translated into different languages could produce token sequences of very different lengths, with differences up to 15 times in some cases. The exact gap for Bangla depends on the model and tokenizer, and newer tokenizers may handle it better.
What this means in practice:
- A Bangla chat may cost more than the same chat in English.
- A Bangla document fills the context window faster.
- You should measure your own text instead of guessing.
Context Window: The AI's Desk
Imagine the model works at a desk. Everything it can "see" right now must be on that desk: the system instructions, the whole chat history, any documents you pasted, and the answer it is writing. That desk is the context window.
The context window is measured in tokens. If your conversation gets bigger than the desk, something must fall off. Either the app drops older messages, summarises them, or the API returns an error.
Important points:
- The context window is like working memory, not long-term knowledge.
- Input and output usually share the same space.
- Bigger is not always better. Long contexts cost more and can be slower, and models may pay less attention to details buried in the middle of a very long prompt.
Typical Context Window Sizes (as of November 2026)
At the time of writing, the major providers' flagship models have very large windows. Always check the official docs, because these numbers change often.
| Provider | Example models | Context window (as of November 2026) |
|---|---|---|
| Anthropic | Claude Opus 5.5, Sonnet 5.5, Haiku 5.5 | 1M tokens (Claude models overview) |
| OpenAI | Current GPT-6 family flagship models | About 1.05M tokens, per OpenAI's models page |
| Gemini 4 models | 1M tokens, per Google AI Studio's models page |
For scale, Anthropic says 1M tokens is roughly 555,000 English words on its current tokenizer. That is several long books. But remember, Bangla text will fit fewer words in the same space.
Tip: A large context window does not mean you should fill it. Send only what the model needs. Your app will be faster and cheaper, and answers are often better.
Temperature and Top-p: Controlling Randomness
When the model predicts the next token, it does not have just one answer. It has a list of candidates with probabilities. For example, after "The weather today is":
"sunny" 40%
"hot" 25%
"cloudy" 20%
"rainy" 10%
...others 5%
Temperature controls how the model picks from that list:
- Low temperature (close to 0): it almost always picks the top choice. Output is focused and repeatable. Good for extracting data or classifying text.
- High temperature (around 1 or higher): it picks less likely options more often. Output is varied and creative. Good for brainstorming or stories.
Top-p (nucleus sampling) is a related setting. It says, "Only choose from the smallest group of tokens whose probabilities add up to p." With top-p 0.9, the model ignores the rare 10% tail.
Two honest warnings:
- Even at temperature 0, outputs are not fully deterministic. Anthropic's glossary states this clearly.
- Some newer APIs no longer let you set these at all. For example, as of November 2026, Anthropic's newest API version rejects
temperature,top_pandtop_kand uses defaults instead. Check your provider's docs before you depend on them.
Why LLMs Hallucinate
A hallucination is when the model gives an answer that sounds confident but is wrong or made up. A fake library function. A research paper that does not exist. A wrong date.
Once you understand next-token prediction, this is not surprising. The model is trained to produce text that looks likely. It is not checking a fact database. If it does not know the answer, the most "likely-looking" text may still be a confident sentence.
It happens most with rare or very new topics, vague prompts, and prompts that push for an answer even when none exists.
How to reduce hallucinations:
- Give the model the real source text and ask it to answer only from it. This is the idea behind RAG, explained simply.
- Allow it to say "I don't know".
- Ask for quotes or references you can verify.
- Review AI output like code from a junior developer. My post on how to review AI work you did not write covers this.
Why LLMs Don't "Remember" Between Chats
Here is a fact that surprises many beginners: the model itself has no memory between requests. Each API call starts fresh.
So how does a chat app "remember" what you said five messages ago? The app sends the whole conversation again with every new message. The model reads the full history on its desk, then predicts the next reply.
Request 1: [system] + [user: Hi, I'm Rahim]
Request 2: [system] + [user: Hi, I'm Rahim] + [assistant: Hello Rahim!] + [user: What's my name?]
This has three effects:
- Long chats get more expensive, because you pay for the history again each time.
- When the history exceeds the context window, old messages must be dropped or summarised.
- "Memory" features in apps are built by the app, for example by saving notes and adding them to future prompts.
If you are building a chat app on Android, you will manage this history yourself. I show this step by step in building an AI chat app with the Gemini API on Android.
How LLM Pricing Works: Cost per Token
Most LLM APIs charge per token, with separate prices for input (what you send) and output (what the model writes). Output is usually several times more expensive.
Let us do a simple example using Claude Sonnet 5.5, priced at $2 per million input tokens and $10 per million output tokens on Anthropic's models page as of November 2026.
| Item | Tokens | Price per 1M | Cost |
|---|---|---|---|
| Input (system + history + question) | 2,000 | $2 | $0.004 |
| Output (answer) | 500 | $10 | $0.005 |
| Total per request | $0.009 |
Less than one cent. But at 10,000 requests a day, that is about $90 per day. Small numbers grow fast.
To keep cost down, trim old history, cap output length, and use prompt caching or batch processing if your provider offers them.
Practical Takeaways for Developers
Here is what I keep in mind when I add LLM features to real apps:
- Treat output as a guess, not a fact. Validate it, especially JSON and code.
- Count tokens, don't guess. Especially for Bangla or mixed-language text.
- Send only what is needed. The context window is a desk, not a storage room.
- Manage memory yourself. Store history, trim it and summarise it.
- Pick the right model size. A small model is often enough.
- Set budgets and limits. Cap output tokens and watch your usage dashboard.
- Run network calls off the main thread. On Android, use Kotlin coroutines so your UI stays smooth while the model responds.
- Learn to prompt well. Clear prompts fix many problems. Start with my guide to prompt engineering for developers.
When you are ready to go further, LLMs can also call tools and act in steps. Read AI agents explained and what MCP (Model Context Protocol) is to see how models connect to real data and actions.
Frequently Asked Questions
Is an LLM just a fancy autocomplete? At its core, yes. It predicts the next token again and again. But because it learned from a huge amount of text, that prediction can produce useful reasoning, code and explanations.
How many words is 1,000 tokens? It depends on the language and the model's tokenizer. Anthropic's docs suggest about 750 English words on its older tokenizer and about 555 on its newest one. For Bangla it is usually fewer words. Use your provider's token counter for exact numbers.
Does setting temperature to 0 make answers always the same? No. Low temperature makes answers more consistent, but providers say output is still not fully deterministic. Some newer APIs do not allow you to set temperature at all.
Can an LLM learn from my conversations? Not during your chat. Each API call is independent and the model's parameters do not change. Apps can save notes and add them to later prompts, which feels like memory.
Key Takeaways
- How LLMs work: they predict the next token, one at a time, based on patterns learned in training.
- Training happens once at the AI company; inference happens on every API call you make.
- Tokens are not words, and Bangla text often needs more tokens than English for the same meaning.
- The context window is the model's desk. As of November 2026, major flagship models offer around 1M tokens.
- Temperature and top-p control randomness, but even low settings are not fully deterministic.
- Hallucinations come from predicting likely text, not checking facts. Give sources and verify output.
- Models do not remember between chats; your app resends the history.
- You pay per input and output token, so trim prompts and choose the right model size.
Related articles
What AI agents really are: the observe-think-act loop, tool calling, memory, MCP, common failures like looping and prompt injection, and practical guardrails.
How Retrieval-Augmented Generation lets AI answer from your own documents: the pipeline, embeddings, a small Python example, common failures and how to evaluate.
Ten practical prompt engineering rules for developers, with before/after examples, JSON output formats, eval tables and a prompt injection warning.
MCP explained simply: the N×M problem, hosts, clients and servers, tools, resources and prompts, stdio vs Streamable HTTP, the 2026 spec and security tips.