Quick revision
The short answer
Tokens are the sub-word pieces a model reads and writes; providers count and charge for input and output tokens separately. The context window is the maximum number of tokens one request can hold, including the system prompt, history, retrieved documents and the answer. Temperature controls how random the sampling of each next token is, and max output tokens caps how long the answer may be, which matters because output tokens are generated one by one and dominate both latency and cost.
What to remember
In this answer, we saw that tokens are the sub-word pieces that everything is counted and priced in, with output tokens usually costing more than input. The context window is one shared budget for instructions, history, documents and the answer. Temperature controls sampling randomness, low for repeatable tasks and higher for variety. Max output tokens caps the answer, and since output is generated one token at a time it dominates latency. Long prompts delay the first token, and prompt caching can reduce the cost of repeated prefixes.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Tokens are the sub-word pieces a model reads and writes; providers count and charge for input and output tokens separately. The context window is the maximum number of tokens one request can hold, including the system prompt, history, retrieved documents and the answer. Temperature controls how random the sampling of each next token is, and max output tokens caps how long the answer may be, which matters because output tokens are generated one by one and dominate both latency and cost.
Why this question is asked
The first time we call an LLM API from Java, the request has a handful of parameters that look harmless: a model name, a list of messages, a temperature and a max tokens value. Each of them decides how much the call costs, how long the user waits and what comes back. Interviewers ask about them because developers who do not understand them build features that are slow, expensive or flaky, and then blame the model. In this answer, we will explain tokens, the context window, temperature and max output tokens one by one, see how each affects cost, latency and output, and then set them from Spring AI.
1What a Token Is
A model does not read characters or words. Before our text reaches the model, a tokenizer splits it into tokens: common words become one token, rarer words are split into several pieces, and punctuation and spaces are tokens or parts of tokens too. For ordinary English, one token is roughly four characters, or about three quarters of a word, so 1,000 words is around 1,300 tokens.
That ratio changes with the content. Code uses more tokens per line than prose, because identifiers like getCustomerOrderTotal and symbols like braces are split into many pieces. Many non-English languages also need more tokens per word, because the tokenizer was built mostly from English text. So the same message translated into another language can cost noticeably more.
Tokens matter because everything is counted in them. Providers price input tokens (what we send) and output tokens (what the model writes) separately, usually per million tokens, and output tokens typically cost several times more than input tokens. Rate limits and the context window are counted in tokens too.
Notice that 300 output tokens cost almost as much as 2,000 input tokens. That is typical, and it is the first reason to keep answers short.
2The Context Window: Everything Must Fit
The context window is the maximum number of tokens the model can consider in one request. It is a single budget shared by everything: the system prompt with our instructions, the conversation history, any documents we retrieved and pasted in, and the answer the model is about to write. If a model has a 128,000-token window and we send 126,000 tokens of input, there is room for only 2,000 tokens of answer.
The model has no memory between calls. If a chat feature seems to remember earlier messages, it is because our code sends the history again with every request. So a long conversation grows the input with each turn, and with it the cost and the waiting time, until we trim or summarise old messages. When the input is larger than the window, the API rejects the request with an error, so long histories need a trimming strategy rather than hope.
3Temperature: How Random Each Next Token Is
A model writes its answer one token at a time. At each step it computes a probability for every possible next token, and then one token is sampled from that distribution. Temperature reshapes the distribution before sampling. A low temperature, close to 0, makes the most likely token win almost every time, so the output is focused and repeatable. A higher temperature, around 0.8 to 1.0, flattens the distribution, so less likely tokens get picked more often and the output becomes more varied.
So the choice follows the task. For extraction, classification and anything that must parse as JSON, we use a low temperature such as 0 to 0.2. For brainstorming names or marketing drafts, a higher value gives more variety. Temperature does not change cost or speed directly; it changes what we get back.
4Max Output Tokens: The Cap on the Answer
Max output tokens (often called max_tokens) is a hard limit on how many tokens the model may generate. When the limit is reached, generation stops mid-sentence and the response reports a finish reason such as length instead of a normal stop, so our code should check it. A truncated JSON object will not parse, which is a common surprise.
This setting matters for latency more than any other, because of how generation works. The whole input is processed in one parallel pass, which is fast, but output tokens come out strictly one after another. If a model produces 50 tokens per second, a 1,000-token answer takes 20 seconds, while a 100-token answer takes 2. So long answers are both the slowest and the most expensive part of a call.
| Setting | What it is | Effect on cost | Effect on latency and output |
|---|---|---|---|
| Input tokens | Everything we send | Charged per token, cheaper rate | Longer prompt, later first token |
| Context window | Max tokens per request, input plus output | Bigger requests cost more | Too much input leaves no room for the answer |
| Temperature | Sampling randomness | None directly | Low: focused and repeatable; high: varied |
| Max output tokens | Cap on generated tokens | Caps the costliest tokens | Bounds total time; may truncate output |
5Long Prompts and Prompt Caching
A long prompt does not slow every token, but it delays the first one. Before the model can write anything, it has to process the whole input, so a 50,000-token prompt with many retrieved documents can add seconds before the first word appears. That delay is called time to first token, and users feel it most.
Some providers offer prompt caching for exactly this case. If many requests start with the same long prefix, such as the same instructions and the same reference document, the provider can keep the processed prefix and charge much less for it on later calls, often with a faster first token too. To benefit, we put the stable parts of the prompt first and the changing parts, like the user's question, last. The exact discount, cache lifetime and how to enable it differ by provider, so we check the documentation.
6Setting These Values From Spring AI
In Spring AI we can set defaults in configuration and override them per call with ChatOptions. After the call, the response metadata gives the real token counts, which we should log for cost tracking.
Notice how small maxTokens is for a classification: there is no reason to allow a long answer, and the cap protects us if the model starts explaining itself. Method names on Usage have changed between Spring AI versions, so check your version.
7Follow-up questions
Why does the same prompt give different answers at temperature 0 sometimes?
Temperature 0 means the model should pick the most likely token at each step, but several things still introduce variation. GPUs add floating-point numbers in different orders depending on how requests are batched together on the server, and tiny differences can change which of two nearly tied tokens wins; once one token differs, the rest of the answer follows a different path. Some providers do not implement pure greedy decoding even at 0, and a model alias can also point to an updated model version. So temperature 0 makes output much more consistent, but not guaranteed identical; where we need exact repeatability, we cache results or validate them in code.
Should we always use the largest context window available?
No. A large window lets us send more, but every extra token costs money and delays the first token, and models tend to use information in the middle of very long prompts less reliably than information at the start or end. It is usually better to retrieve the few most relevant passages than to paste in everything.
How do we count tokens accurately in Java before sending?
The response always reports the exact counts, which is the number to log and bill on. For estimates before sending, we can use the provider's tokenizer if it publishes one; for OpenAI-style tokenizers the jtokkit library implements the encodings in Java. A characters-divided-by-four estimate is enough for budgeting.
8Conclusion
In this answer, we saw that tokens are the sub-word pieces that everything is counted and priced in, with output tokens usually costing more than input. The context window is one shared budget for instructions, history, documents and the answer. Temperature controls sampling randomness, low for repeatable tasks and higher for variety. Max output tokens caps the answer, and since output is generated one token at a time it dominates latency. Long prompts delay the first token, and prompt caching can reduce the cost of repeated prefixes.
Open with
Tokens are the pieces the tokenizer splits text into, roughly four characters of English each, and more per word for code and many other languages. Everything is counted in tokens: providers charge separately for input and output tokens, usually per million, and output is typically several times more expensive.
Then, if they want more
The context window is the maximum number of tokens the model can consider in one request, and it is shared by the system prompt, the conversation history we resend each time, retrieved documents and the answer itself.
Temperature reshapes the probability distribution the next token is sampled from: near zero gives focused, repeatable output, which I use for extraction and classification, and higher values give more variety. Max output tokens caps the answer; I set it tight, and I check the finish reason because a truncated answer, especially truncated JSON, is a common bug.
For latency, the input is processed in parallel, so a long prompt mainly delays the first token, but output comes out one token at a time, so long answers dominate total time and cost. If many requests share a long prefix, prompt caching from some providers makes that prefix cheaper, so I put stable instructions first.
And temperature zero is not perfectly deterministic, because of floating-point effects from batching on the GPUs.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring AI reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain to a Java developer what tokens, context window, temperature and max output...": https://scrivio-interview-library.netlify.app/spring-boot/llm-fundamentals/9d26ff62d3/