LangChain Mastery

Course Content

LangChain Mastery

7 sections · 109 lessons

What is LangChain, and how is it used in NLP applications?


What you need to know

A real NLP feature is almost never one model call. A support bot has to turn the question into a prompt, find the right help articles, call the model, and turn the reply into something the app can show. LangChain gives you a standard building block for each of those steps, and a standard way to join them.

Python
from langchain.chat_models import init_chat_modelfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserllm = init_chat_model("openai:gpt-5.4-mini", temperature=0)prompt = ChatPromptTemplate.from_messages([    ("system", "Label the ticket as billing, technical or other. One word only."),    ("human", "{ticket}"),])classify = prompt | llm | StrOutputParser()print(classify.invoke({"ticket": "I was charged twice for March."}))  # billing

The template fills in the ticket, the model replies with an AIMessage, and the parser turns that message into a plain string. Because classify is a Runnable, classify.batch([...]) and classify.stream(...) work with no extra code.

How the project is split (LangChain 1.x)

PackageWhat it holds
langchain-coreThe interfaces: Runnables, messages, prompts, parsers, tools
langchaininit_chat_model, create_agent, middleware
langchain-openai, langchain-anthropic, ...One package per provider
langgraphThe runtime for stateful, looping agents and workflows
langchain-classicLegacy LLMChain, RetrievalQA, old memory classes
langsmithTracing, evaluation and prompt management

Typical NLP uses

  • RAG (retrieval-augmented generation): find relevant passages, then answer from them.
  • Extraction: turn an invoice or email into a validated Pydantic object.
  • Classification and routing: label a ticket and send it to the right flow.
  • Agents: the model picks tools (search, SQL, an API) in a loop.

When not to use it

For one fixed prompt and one provider, the provider's own SDK is simpler and has fewer layers to debug. LangChain earns its place when you need several steps, swappable models, retrieval, tracing, or an agent loop.

A real-life example

A Pune SaaS payroll company has 1,400 help-centre articles and gets about 3,000 support chats a day. Their bot is a small LangChain pipeline: a retriever finds the 4 most relevant articles, a ChatPromptTemplate puts them in the prompt with the question, the model answers, and a parser returns the answer plus the article links. When the team later moved from one model provider to another to save cost, they changed one string in init_chat_model; the prompt, retriever and parser code stayed the same. LangSmith tracing showed each step's latency, which is how they found that retrieval, not the model, took 60% of the time.

Follow-up questions to expect

  • "How is LangChain different from LangGraph?" — LangChain gives the building blocks and a ready-made agent (create_agent); LangGraph is the lower-level runtime for stateful graphs with loops, branches, persistence and human approval steps. create_agent runs on LangGraph.
  • "Is LLMChain still used?" — It is deprecated and moved to langchain-classic. The replacement is prompt | llm | parser.
  • "Why not just call the OpenAI SDK?" — For one call, do that. LangChain pays off when you need to swap providers, compose steps, stream, batch, or trace.