liveBook · Manning

Time to first token

As explained in LLMs in Production

Time to first token is the elapsed time between submitting a language-model request and receiving its first generated token.

A streaming service may complete a request quickly yet feel slow if users wait before seeing any output. Time to first token (TTFT) measures that initial wait: the elapsed time from request submission until the first generated token arrives. section 6.3.2

Use TTFT with complementary service and user-experience metrics:

  • Compare TTFT with total latency, which measures complete-request duration. section 6.3.2
  • Track tokens per second (TPS) to measure streaming speed after the first token. section 6.3.2
  • Record CPU, GPU, and memory utilization during load tests to expose resource pressure. section 6.3.2

Measuring TTFT with Locust

A streaming Locust request records the request start time, captures the first response timestamp, subtracts the two, and writes TTFT alongside latency and TPS to CSV. section 6.3.2

Listing 6.12 Load testing with Locust
import time
from locust import HttpUser, task, events

stat_file = open("stats.csv", "w")      #1
stat_file.write("Latency,TTFT,TPS\n")


class StreamUser(HttpUser):
    @task
    def generate(self):
        token_count = 0           #2
        start = time.time()

        with self.client.post(       #3
            "/generate",
            data='{"prompt": "Salt Lake City is a"}',
            catch_response=True,
            stream=True,
        ) as response:
            first_response = time.time()
            for line in response.iter_lines(decode_unicode=True):
                token_count += 1

        end = time.time()          #4
        latency = end - start
        ttft = first_response - start
        tps = token_count / (end - first_response)

        stat_file.write(f"{latency},{ttft},{tps}\n")     #5


# Close stats file when Locust quits
@events.quitting.add_listener
def close_stats_file(environment):
    stat_file.close()

Inspect the complete Python load-test listing, including streaming iteration, timestamp capture, and CSV reporting. section 6.3.2

How LLMs in Production covers it

How deep it goes
Defines TTFT as delay until feedback and computes it in a Locust streaming test, without prescribing a TTFT target.
Examples
Uses a Locust streaming request to record timestamps and export TTFT, latency, and TPS to CSV.
What it assumes
Assumes streaming responses and basic load-testing measurements while distinguishing perceived latency from total latency.
What you can do after
Diagnose perceived responsiveness by measuring TTFT alongside latency and tokens per second in a Locust load test.
How it connects
Links TTFT with latency, throughput, tokens per second, intertoken latency, and resource utilization.

Also explained in

Compare how 3 books explain time to first token →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.