Learn/LangChain Mastery/Part 9 · 8. Production, Evaluation & Governance
Lesson 62 of 68
SyllabusLangChain Mastery · Part 9

Latency & TTFT Optimization

The psychology of waiting. Streaming vs Bulk.

Course outline & contents
LangChain Mastery
Async & High-Throughput PatternsLatency & TTFT OptimizationTracing with LangSmithRAG Evaluation BasicsFaithfulness & RelevancePrompt Injection DefenseInput & Output GuardrailsDeployment & API Serving

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."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonAsync & High-Throughput PatternsNext lesson →Tracing with LangSmith