| name | langchain-performance-tuning |
| description | Tune LangChain 1.0 / LangGraph 1.0 Python chains and agents for throughput,
latency, and cost โ streaming modes, explicit batch concurrency, semantic
plus exact caches, persistent message history, and async-safe retriever
patterns. Use when p95 latency exceeds target, batching "does not work",
cost grows linearly with traffic, or a process restart wipes chat history.
Trigger with "langchain performance", "langchain slow batch",
"langchain throughput", "langchain p95 latency", "semantic cache hit rate".
|
| allowed-tools | Read, Write, Edit, Bash(python:*), Bash(redis-cli:*) |
| version | 2.5.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","langchain","langgraph","python","langchain-1.0","performance","caching","async"] |
| compatibility | Designed for Claude Code, also compatible with Codex |
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:
await chain.abatch(inputs)
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
-