Quick revision
The short answer
The cost of an LLM call is mostly the number of input and output tokens multiplied by the model's per-token prices, with output tokens usually costing several times more than input. Latency depends on the model's size, the length of the output and the provider's load, and for streaming the time to first token is what users feel. Long contexts, such as large RAG prompts or long chat memory, increase both. In Spring AI we read token usage from the ChatResponse metadata and from Micrometer observations, and we track cost and latency per feature and per user.
What to remember
In this answer, we saw that cost is input tokens and output tokens times per-token prices that vary by model, with context we add usually dominating the input and output tokens costing more each. Latency comes from queueing, prefill and especially decoding, with time to first token mattering for streaming, and long contexts from RAG and chat memory raise both. We read token usage from the ChatResponse metadata and Micrometer observations in Spring AI and tracked cost and latency per feature and per user.
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.
The cost of an LLM call is mostly the number of input and output tokens multiplied by the model's per-token prices, with output tokens usually costing several times more than input. Latency depends on the model's size, the length of the output and the provider's load, and for streaming the time to first token is what users feel. Long contexts, such as large RAG prompts or long chat memory, increase both. In Spring AI we read token usage from the ChatResponse metadata and from Micrometer observations, and we track cost and latency per feature and per user.
Why this question is asked
The first invoice from a model provider often surprises a team, and so does the first user complaint that the assistant is slow. Both come from the same few drivers, and once we understand them we can predict, measure and reduce them. Interviewers ask this to check that we can reason about an LLM feature's economics with numbers, not just call the API. In this answer, we will recap what a token is, calculate the cost of a call, break latency into its parts, see why long contexts hurt twice, measure everything from Spring AI, and look at the levers that bring cost and latency down.
1A Quick Recap of Tokens
Models do not read characters or words; they read tokens, pieces of text produced by the model's tokenizer. In English, a token is on average about three quarters of a word, so 1,000 tokens are roughly 750 words. Code, other languages and unusual words use more tokens per word. Every request has input tokens, everything we send, which includes the system message, chat history, retrieved documents, tool descriptions and the question, and output tokens, everything the model generates. Providers price these separately, per million tokens.
2Calculating the Cost of a Call
Let us take a support assistant call with illustrative prices of 3 dollars per million input tokens and 15 dollars per million output tokens. Real prices differ widely by model, often by a factor of 10 or more between a small and a large model, so we always use the current price list.
Notice two things. Output tokens cost five times as much here, so a verbose answer is expensive. And the input is dominated by context we add, not by the user's question. A 15-word question costs almost nothing; the 2,000 tokens of retrieved documents we attach to it are most of the input cost. Some providers also charge less for repeated prompt prefixes through prompt caching, which matters when every call starts with the same long system message.
3Where the Latency Comes From
An LLM call has three phases. First, network and queueing: the request reaches the provider and may wait if the provider is busy. Second, prefill: the model reads all input tokens; this is fast per token but grows with long prompts. Third, decoding: the model generates output one token at a time, and this is usually the largest part. A model might produce 50 to 100 tokens per second, so a 500-token answer takes 5 to 10 seconds of decoding alone. Larger models are generally slower per token, and provider load can vary latency during the day.
For a streamed answer, users feel the time to first token, the network, queue and prefill time, which is often under a second, and then they read while the rest arrives. For a blocking call, they feel the total. So the same model can feel fast in a chat UI with streaming and slow in a form that waits for the full result.
- Network and provider queuetens to hundreds of ms, more under load
- Prefill: read 2,400 input tokensgrows with context length
- First token arrivestime to first token, about 0.5 to 1 s
- Decode 500 output tokensat 50 tokens/s, about 10 s
- Response completetotal latency for a blocking call
5 steps in order. Play them, or step through them one at a time.
Output length usually dominates; context length adds to both prefill and cost.
4Why Long Contexts Hurt Twice
Context is everything we send besides the question. Retrieval that attaches 10 chunks of 500 tokens adds 5,000 input tokens to every call. Chat memory that keeps 40 messages may add another 6,000 by the end of a long conversation, and it grows with each turn, so the 20th message in a chat costs far more than the first. Tool descriptions are also sent on every call. All of these increase cost linearly and add prefill time. Retrieving 4 well-chosen chunks instead of 10, keeping a window of the last 10 messages or a summary of older ones, and only offering the tools a request needs often cut input tokens by half without hurting quality, which the evaluation set confirms.
5Measuring Tokens and Latency in Spring AI
Every ChatResponse carries usage metadata from the provider. We read it when we need per-call numbers, for example to record cost per user in our own table.
For aggregate numbers we do not need custom code. Spring AI records Micrometer observations for chat calls, including duration and token usage, with tags such as the model name; with Prometheus they become metrics we can chart and alert on. The metric names follow the OpenTelemetry conventions for generative AI and may differ by version. We add our own low-cardinality tags such as the feature name, and keep user ids out of metric tags, since one series per user would overload the metrics system; per-user cost goes into logs or a usage table instead.
6Tracking Per Feature and Per User
Totals hide the story. A dashboard per feature shows cost per day, average input and output tokens, p50 and p95 latency and time to first token, so we can see that the summarising job costs more than the chat assistant, or that one feature's prompt doubled after a release. Per-user tracking finds heavy users and abuse, such as one account sending 5,000 requests a day, and supports per-user quotas. With these numbers, a decision such as moving a feature to a smaller model that costs a tenth as much becomes a measured trade-off checked against the evaluation set.
7Follow-up questions
How can we reduce cost without hurting quality?
Send less context by retrieving fewer, better chunks and trimming chat history, cap and shorten outputs, use a smaller model for simple tasks such as classification, cache answers to repeated questions, and use provider prompt caching for long fixed prefixes. Each change is checked against the evaluation set before release.
Why is the cost of a chat growing during a conversation?
Because each new message sends the whole history again as input. The 20th turn carries 19 earlier turns. A message window or a summary of older turns keeps the input size bounded.
Can we estimate tokens before calling the model?
Roughly, by counting characters and dividing by about four for English, or precisely with the provider's tokenizer if one is available for Java. An estimate is useful to reject or trim requests that would exceed the context limit or a per-request budget.
8Conclusion
In this answer, we saw that cost is input tokens and output tokens times per-token prices that vary by model, with context we add usually dominating the input and output tokens costing more each. Latency comes from queueing, prefill and especially decoding, with time to first token mattering for streaming, and long contexts from RAG and chat memory raise both. We read token usage from the ChatResponse metadata and Micrometer observations in Spring AI and tracked cost and latency per feature and per user.
Open with
Cost is basically tokens times price. Every call has input tokens, which include the system prompt, chat history, retrieved documents and tool descriptions, not just the question, and output tokens, and providers charge per million of each, with output usually several times more expensive and prices varying a lot by model.
Then, if they want more
For example, 2,400 input and 500 output tokens at three and fifteen dollars per million is about one and a half cents a call, which is around forty thousand dollars a month at a hundred thousand calls a day.
Latency has three parts: network and queueing at the provider, prefill where the model reads the input, and decoding, which generates tokens one at a time, maybe fifty to a hundred per second, so output length dominates. Bigger models are slower, and provider load varies. For streaming, the time to first token is what users feel.
Long contexts, big RAG prompts or long chat memory, increase both cost and prefill time, and chat history grows with every turn. To measure it in Spring AI, I read token usage from ChatResponse metadata when I need per-call numbers, and rely on Micrometer observations for duration and token metrics per model.
I tag by feature, keep per-user cost in logs or a usage table, and track both per feature and per user.
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 "What drives the cost and latency of an LLM call from a Java service, and how do you...": https://scrivio-interview-library.netlify.app/spring-boot/spring-ai/ba2c65fe71/