ModelRefs / LangChain Framework — Tutorial

LangChain Framework — Tutorial

Chains, prompts, memory, and tool integrations — LangChain's building blocks for LLM applications

Overview

Level: Advanced. Estimated reading time: 35 minutes.

What LangChain provides

LangChain is an open-source framework (Python + TypeScript) for composing LLM applications. Its core value: standardised interfaces that let you swap LLMs, vector stores, and tools without rewriting application logic.

Core abstractions: LLMs/ChatModels (unified interface for OpenAI, Anthropic, Ollama etc.), Prompts (PromptTemplate with variable interpolation), Output parsers (structured extraction), Chains (multi-step pipelines), Memory (conversation history), Tools and Agents, Retrievers (vector store interface).

The main criticism of LangChain is over-abstraction. For production systems, many teams use LangChain for rapid prototyping then build custom pipelines. LangGraph (LangChain's graph-based agent framework) is the current focus for agentic applications.

Chains and the LCEL interface

LCEL (LangChain Expression Language) uses the pipe operator | to compose components:

chain = prompt | llm | output_parser

Each component is a Runnable with .invoke(), .stream(), and .batch() methods. The output of the left component is passed as input to the right.

Common patterns: RAG chain: retriever | format_docs | prompt | llm | StrOutputParser() Extraction chain: prompt | llm.with_structured_output(MySchema) Router chain: classify input → route to one of N sub-chains

Streaming: chain.stream(input) yields tokens as they arrive. chain.batch([input1, input2]) processes multiple inputs in parallel.

Memory and conversation history

LangChain provides several memory classes:

ConversationBufferMemory: keeps the full conversation history. Simple but expensive for long conversations.

ConversationBufferWindowMemory: keeps the last k turns only.

ConversationSummaryMemory: periodically summarises older turns with an LLM call.

ConversationSummaryBufferMemory: keeps recent turns verbatim, summarises older turns. Most practical for production.

With LCEL, memory is managed explicitly: retrieve history, add to prompt, save new turn after response. MessagesPlaceholder in ChatPromptTemplate injects the history list.

Continue your research

Use these connected ModelRefs sections to compare alternatives, inspect implementation paths, and review the evidence and governance boundaries relevant to LangChain Framework — Tutorial.