0Pricing
AI Engineering Academy · レッスン

LLMレイテンシの測定:TTFTとTPOT

Time to First TokenとTime Per Output Tokenを2つの主要なレイテンシ指標として定義し、アプリケーションに計測機能を組み込んで両方を測定し、エンドポイントごとのSLA目標を設定します。

「LLMレイテンシの測定:TTFTとTPOT」はCoddyKit上の無料AI Engineering Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAI Engineering Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 AI Engineering Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why LLM Latency Has Two Components

Measuring LLM latency as a single number is misleading. There are actually two distinct phases: the time until the first token arrives (perceived responsiveness), and the time it takes to generate subsequent tokens (output speed). A model can have great TTFT but slow TPOT, making long responses feel sluggish even though the initial response felt instant.

TTFT: Time to First Token

Time to First Token (TTFT) is the duration from sending the API request to receiving the very first token of the response. It includes network latency, queuing time at the inference server, and prefill time (processing the input tokens). TTFT dominates perceived responsiveness — users notice when nothing appears for more than 1-2 seconds, regardless of how fast tokens stream afterward.

import time
from openai import OpenAI

client = OpenAI()

def measure_ttft(prompt: str) -> float:
    start = time.perf_counter()
    first_token_time = None
    stream = client.chat.completions.create(
        model='gpt-4o',
        messages=[{'role': 'user', 'content': prompt}],
        stream=True
    )
    for chunk in stream:
        if chunk.choices[0].delta.content:
            first_token_time = time.perf_counter()
            break  # stop after first token
    return first_token_time - start

TPOT: Time Per Output Token

Time Per Output Token (TPOT) is the average time between successive tokens once generation has started. It is calculated as total generation time divided by total output tokens. TPOT determines reading speed — humans read at roughly 250 words per minute, so a TPOT above 100ms per token (10 tokens/second) will feel noticeably slow for long responses.

import time
from openai import OpenAI

client = OpenAI()

def measure_tpot(prompt: str) -> dict:
    start = time.perf_counter()
    first_token_time = None
    token_count = 0
    stream = client.chat.completions.create(
        model='gpt-4o',
        messages=[{'role': 'user', 'content': prompt}],
        stream=True
    )
    for chunk in stream:
        delta = chunk.choices[0].delta.content or ''
        if delta:
            if first_token_time is None:
                first_token_time = time.perf_counter()
            token_count += 1
    end = time.perf_counter()
    ttft = first_token_time - start
    generation_time = end - first_token_time
    tpot = generation_time / max(token_count, 1)
    return {'ttft_ms': ttft * 1000, 'tpot_ms': tpot * 1000, 'tokens': token_count}

Total Latency vs TTFT vs TPOT

The relationship between these metrics is: total_latency = TTFT + (output_tokens × TPOT). For a response of 500 tokens at 50ms TPOT, generation takes 25 seconds. For streaming applications, optimize TTFT first — users tolerate slow streaming better than a blank screen. For batch processing without streaming, total latency is what matters, so optimize TPOT by choosing models with faster inference.

# Latency breakdown for a 200-token response
ttft_ms = 450       # half a second to first token
tpot_ms = 25        # 25ms per token = 40 tokens/sec
output_tokens = 200

total_latency = ttft_ms + (tpot_ms * output_tokens)
print(f'TTFT:  {ttft_ms}ms')
print(f'Generation: {tpot_ms * output_tokens}ms')
print(f'Total: {total_latency}ms ({total_latency/1000:.1f}s)')
# Output:
# TTFT:  450ms
# Generation: 5000ms
# Total: 5450ms (5.5s)

Factors That Affect TTFT

TTFT is dominated by prefill cost, which scales with the number of input tokens. A 10,000-token system prompt will have 10x higher TTFT than a 1,000-token prompt, all else being equal. Other factors include server load (queue time), network round-trip time to the API endpoint, and whether prompt caching reduces the effective prefill work. Minimize system prompt length to reduce TTFT.

# TTFT scales approximately linearly with input tokens
# Measured typical values for gpt-4o (2026):
# 500 input tokens:   ~400ms TTFT
# 2000 input tokens:  ~600ms TTFT
# 10000 input tokens: ~1500ms TTFT
# 32000 input tokens: ~4000ms TTFT

# Use prompt caching to avoid paying for repeated long prefixes:
# Cached tokens: ~200ms saved per 1000 cached tokens

Factors That Affect TPOT

TPOT is primarily determined by the model size and hardware. Smaller models (GPT-4o-mini) decode much faster than large models (GPT-4o). On self-hosted models, batch size and GPU memory bandwidth are the main factors. For OpenAI-hosted models, TPOT varies with server load but is typically 15-40ms per token. You cannot directly control TPOT on hosted APIs — model selection is your main lever.

# Typical TPOT benchmarks (approximate, 2026):
# gpt-4o-mini:   15-20ms per token (50-65 tokens/sec)
# gpt-4o:        25-40ms per token (25-40 tokens/sec)
# claude-3.5-haiku: 20-25ms per token
# claude-3.5-sonnet: 30-50ms per token
# local llama-3.1-8B on A100: 8-12ms per token
# local llama-3.1-70B on 4xA100: 25-35ms per token

Setting SLA Targets Per Endpoint

