Skip to content

LLM application performance: latency and token cost

B2Bnetwork team · · 6 min

System performance

In brief

The performance of an application built on a large language model is measured differently from a regular API: time to first token, the rate of generating further tokens, total response time and token cost per business operation. Time and cost grow with context length, answer length and the number of model calls in one task. A load test of such an application uses real questions from logs, checks the provider's limits and calculates cost as it runs, because with an LLM every test comes with its own bill.

Metrics that matter

For a regular API, response time percentiles are enough. In an LLM application the answer is produced gradually, token by token, so a single time does not tell the whole story.

MetricWhat it tells youWhen it matters
Time to first tokenhow long the user waits before seeing the start of the answerchat, in-app assistants, streamed answers
Tokens per secondhow fast the answer growslong answers, summaries, document drafts
Total timewhen the result is completedocument processing, process steps, integrations
Input and output tokens per operationwhat one task costsevery deployment, especially at high volume
Error and refusal rateprovider limits, timeouts, content filterstraffic peaks, long tasks

Quality comes on top of these. A faster answer with worse content is not an improvement, so a performance test is best paired with a test set that checks the correctness of results. The OpenTelemetry semantic conventions for generative AI propose common names for attributes and metrics of model calls, which makes it easier to collect this data in one tool.

Where time and cost come from

A language model first processes the whole input context and then generates the answer. That gives four sources of time and cost:

  1. Context length. The system prompt, conversation history and document chunks from retrieval (RAG) all count as input tokens on every call. A dozen chunks of a few hundred tokens each easily adds up to several thousand tokens per question.
  2. Answer length. Generating output tokens takes longer and usually costs more than processing input. A limit on answer length is the simplest lever.
  3. Number of calls per task. An application that first classifies the case, then retrieves documents, then calls a tool and finally writes the answer pays in time and tokens for every step. Five calls of two seconds each make ten seconds for the user.
  4. Queuing at the provider or on your own server. A cloud service has request and token limits per minute. Your own GPU server can handle a limited number of parallel requests, and above that, requests wait in a queue.

How to choose the model and where it runs is covered in A language model in the cloud or on your own server. The cost of the model itself is discussed in The cost of language models.

How to calculate the cost of an operation

Cost is calculated per business operation, not per model call. An example to reason with: an application answers customer questions, one question averages 2,000 input tokens (system prompt, document chunks, the question) and 300 output tokens, and 50,000 questions come in each month. That is 100 million input tokens and 15 million output tokens a month. The cost is 100 times the price per million input tokens plus 15 times the price per million output tokens, using the current price list of the chosen model.

This calculation shows straight away where to look for savings. When the system prompt and fixed documents make up half of the input, prompt caching helps, and many providers offer it: the repeated beginning of the context is processed once, and later cache reads are cheaper and faster. Providers usually require a minimum length for the cached prefix, and writing to the cache can cost more than regular input, so it pays off for repetitive requests.

Levers: what cuts time and cost

LeverEffect on timeEffect on costWatch out for
Fewer, better chosen RAG chunksshorter time to first tokenfewer input tokenslower quality if a needed chunk is cut
Limit on answer lengthshorter total timefewer output tokenstruncated answers
Prompt cachingshorter time with a repeated prefixcheaper readsminimum length, cache lifetime
A smaller model for simple stepsfaster simple stepslower token pricequality has to be measured separately
Parallel steps instead of a chainshorter task timeno changedependencies between steps
Streaming the answershorter perceived timeno changedoes not help in processes without a person

Each lever is checked with two measures at once: time and quality on the same test set. A change that halves the time but spoils one answer in ten does not pass.

Load testing an LLM application

Testing an LLM application differs from a regular load test in several ways:

  1. Real questions from logs, with their length distribution. The same short question a hundred times will hit the cache and understate both time and cost.
  2. Measuring the stream: the test tool has to record the moment of the first token and the end of the answer, not just the end of the HTTP request.
  3. Provider limits: the test shows at what traffic rate-limit responses appear and whether the application retries them sensibly, with a delay.
  4. A cost meter: tokens from every response go into the test results. An hour-long test at peak scale can cost a noticeable amount, so set a budget for the test.
  5. Protection against excessive consumption: token limits per user and per request protect you from a bill caused by abuse, which OWASP describes as the risk of unbounded consumption. Security is covered more broadly in LLM application security.

An example where an embedding model sits in the customer journey with a p99 API latency target is the case study on semantic search and product recommendations. At B2Bnetwork, performance testing of LLM applications comes with a cost-per-operation measurement. AI Workers that take over processes in companies are deployed by DYNAMINDS Labs.

Questions and answers

What response time is acceptable in an LLM application?

It depends on who is waiting. In a chat, time to first token matters, because the user reads the answer as it is generated. In a process without a person, for example describing invoices, what matters is total time and throughput per hour. Set the threshold for each case separately, in percentiles.

Is a self-hosted model faster than a cloud service?

Not always. Your own server has no provider limits or network latency, but it handles a limited number of parallel requests per GPU. At low traffic it can be faster; at high traffic it needs more hardware or a queue. A load test with real traffic and a comparison of cost per operation settles it.

How do you limit the cost of load testing with a paid model?

Set a token budget before the test, start with short runs and run at full scale only the passes you need. For testing the application infrastructure alone, you can replace the model with a stub that has realistic latency. Run the full test with the real model when you are checking provider limits and cost.

Does prompt caching always pay off?

No. It helps when many requests start with the same long prefix, for example a system prompt and a fixed set of documents. With short or always different contexts it brings no benefit, and writing to the cache can cost more than regular input. Check the effect in a test on real requests.

What it looks like in a project

Your system slows down under load? We'll find where it loses time.

Let's talk

Read more: System performance

All in System performance →