AI Engineer Roadmap All lessons Interactive Quiz GitHub

LangChain

Beginner · 11 min read · Updated · By Saurabh Pathak

In short: LangChain is a Python library of building blocks (prompts, models, parsers, retrievers, tools) that you join together with a pipe operator.

Play the interactive lesson: LangChain: a chain of building blocksRead on GitHub
On this page
  1. Explain it like I'm new
  2. Watch it
  3. How it works
  4. Code example
  5. When to use it / when not to
  6. Common mistakes
  7. Recap
  8. Interview questions
  9. Go deeper

Level: Beginner · Reading time: 11 min · Phase: 5 — Frameworks

Before you start: You should know what an LLM API call is (Calling an LLM API) and the basic RAG idea (What is RAG).

Explain it like I'm new

Think of a kitchen assembly line. One person chops, one cooks, one plates. Each does one job and hands the result on. LangChain gives you the stations. You decide the order.

Without a framework, you write the glue yourself: build a prompt string, call the model's API, parse the reply. LangChain wraps each of those steps in a common interface so they snap together and so you can swap one part (say, the model provider) without rewriting the rest.

Words to know:

Watch it

LangChain: a chain of building blocks: Pieces joined with the pipe operator.
LangChain: a chain of building blocks: Pieces joined with the pipe operator.

It plays on a loop. Read the caption in the box, one step at a time. Want to go at your own pace? Step through it with Previous and Next.

How it works

  1. Input dict goes into the prompt template, which produces messages.
  2. The chat model turns messages into a reply.
  3. The parser converts the reply into the final value.
flowchart LR
    I[input dict] --> P[prompt] --> M[chat model] --> O[output parser] --> R[result]

Mapping to earlier phases: a RAG chain is retriever | format docs | prompt | model | parser — exactly the pipeline from Phase 1. An agent is a loop around a model with tools — Phase 4. LangChain's own agent loop is built on LangGraph.

Worked example

Here is one input travelling through the three-step chain from the repo's offline demo (fw_mini_chain.py). The "model" is a fake that echoes the last word in capitals, so we need no network or key. I ran this and pasted the real values.

StepWhat goes inWhat comes out
Start{'letter': 'a'}(the input dict)
Prompt template{'letter': 'a'}'Name a fruit starting with a please apple'
Fake modelthat prompt string'ANSWER: APPLE'
Output parser'ANSWER: APPLE''APPLE'

The final result of chain.invoke(...) is APPLE. Each step only knew about its own input and output. The | just passed each result to the next step.

With a real model, only the middle row changes: a provider's reply replaces the fake one. Check the current docs for exact imports and package names.

Code example

From scratch, stdlib only. This is the core idea in about 15 lines (full version: fw_mini_chain.py):

class Step:
    def __init__(self, fn):
        self.fn = fn
    def invoke(self, value):
        return self.fn(value)
    def __or__(self, other):
        return Step(lambda v: other.invoke(self.invoke(v)))

prompt = Step(lambda d: f"Translate to French: {d['text']}")
model = Step(lambda p: "Bonjour")          # fake model, no network
parser = Step(lambda s: s.strip().lower())

chain = prompt | model | parser
print(chain.invoke({"text": "Hello"}))     # bonjour

In real projects (needs pip install langchain-openai and an API key; check the docs for current package names):

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

prompt = ChatPromptTemplate.from_template("Translate to French: {text}")
chain = prompt | ChatOpenAI(model="gpt-4o-mini") | StrOutputParser()
print(chain.invoke({"text": "Hello"}))

When to use it / when not to

Common mistakes

Recap

If you remember only three things:

Interview questions

  1. What is LCEL? <details><summary>Answer</summary>A syntax where runnables are joined with |. Each step shares the same interface, so chains support invoke, batch and stream with no extra code.</details>
  2. Chain vs agent? <details><summary>Answer</summary>A chain follows a path you fixed in code. An agent lets the model choose the next step in a loop, usually by calling tools.</details>
  3. Name a downside of using LangChain. <details><summary>Answer</summary>Extra abstraction layers make debugging harder and the API changes often. For simple tasks, a plain SDK call is clearer.</details>

Go deeper

Prev: Phase 5 overview · Next: LangGraph · Back to phase: Phase 5