Index of every page on this site: llms.txt
All skills
jeremylongshore avatar

/langchain-performance-tuning

@0d57af8
Run it yourself

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

Use this Skill: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/langchain-performance-tuning

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencespersistent-history.md

≈1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Persistent Chat History

The P22 Failure

InMemoryChatMessageHistory lives in the process heap. Every deploy, OOM kill, or autoscale event wipes every user's conversation. On multi-worker deploys it is worse: each worker has its own copy, so a second request from the same user may land on a worker that never saw the first turn.

RunnableWithMessageHistory accepts any BaseChatMessageHistory — wire a persistent backend.

Redis-Backed History (Default Choice)

from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_community.chat_message_histories import RedisChatMessageHistory

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

chain_with_history = RunnableWithMessageHistory(
    chain,
    history_factory,
    input_messages_key="input",
    history_messages_key="history",
)

Operational notes:

  • ttl is mandatory. Without it, abandoned sessions pile up forever.
  • Pin the Redis namespace per tenant (session_id prefix) to avoid collision and to support per-tenant purge.
  • Size the Redis instance for worst-case turn count. 10k active users 20 turns 1KB ≈ 200MB — small, but monitor it.
  • RedisChatMessageHistory uses rpush + lrange. Read/write is O(1) / O(n) respectively; truncate long histories before persisting (see trim policy below).

Postgres-Backed History

Use when compliance requires relational storage, or when you already pay for managed Postgres and want one fewer dependency.

from langchain_postgres import PostgresChatMessageHistory
import psycopg

conn = psycopg.connect(os.environ["POSTGRES_URL"])
PostgresChatMessageHistory.create_tables(conn, "chat_history")

def history_factory(session_id: str) -> PostgresChatMessageHistory:
    return PostgresChatMessageHistory(
        "chat_history",
        session_id,
        sync_connection=conn,
    )

Trade-off: Postgres is durable and queryable, but each append is a synchronous write with higher latency than Redis. Cache recent turns in memory if per-request latency matters.

LangGraph Checkpointer as History

LangGraph 1.0 offers first-class thread state via AsyncPostgresSaver / AsyncSqliteSaver. For graph-style agents this is strictly better than RunnableWithMessageHistory — the checkpoint captures the full state machine, not just messages.

from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
from langgraph.graph import StateGraph

async with AsyncPostgresSaver.from_conn_string(os.environ["POSTGRES_URL"]) as saver:
    await saver.setup()
    graph = builder.compile(checkpointer=saver)
    result = await graph.ainvoke(
        {"messages": [("user", "...")]},
        config={"configurable": {"thread_id": session_id}},
    )

Migrating Off InMemoryChatMessageHistory

  1. Replace the factory function — nothing else changes if you use RunnableWithMessageHistory.
  2. Backfill is usually not needed; prior sessions were going to be lost on restart regardless.
  3. Add an integration test that restarts the worker mid-conversation and asserts the next turn still sees the earlier messages.
  4. Add a dashboard for history size per session. Pages > 100 turns are bugs or abuse.

Trim Policy

Long histories cost tokens linearly. Apply one of:

  • Last-N window: keep the latest 20-40 turns. Simple, bounded cost.
  • Summarization compaction: summarize older turns into a system-message summary. Use LangGraph's SummarizationNode or a periodic cron that rewrites old sessions.
  • Token budget trim: keep messages until a token budget is hit, oldest first. Most accurate for cost control.

Checklist

  • No production path uses InMemoryChatMessageHistory.
  • ttl set on every RedisChatMessageHistory.
  • Tenant isolation in session keys.
  • Restart test exists and is wired into CI.
  • Trim policy is chosen and implemented.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides comprehensive guidance on performance tuning for LangChain 1.0 and LangGraph applications. It includes best practices for asynchronous programming, batching, caching, and persistent chat history. No security risks, obfuscation, or malicious patterns were identified in the instructions or reference materials.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at 0d57af8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 2 months ago
What it can do
Reads files Edits files Runs commands
version
2.7.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
compatibility
Designed for Claude Code
All 5 allowed tools
ReadWriteEditBash(python:*)Bash(redis-cli:*)
Other metadata
tags
[
  "saas",
  "langchain",
  "langgraph",
  "python",
  "langchain-1.0",
  "performance",
  "caching",
  "async"
]

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/langchain-performance-tuning