Оптимизация цепочек и агентов LangChain 1.0 / LangGraph 1.0 на Python.

Звёзды2 926
Авторjeremylongshore
Установок22

Установка

SKILL.md

LangChain Performance Tuning

Overview

An engineer calls chain.batch(inputs_1000) expecting 1000 parallel LLM calls. Actual behavior: Runnable.batch and Runnable.abatch in LangChain 1.0 default to max_concurrency=1, so the 1000 inputs run sequentially with bookkeeping overhead — sometimes slower than a plain for loop. This is pain-catalog entry P08. The fix is one line:

# Before: serial, ~1000 * per_call_latency
await chain.abatch(inputs)

# After: 10x throughput at 10 providers' worth of concurrency
await chain.abatch(inputs, config={"max_concurrency": 10})

Other silent regressions in the same pain catalog: P48 (invoke inside async def blocks the FastAPI event loop), P22 (InMemoryChatMessageHistory loses every user's chat on restart), P62 (RedisSemanticCache at the default score_threshold=0.95 returns under 5% hit rate), P59 (async retrievers leak connections on cancellation), P60 (BackgroundTasks fires after the response — wrong for per-token SSE), P01 (streaming token counts are only reliable on the on_chat_model_end event).

This skill wires a production performance baseline: explicit batch concurrency, async-only code paths, Redis-backed caches tuned on a golden set, persistent chat history with TTL, and TTFT instrumentation from astream_events(version="v2").

Prerequisites

  • Python 3.11+ with langchain>=1.0,<2, langgraph>=1.0,<2, langchain-openai or langchain-anthropic, langchain-community, langchain-redis or redis>=5.
  • A working LangChain 1.0 chain or LangGraph 1.0 graph that already passes functional tests.
  • Redis 7+ reachable from the app for cache and history (local Docker is fine for dev).
  • A FastAPI / Starlette async endpoint, or an equivalent async entrypoint.
  • Observability: a place to emit metrics (Prometheus, OpenTelemetry, or LangSmith) — needed to measure TTFT, p95, and cache hit rate.

Instructions

  1. Establish a latency budget and baseline. Pick explicit targets before changing code: TTFT under 1s, p95 total under 5s, throughput over 20 req/s per worker, cost under $X per 1k interactions. Run a 5-minute load test with locust or wrk against the current chain and record p50 / p95 / p99 / TTFT / total cost. Without these numbers every downstream change is theater.

  2. Convert every hot path to async (P48). Inside async def handlers, replace invoke, stream, batch, get_relevant_documents, and tool.run with ainvoke, astream / astream_events(version="v2"), abatch, aget_relevant_documents, and tool.arun. See references/async-safety-checklist.md for a grep pattern and a CI linter. Target: zero sync LangChain calls inside any async function.

  3. Fix .abatch() concurrency (P08). Every .abatch / .batch call must pass config={"max_concurrency": N} where N is chosen from the provider table in references/batch-concurrency-per-provider.md (Anthropic 10-20, OpenAI 20-50, local vLLM 100+). For multi-worker deploys, cap account-wide calls with a LiteLLM / Portkey proxy or a Redis semaphore — max_concurrency only governs one process.

  4. Instrument TTFT with astream_events(version="v2") (P01). Measure time to first token separately from total latency — user-perceived performance hinges on TTFT. Read usage metadata only on the on_chat_model_end event; per-chunk usage fields lag and are not reliable mid-stream.

    from time import perf_counter
    async def run(chain, query: str):
        t0 = perf_counter(); ttft = None; tokens = 0
        async for ev in chain.astream_events({"input": query}, version="v2"):
            if ev["event"] == "on_chat_model_stream" and ttft is None:
                ttft = perf_counter() - t0
            if ev["event"] == "on_chat_model_end":
                tokens = ev["data"]["output"].usage_metadata["total_tokens"]
        return {"ttft_s": ttft, "total_s": perf_counter() - t0, "tokens": tokens}
    
  5. Enable an exact LLM cache. For deterministic (temperature=0) prompts, set RedisCache or SQLiteCache globally. LangChain 1.0 keys include the bound tools signature (P61 fix), which prevents cache poisoning when an agent's tool list changes. Always set an explicit TTL on Redis keys — default Redis keys are immortal.

    from langchain_core.globals import set_llm_cache
    from langchain_community.cache import RedisCache
    import redis
    set_llm_cache(RedisCache(redis.Redis.from_url("redis://cache:6379/0")))
    
  6. Add a semantic cache with a tuned threshold (P62). The RedisSemanticCache default score_threshold=0.95 produces < 5% hit rate on real traffic. Collect a 200-500 prompt golden set with labeled near-duplicates, measure cosine similarity with your embedding model, and pick the F1-maximizing threshold — typically 0.85-0.90 for text-embedding-3-small. Full procedure in references/cache-tuning.md. Do not run semantic cache behind temperature > 0; users will see prior random draws.

  7. Replace InMemoryChatMessageHistory (P22). Every production chat path must use RedisChatMessageHistory (with ttl) or a LangGraph checkpointer (AsyncPostgresSaver / AsyncSqliteSaver). Add a restart test: mid-conversation, kill and restart the worker, assert the next user turn still sees prior messages. See references/persistent-history.md for migration steps and trim policies.

  8. Close retriever connection pools in FastAPI lifespan (P59). Build the vector store once at startup, expose it via app.state, close it in the finally block. Never construct a retriever per request — cancellations leak pg connections.

  9. Stream tokens with SSE, not BackgroundTasks (P60). BackgroundTasks runs after the response body is flushed; per-token dispatch via it delivers tokens the client will never read. Use EventSourceResponse (sse-starlette) or a WebSocket and pipe events from astream_events.

  10. Re-run the load test and diff the four metrics. TTFT, p95, throughput, cost per 1k. If any regressed, revert that step and investigate — do not stack changes without verification. Execute in this order to isolate effects:

    1. Run the baseline load test and save results.
    2. Set max_concurrency on every .abatch call and re-run.
    3. Add exact cache, re-run, check cache hit rate.
    4. Configure semantic cache with tuned threshold, re-run, check hit rate again.
    5. Verify persistent history survives a worker restart.

Throughput Tuning Table (starting values)

ProviderSafe max_concurrencyCeiling signal
Anthropic (sonnet-4.5/4.6)10-20429 rate_limit_error
OpenAI (gpt-4o / 4o-mini)20-50429 + TPM exhaustion header
OpenAI o1 / reasoning2-5Cost + latency, not rate
Google Gemini 1.5/2.510-30429
Cohere20-40429
Local vLLM / TGI100-500 (batch N≈32-64)GPU KV-cache OOM
Ollama on consumer GPU1-4Process queue backpressure

Latency Breakdown Template

Record these for every change, not just total:

MetricTargetSource
TTFT p50 / p95500ms / 1sfirst on_chat_model_stream event
Total p50 / p952s / 5send-to-end handler
Tool-call p95< 1s per toolon_tool_end - on_tool_start
Retriever p95< 300mson_retriever_end - on_retriever_start
Provider p95measure per modelsplit by LLM node

Batch Sweet-Spot Numbers

  • Anthropic tier 2 chat: max_concurrency=10 saturates at roughly 8 req/s, p95 doubles past 20.
  • OpenAI gpt-4o-mini tier 3: knee of the curve around max_concurrency=30-40; ~40 req/s throughput.
  • Local vLLM A100: server-side batch sweet spot N=32-64, client max_concurrency=100+.

Verify on your own account — these are starting points, not promises.

Output

Deliverables from running this skill end-to-end:

  • A perf/ directory with baseline.json and tuned.json load-test results.
  • All async handlers use ainvoke / astream_events / abatch with explicit max_concurrency.
  • set_llm_cache wired to RedisCache (exact) and optionally RedisSemanticCache (tuned threshold).
  • RunnableWithMessageHistory or LangGraph checkpointer backed by Redis or Postgres, with TTL.
  • FastAPI lifespan closing vector store pools on shutdown.
  • SSE endpoint streaming from astream_events(version="v2").
  • A tests/test_no_sync_in_async.py CI guard (see async-safety reference).
  • Metrics exported: ttft_seconds, total_latency_seconds, cache_hit_total, cache_miss_total, batch_concurrency_current.
  • Runbook entry with the tuned max_concurrency per provider and the semantic-cache threshold, versioned in git.

Error Handling

SymptomRoot causeFix
.abatch(inputs) no faster than a for loopmax_concurrency=1 default (P08)Pass config={"max_concurrency": N}
FastAPI TTFT collapses under loadSync invoke inside async def (P48)Switch to ainvoke / astream_events
Chat forgets prior turns after deployInMemoryChatMessageHistory (P22)Move to RedisChatMessageHistory with TTL
Semantic cache hit rate < 5%score_threshold=0.95 default (P62)Tune on golden set to 0.85-0.90
pg pool exhausted hours into load testRetriever not closed on cancel (P59)Close vector store in FastAPI lifespan
SSE client sees zero tokensDispatching via BackgroundTasks (P60)Use EventSourceResponse and astream_events
Per-chunk token counts fluctuateUsage metadata lags during stream (P01)Read only on on_chat_model_end
429 storm after tuning concurrencyPer-worker limit * N workers > account RPMAdd LiteLLM/Portkey proxy or Redis semaphore
Semantic cache returns off-brand outputCache hit on temperature > 0 routeDisable semantic cache or force temperature=0
Cache poisoning after tool changeMissing tools in cache keyUpgrade LangChain to 1.0.x post-P61 fix

Examples

Example 1 — Fix a sequential batch job.

# Before — 1000 items, 18 minutes end-to-end
results = await chain.abatch(inputs)

# After — 1000 items, ~2 minutes; Anthropic tier-2 account, N=10
results = await chain.abatch(inputs, config={"max_concurrency": 10})

Example 2 — Wire persistent history and an exact cache on a FastAPI app.

from contextlib import asynccontextmanager
from fastapi import FastAPI
from langchain_core.globals import set_llm_cache
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_community.cache import RedisCache
from langchain_community.chat_message_histories import RedisChatMessageHistory
import redis

@asynccontextmanager
async def lifespan(app: FastAPI):
    r = redis.Redis.from_url("redis://cache:6379/0")
    set_llm_cache(RedisCache(r))
    app.state.r = r
    yield
    r.close()

app = FastAPI(lifespan=lifespan)

def history_for(session_id: str) -> RedisChatMessageHistory:
    return RedisChatMessageHistory(
        session_id=session_id,
        url="redis://history:6379/2",
        ttl=60 * 60 * 24 * 14,
    )

chain_with_history = RunnableWithMessageHistory(
    base_chain, history_for,
    input_messages_key="input",
    history_messages_key="history",
)

Example 3 — Stream tokens with measured TTFT.

from sse_starlette.sse import EventSourceResponse
from time import perf_counter

@app.post("/chat")
async def chat(req: ChatReq):
    async def gen():
        t0 = perf_counter()
        async for ev in chain_with_history.astream_events(
            {"input": req.text},
            config={"configurable": {"session_id": req.session_id}},
            version="v2",
        ):
            if ev["event"] == "on_chat_model_stream":
                yield {"data": ev["data"]["chunk"].content}
        app.state.r.incrbyfloat("ttft_sum_s", perf_counter() - t0)
    return EventSourceResponse(gen())

Resources

  • One-pager — problem / solution / key features snapshot.
  • batch-concurrency-per-provider — per-provider max_concurrency table, sweep procedure, semaphore patterns.
  • cache-tuning — exact vs semantic, Redis key design, golden-set threshold procedure, TTL strategy.
  • persistent-history — Redis / Postgres / LangGraph checkpointer migration off InMemoryChatMessageHistory.
  • async-safety-checklist — sync-in-async grep + linter, lifespan pool cleanup, SSE vs BackgroundTasks.
  • LangChain streaming / batching — official docs for Runnable.batch and streaming modes.
  • LangChain caching — set_llm_cache, Redis and SQLite backends.
  • LangGraph checkpointers — persistence for graph state.
  • Companion skills in langchain-py-pack: langchain-model-inference (token accounting), langchain-embeddings-search (retrieval tuning), langchain-middleware-patterns (tool-signature cache keying, P61).

Вставить в README

Ссылка на карточку для вашего README — без картинок и счётчиков.

[DarkPlay: langchain-performance-tuning](https://darkplay.ru/ai/skills/jeremylongshore-claude-code-plugins-plus-skills-langchain-performance-tuning)

Похожие скиллы и другие разделы каталога

Популярное в разделе «Скиллы»

Самые востребованные скиллов раздела по установкам и звёздам.

find-skillsvercel-labs · Помогает находить и устанавливать навыки агентов по запросам вроде «как сделать X»…
4 400 000 устан.
grill-memattpocock · Настойчивое интервью для доработки плана или дизайна.
1 600 000 устан.
hyperframesheygen-com · Обязательная точка входа для любых задач по видео, анимации и motion-графике в…
1 600 000 устан.
hyperframes-cliheygen-com · Цикл разработки HyperFrames CLI: init, add, render, preview, publish и другие команды.
1 600 000 устан.
agent-browservercel-labs · CLI для автоматизации браузера для ИИ-агентов: навигация по страницам, заполнение форм…
1 600 000 устан.
media-useheygen-com · Agent Media OS: единый навык для медиа в HyperFrames — BGM, SFX, изображения, голос, LUT.
1 500 000 устан.
hyperframes-registryheygen-com · Установка и подключение блоков реестра в композиции HyperFrames через add и catalog.
1 400 000 устан.
grill-with-docsmattpocock · Настойчивое интервью для доработки плана или дизайна, попутно создающее документацию (ADR…
1 300 000 устан.
improve-codebase-architecturemattpocock · Поиск в кодовой базе возможностей для углубления, визуальный HTML-отчёт о них и детальный…
1 300 000 устан.
tddmattpocock · Разработка через тестирование (TDD): red-green-refactor и интеграционные тесты.
1 300 000 устан.
Похожее в других разделах каталога

Соседние разделы каталога: возможно, нужный инструмент живёт там.

Cpp Revieweraffaan-m · Ревьюер C++: безопасность памяти, современные идиомы, конкурентность, производительность.
225 587 ★
Csharp Revieweraffaan-m · Ревьюер C#: конвенции .NET, async, безопасность, nullable-типы, производительность.
225 587 ★
Database Revieweraffaan-m · Специалист PostgreSQL: оптимизация запросов, схемы, безопасность, Supabase.
225 587 ★
Flutter Revieweraffaan-m · Ревьюер кода Flutter и Dart: виджеты, управление состоянием, идиомы и архитектура.
225 587 ★
Fsharp Revieweraffaan-m · Ревьюер кода F#: функциональные идиомы, типы, сопоставление с образцом и…
225 587 ★
Go Revieweraffaan-m · Ревьюер кода Go: идиоматичность, конкурентность, обработка ошибок и производительность.
225 587 ★
Harmonyos App Resolveraffaan-m · Эксперт по HarmonyOS на ArkTS и ArkUI: состояние V2, навигация, API и производительность.
225 587 ★
Performance Optimizeraffaan-m · Специалист по производительности: узкие места, профилирование, память и бандлы.
225 587 ★
Php Revieweraffaan-m · Ревьюер PHP: PSR-12, типы, Eloquent, безопасность и производительность.
225 587 ★
Python Revieweraffaan-m · Ревьюер Python: PEP 8, идиомы, аннотации типов, безопасность и производительность.
225 587 ★

Лучшие ИИ-инструменты для автоматизации и ассистентов

Ассистенты и автоматизация

Подборка ИИ-ассистентов для автоматизации рутины и ускорения рабочих процессов. Сервисы, которые берут на себя переписку, анализ данных, ведение базы знаний и подготовку продаж, эк

Give Me Public
Есть бесплатный тариф
5.0
AITunnelAitunnel
По запросу
5.0
STIVAStiva
По запросу
5.0
Wikibot (Викибот)Wikibot
По запросу
4.9
mymeet.aiMymeet
По запросу
4.9
InsighterInsighter
По запросу
4.9
TGHuntTghunt
Есть бесплатный тариф
4.9
LegiscanLegiscan
По запросу
4.9
RouterAIRouterai
По запросу
4.9
Wikibot 1.0 (Викибот)Wikibot
По запросу
4.9

Лучшие ИИ-инструменты для генерации видео

Генерация видео

Подборка нейросетей для генерации и монтажа видео. Инструменты, которые превращают текст в готовые ролики, экономят часы на съемках и постпродакшне. Подходит для SMM-специалистов,

VEED.IOVeed
По запросу
5.0
MootionMootion
По запросу
5.0
FlyviFlyvi
Есть бесплатный тариф
4.9
Google Veo 3.1Google
Платно
4.9
Sora (OpenAI)OpenAI
Платно
4.9
PikaPika
Платно
4.9
captions space
Есть бесплатный тариф
4.8
Клайп
Есть бесплатный тариф
4.8
viralpulse.appViralpulse
По запросу
4.8
Viral Maker AIViral-maker
Есть бесплатный тариф
4.8

Лучшие ИИ-инструменты для генерации текста и копирайтинга

Генерация текста и копирайтинг

Подборка нейросетей для генерации текстов и копирайтинга. Сервисы, которые помогают создавать статьи, посты и рекламные объявления с нуля или по черновику, экономя часы рутинной ра

NeuroscribeNeuroscribe
Есть бесплатный тариф
5.0
GoGPTGogpt
По запросу
5.0
ChatsonicChatsonic
По запросу
4.9
НейроТекстерNeuro-texter
По запросу
4.9
ChatGPTOpenAI
По подписке
4.9
Редактум
Есть бесплатный тариф
4.8
ChadGPTChadgpt
По запросу
4.8
Cosmo ScribeCosmoscribe
Есть бесплатный тариф
4.8
Memo HelperCopilot33
По запросу
4.8
LibraChatLibrachat
Есть бесплатный тариф
4.8