AI/ML7 min read

LangChain

Compose LLM applications from chains, tools, memory, and retrievers - LangChain provides the building blocks for RAG pipelines, agents, and multi-step AI workflows.

LangChainLLMRAGAgentsPython

skill: LangChain category: AI/ML tags: [LangChain, LLM, RAG, Agents, Python] readingTime: 7

LangChain accelerates building LLM applications with pre-built retrievers, memory stores, and tool integrations. For simple one-shot LLM calls, use the OpenAI or Anthropic SDK directly. For production agents requiring reliability and observability, pair LangChain with LangSmith tracing.

Workflow

  1. Install and set up LangChainLangChain is modular - install only the integrations you need to keep bundle size small.
  2. Build chains with LCELLangChain Expression Language (LCEL) composes prompts, models, and output parsers using a pipe | syntax - streams by default and is easy to reason about.
  3. Build a RAG chain with a retrieverLangChain's retriever interface abstracts over Pinecone, pgvector, Chroma, and more - swap vector stores without changing chain logic.
  4. Add memory for multi-turn conversationsLLMs are stateless - without memory, each call has no context from prior turns. Memory stores and injects conversation history.
  5. Trace and debug with LangSmithLangSmith captures every prompt, response, latency, and token count in a UI - essential for debugging multi-step chains where intermediate outputs are invisible.

Output Format

Composable LangChain application: LCEL chain → retriever → memory → LangSmith tracing.

from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser

chain = prompt | ChatOpenAI(model='gpt-4o') | StrOutputParser()
result = chain.invoke({'key': 'value'})

Examples

Input

Output

Edge Cases & Gotchas

  • Version fragmentation - LangChain frequently breaks APIs between versions (0.1, 0.2, 0.3). Pin exact versions in requirements.txt and read the migration guide before upgrading.
  • Context window overflow - Injecting too many retrieved chunks exceeds the model's context window. Limit with top_k=5, and trim chunks to max_tokens/k characters.
  • Parallel retrieval latency - Sequential retrieval is slow when fetching from multiple sources. Use RunnableParallel to fetch from multiple retrievers simultaneously.
  • Memory grows unbounded - InMemoryChatMessageHistory grows with every message. Trim with a ConversationBufferWindowMemory(k=5) to keep only the last k turns.

When to Break These Rules

  • Use the raw OpenAI/Anthropic SDK for simple one-shot generation - LangChain adds overhead and complexity for single calls.
  • Use LlamaIndex instead of LangChain when the application is purely a knowledge retrieval system - LlamaIndex's data connectors and index types are more mature for that use case.

Reference Files

  • https://python.langchain.com/docs/
  • https://docs.smith.langchain.com/

More skills in this library

Browse all technical skills - Mobile, AI/ML, Web, Backend, Design, and Product.