Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 9 · 8. Production, Evaluation & Governance
Lesson 62 of 68

Latency & TTFT Optimization

The psychology of waiting. Streaming vs Bulk.

Derpx0610 Jan · 1 min read
LangChain Mastery
Async & High-Throughput PatternsLatency & TTFT OptimizationTracing with LangSmithRAG Evaluation BasicsFaithfulness & RelevancePrompt Injection DefenseInput & Output GuardrailsDeployment & API Serving
Contents
1. Time To First Token (TTFT)2. Streaming3. Optimizing Prompt Size (Pre-fill Time)4. Caching5. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
Async & High-Throughput PatternsLatency & TTFT OptimizationTracing with LangSmithRAG Evaluation BasicsFaithfulness & RelevancePrompt Injection DefenseInput & Output GuardrailsDeployment & API Serving
Contents
1. Time To First Token (TTFT)2. Streaming3. Optimizing Prompt Size (Pre-fill Time)4. Caching5. Summary

1. Time To First Token (TTFT)

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.

2. Streaming

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.

3. Optimizing Prompt Size (Pre-fill Time)

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.

4. Caching

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

5. Summary

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