LangSmith, Reliability, Testing and Migration
Production LangChain work is about seeing, measuring and containing what the model does: LangSmith traces and evaluations, caching and rate limiting, retry and fallback strategy, token accounting, offline tests with fake models, and moving 0.x code onto the slimmer 1.x packages. Interviewers probe judgement here: which lever fits which failure, and what each tool does not do.
Key points
- 1
Tracing turns on with
LANGSMITH_TRACING=trueandLANGSMITH_API_KEY;LANGSMITH_PROJECTpicks the project (a default project is created otherwise), and non-US accounts setLANGSMITH_ENDPOINT.ls.tracing_context(enabled=..., project_name=...)scopes tracing in code. - 2
@traceable(fromlangsmith) traces any function, LangChain or not, as a run nested under the active parent;run_typedefaults tochainand can betool,retriever,llmand others. With tracing off, the function simply runs. - 3
Tags and metadata from
RunnableConfig(or@traceable(tags=..., metadata=...),langsmith_extra,tracing_context(metadata=...)) are recorded on runs and inherited by children, so traces can be filtered by customer, version or environment; athread_idmetadata key groups traces into threads. - 4
LANGSMITH_TRACING_SAMPLING_RATE(0 to 1, default all) samples whole traces;Client(tracing_sampling_rate=...)withtracing_context(client=...)sets per-path rates. In serverless, setLANGCHAIN_CALLBACKS_BACKGROUND=falseor callwait_for_all_tracers()so traces flush before the function freezes. - 5
Offline evaluation: a dataset of examples (inputs, optional reference outputs, metadata), a target that receives only
inputs, evaluators that receiveinputs,outputsandreference_outputsby name, andevaluate(target, data=..., evaluators=[...]), which records an experiment to compare against a baseline. - 6
Evaluator types: code (deterministic rules), LLM-as-judge (reference-free or reference-based), human annotation, and pairwise comparison. Online evaluators run on sampled, filtered production runs, have no reference outputs, and carry spend limits; user feedback is logged with
client.create_feedback(...). - 7
Agents need more than final-answer checks:
agentevalstrajectory match modes arestrict(same calls, same order),unordered,subset(no extra tools) andsuperset(extras allowed), plus an LLM-as-judge trajectory evaluator. - 8
set_llm_cache(...)(fromlangchain_core.globals) installs a global cache;Nonedisables it.InMemoryCacheis per process and lost on restart,SQLiteCache(langchain_community.cache) persists to a file, and semantic caches such asRedisSemanticCachematch by embedding similarity with ascore_threshold. A model'scachefield overrides:Falseskips caching, an instance uses its own cache. - 9
The exact-match cache key is the serialized prompt plus a string of the model's parameters and call options, so a different prompt,
stopor bound kwargs all miss. The cache is checked before the rate limiter, so hits cost no limiter token. - 10
InMemoryRateLimiter(requests_per_second, check_every_n_seconds, max_bucket_size)is a thread-safe token bucket that counts requests, not LLM tokens, starts empty, and is not shared across processes or machines. - 11
Chat models retry network errors, 429 and 5xx with exponential backoff (
max_retriesdefault 6) and do not retry 401 or 404. At system level, keep retries at one layer, make side-effecting tools idempotent before retrying them, set timeouts, and treat a provider fallback as a separate path that needs its own monitoring and evals. - 12
Token tracking:
get_usage_metadata_callback()(orUsageMetadataCallbackHandler) sumsusage_metadataper model name across calls in scope. Testing:GenericFakeChatModel(scriptedAIMessages; subclass to addbind_tools),FakeListChatModel(cycling strings),DeterministicFakeEmbedding,InMemorySaverfor multi-turn tests; integration tests behind a pytest marker with cassettes;langchain-testsstandard unit and integration suites for integrations. 1.x moved legacy chains, retrievers, indexing, hub and community re-exports tolangchain-classicand needs Python 3.10+.
Common traps
from langchain.globals import set_llm_cachefails on 1.x; import it fromlangchain_core.globals.A cache hit replays the stored reply's
usage_metadata, so usage callbacks count tokens that no API call used; bill from real calls or provider reports.Eight worker processes each with
InMemoryRateLimiter(requests_per_second=5)allow about 40 requests per second in total, not 5.create_agentwith tools fails on a plainGenericFakeChatModelbecause it raisesNotImplementedErrorfrombind_tools.An evaluator with exactly two unrecognised parameter names (for example
(prediction, answer)) is treated as the legacy(run, example)signature and silently compares the wrong objects.A loose semantic-cache threshold returns the answer to a similar but different question (open versus close an account).
LLM judges show position, verbosity and self-enhancement biases; calibrate them against human labels before gating releases on them.
Online evaluators have no reference outputs, so a gap between offline and online scores usually means the dataset no longer matches real traffic.
Read the source
Further reading
Suggest a resourceNo extra reading for LangSmith, Reliability, Testing and Migration yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on LangSmith, Reliability, Testing and Migration
Ten questions, with the answer and explanation after each one.