Not all endpoints need the same latency target. A chat endpoint has a tight TTFT SLA (users expect <500ms), while a batch summarization endpoint can tolerate multi-second latency. Define explicit SLA targets per endpoint and instrument each one separately. Common targets: interactive chat = p95 TTFT <600ms, document extraction = p95 total <10s, batch = no real-time SLA.

SLA_TARGETS = {
    'chat':       {'ttft_p95_ms': 600,  'total_p95_ms': 8000},
    'extraction': {'ttft_p95_ms': 1500, 'total_p95_ms': 10000},
    'summary':    {'ttft_p95_ms': 2000, 'total_p95_ms': 30000},
    'batch':      {'ttft_p95_ms': None, 'total_p95_ms': None},
}

Logging Latency Metrics

Log TTFT and TPOT for every production request with structured logging. Include the model name, endpoint, prompt token count, output token count, and whether a cache hit occurred. This gives you the data to compute percentile distributions (p50, p95, p99), spot regressions after model updates, and correlate latency spikes with queue depth or provider incidents.

import structlog

log = structlog.get_logger()

def log_latency(endpoint: str, model: str, metrics: dict):
    log.info(
        'llm_latency',
        endpoint=endpoint,
        model=model,
        ttft_ms=round(metrics['ttft_ms'], 1),
        tpot_ms=round(metrics['tpot_ms'], 1),
        output_tokens=metrics['tokens'],
        total_ms=round(metrics['ttft_ms'] + metrics['tpot_ms'] * metrics['tokens'], 1)
    )

Computing Percentiles from Samples

Raw averages are misleading for latency — a few slow outliers inflate the mean without affecting most users. Always report p50, p95, and p99 percentiles. P95 is the most common SLA metric: it means 95% of requests completed within that time. Use NumPy or the statistics module to compute percentiles from your logged latency samples.

import numpy as np

def compute_percentiles(samples: list, label: str = 'latency_ms'):
    arr = np.array(samples)
    stats = {
        'count': len(arr),
        'p50': np.percentile(arr, 50),
        'p95': np.percentile(arr, 95),
        'p99': np.percentile(arr, 99),
        'mean': np.mean(arr),
        'max': np.max(arr)
    }
    print(f'{label}:')
    for k, v in stats.items():
        print(f'  {k}: {v:.1f}')
    return stats

Reducing Latency with max_tokens

Setting an appropriate max_tokens limit reduces worst-case total latency by preventing runaway long responses. If your use case needs at most 200-token answers, set max_tokens=250. This also caps cost. Combine with streaming to ensure users see output immediately while the full response is still generating. Never leave max_tokens unlimited in production endpoints.

response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{'role': 'user', 'content': question}],
    max_tokens=300,  # cap at 300 tokens
    stream=True
)

# With max_tokens=300 and TPOT=30ms:
# worst-case total generation = 9000ms
# Without limit: could run to 4096+ tokens = 123s+

Latency Optimization Priorities

When latency is too high, address bottlenecks in priority order. First, enable streaming so users see output immediately even if total latency is high. Second, shorten the system prompt to reduce TTFT. Third, add prompt prefix caching to amortize prefill cost across repeated requests. Fourth, switch to a smaller model if quality allows. Finally, consider self-hosted inference for maximum control over both TTFT and TPOT.

# Latency optimization checklist (in priority order):
# 1. Enable streaming (perceived latency: immediate)
# 2. Shorten system prompt by 50% (TTFT: -20%)
# 3. Enable prompt prefix caching (TTFT: -40% on cache hits)
# 4. Downgrade to gpt-4o-mini for simple queries (TPOT: -40%)
# 5. Self-host llama-3.1-8B for high-volume simple queries
#    (TPOT: 8ms vs 25ms; TTFT: 100ms vs 450ms)

Quick Check

Test your understanding of TTFT and TPOT as LLM latency metrics.

Lesson Recap

In this lesson you learned: TTFT (Time to First Token) measures perceived responsiveness and scales with input token count, TPOT (Time Per Output Token) determines generation speed and is mainly controlled by model size, and per-endpoint SLA targets with percentile metrics are the right way to monitor latency in production. Next up we implement load balancing across multiple API keys.

よくある質問

「LLMレイテンシの測定:TTFTとTPOT」レッスンは無料ですか?

はい。「LLMレイテンシの測定:TTFTとTPOT」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、AI Engineering Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 AI Engineering Academyコースには全4レッスンが含まれています。

「LLMレイテンシの測定:TTFTとTPOT」で何を学びますか?

Time to First TokenとTime Per Output Tokenを2つの主要なレイテンシ指標として定義し、アプリケーションに計測機能を組み込んで両方を測定し、エンドポイントごとのSLA目標を設定します。 ブラウザで直接実行するハンズオンコードでAI Engineering Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

AI Engineering Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAI Engineering Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「LLMレイテンシの測定:TTFTとTPOT」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAI Engineering Academyレッスンでコードを書いて実行できますか?

はい。すべてのAI Engineering Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. LLMレイテンシの測定:TTFTとTPOT
  2. 負荷分散と複数キー戦略
  3. フォールバックプロバイダーとサーキットブレーカー
  4. タイムアウト予算とグレースフルデグラデーション
← AI Engineering Academyに戻る