Observability, Tracing & Production Patterns
From the course LangChain & AI Orchestration Frameworks
AI applications are notoriously hard to debug. A chain of LLM calls, tool invocations, and conditional logic creates complex execution paths that need visibility.
LangSmith for Tracing
LangSmith captures every step of your chain execution:
- Traces: Full execution tree showing each node, its inputs, outputs, and timing
- Token usage: Per-step and total token counts with cost estimates
- Latency breakdown: Where time is being spent (model inference vs. tool calls)
- Error tracking: Which step failed and why, with full stack traces
Setting Up Tracing
import os
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = "your-langsmith-key"
os.environ["LANGCHAIN_PROJECT"] = "my-production-app"
Once set, all LangChain operations are automatically traced. No code changes needed.
Production Patterns
Fallback chains: If the primary model fails, fall back to an alternative:
primary = ChatOpenAI(model="gpt-4o")
fallback = ChatAnthropic(model="claude-sonnet-4-20250514")
robust_model = primary.with_fallbacks([fallback])
Retry logic: Automatically retry on transient failures:
from langchain_core.runnables import RunnableConfig
chain.invoke(input, config=RunnableConfig(max_retries=3))
Streaming: For user-facing applications, stream tokens as they're generated:
for chunk in chain.stream({"question": "Explain quantum computing"}):
print(chunk, end="", flush=True)
Caching: Avoid redundant LLM calls for identical inputs:
from langchain_core.globals import set_llm_cache
from langchain_community.cache import SQLiteCache
set_llm_cache(SQLiteCache(database_path="cache.db"))
Evaluation with LangSmith
Create evaluation datasets and run your chain against them:
- Collect representative input-output pairs
- Define evaluation criteria (correctness, relevance, safety)
- Run automated evals on every code change
- Compare performance across chain versions
This creates a CI/CD pipeline for AI quality, catching regressions before they reach production.
Key Takeaways
- •LangSmith provides automatic tracing of every step in your chain execution
- •Use fallback chains and retry logic for production reliability
- •Stream tokens for responsive user-facing applications
- •Automated evaluation datasets catch quality regressions before deployment
Progress is kept in this browser only.