Skip to content
derpx06Notes on systems, models & learning
8. Production, Evaluation & Governance · lesson 62 of 68 · 1 min · January 10, 2026

Latency & TTFT Optimization

The psychology of waiting. Streaming vs Bulk.

User A waits 5 seconds for a full paragraph. User B waits 0.5 seconds for the first word, then watches the rest appear.

User A thinks the system is broken. User B thinks it is fast. TTFT (Time To First Token) is the most important metric for User Experience.

In LangChain, never use .invoke() for a chatbot. Use .stream(). It yields tokens as they are generated.

streaming.py
for chunk in chain.stream("Why represents the sky?"):
  print(chunk, end="", flush=True)
  # The user sees progress instantly.

Latency = Prefill Time + Generation Time.

  • Prefill: Time to process your input prompt.
  • Generation: Time to write the answer.

If you shove 100 documents into the context "just in case," your Prefill Time explodes. Pruning retrieval (Contextual Compression) saves latency.

If two users ask "What is the capital of France?", why compute it twice? Semantic Caching stores (Question_Embedding -> Answer). If a new question is 99% similar to a cached question, return the cached answer instantly (0ms latency).

Latency is physics (Model Size) + engineering (Streaming/Caching). You cannot change physics. You must master engineering.

Key Intuition: "Don't make them wait. Make them watch."