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 61 of 68

Async & High-Throughput Patterns

Handling scale. Why simple loops crash in production.

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. The Synchronous Bottleneck2. Async in LangChain3. Rate Limits & Backpressure4. Batching for Throughput5. 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. The Synchronous Bottleneck2. Async in LangChain3. Rate Limits & Backpressure4. Batching for Throughput5. Summary

1. The Synchronous Bottleneck

In a prototype, you write: response = llm.invoke("Hi") This code blocks the CPU for 2 seconds. If you have 100 users, User #100 waits 200 seconds (3 minutes).

Synchronous LLM calls are death for scale. Production systems must be Asynchronous and Parallel.

2. Async in LangChain

Python's asyncio is the key. Instead of invoke, use ainvoke. Instead of waiting, the CPU switches context to handle another request while the GPU computes.

async_batch.py
import asyncio

async def handle_requests(inputs):
  # This runs 5 calls in PARALLEL.
  # Total time = Max(Time), not Sum(Time).
  tasks = [chain.ainvoke(i) for i in inputs]
  results = await asyncio.gather(*tasks)
  return results

3. Rate Limits & Backpressure

If you parallelize too much, you hit the API Rate Limit (e.g., 5000 TPM). You need a Semaphore to control concurrency.

semaphore = asyncio.Semaphore(10) (Max 10 active requests).

4. Batching for Throughput

GPUs hate single requests. They love matrices. Serving 10 requests of 100 tokens separately takes 10x time. Serving a batch of 10 requests takes ~1.2x time.

In LangChain, use .batch(). Behind the scenes, it optimizes the thread pool for maximum throughput.

5. Summary

Throughput (Requests Per Second) is different from Latency (Seconds Per Request). To maximize Throughput, you must saturate the IO with Async patterns.

Key Intuition: "Don't line up single file. Walk through the door together."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonHuman-in-the-LoopNext lesson →Latency & TTFT Optimization