07. Frameworks & Libraries
LangChain Fundamentals (Chains, Agents, Tools, Memory)
LangChain is a framework for composing LLM apps from runnable pieces: chains (pipelines), agents (dynamic tool loops), tools (schema-backed actions), and memory (state across turns). Learn the four pillars first — then build apps and graduate to LangGraph for durable graphs.
What you will learn
- Explain chains vs agents in LangChain terms
- Describe how tools are declared and executed
- Pick memory patterns for chat and long-running tasks
- Relate LCEL/Runnables to prompt pipelines
- Answer interview questions with short, solid answers
Definition
LangChain is an open-source framework (Python and JS/TS) for building applications around LLMs by wiring prompts, models, parsers, retrievers, tools, and memory into composable units — historically “chains,” now centered on the Runnable / LCEL abstraction.
For Agentic AI, the important split is: chains encode mostly fixed pipelines; agents choose actions (often tools) at runtime from observations. Tools expose capabilities; memory supplies prior state so the next call is not amnesia.
Introduction
This lesson opens Module 07 (Frameworks & Libraries). Modules 01–06 covered concepts and multi-agent design; now you map those ideas onto concrete libraries.
Next: Building AI Applications with LangChain — end-to-end app patterns. LangGraph (later in this module) is the graph/runtime sibling for durable agent workflows.
Why LangChain matters
- Standardizes prompt → model → parse plumbing
- Makes tool calling and RAG building blocks reusable
- Large ecosystem of integrations (models, stores, APIs)
- Shared vocabulary for interviews and teams
Chains (and LCEL)
Think “pipeline,” not a single class name. Modern LangChain expresses chains as Runnable sequences (LCEL): prompt | model | parser, with branches and fallbacks as needed.
- Prompt templates - inject variables safely
- Chat/LLM models - provider wrappers
- Output parsers / structured output - schemas
- Retrievers - optional RAG hops in the pipe
- Use when steps are stable and evaluable
Agents
An agent binds a model to tools and a stop policy: reason, optionally call a tool, observe, repeat until a final answer or budget hit. Same Tool-Use / ReAct ideas from earlier modules.
- Tool-calling agents - model emits structured tool calls
- ReAct-style - interleaved thought and action (classic pattern)
- Always set - max iterations / timeouts
- Prefer a chain - if you already know the tool sequence
Tools
A tool is a named capability with a description, argument schema, and handler. The model selects; your runtime validates and executes — never trust raw args.
- Declare - name, description, args schema
- Implement - side effects in code with authz/timeouts
- Return - concise observations the model can use
- Catalog size - keep allowlists small per agent
Memory
- Chat history - buffer of recent messages
- Window / token limit - drop oldest turns
- Summary memory - compress old turns into a synopsis
- Entity / vector memory - long-term facts via stores
- Session key - isolate users/threads; never mix tenants
How the four fit together
- Simple Q&A - prompt chain + optional short memory
- RAG app - retrieve | prompt | LLM chain
- Tool assistant - agent + tools + chat memory
- Production path - often migrate complex agents to LangGraph for state/checkpoints
Failure modes
- Agent for everything - higher cost when a chain would do
- Huge tool catalogs - wrong-tool selection
- Unbounded memory - context overflow and leakage
- Skipping parsers - brittle string scraping
- Ignoring stop budgets - tool loops that never end
Program / implementation notes
Learn the Runnable interface: invoke, batch, stream. Keep secrets in env/config, not prompts. Pin package versions — LangChain APIs evolve.
- Python: langchain / langchain-core + provider packages
- JS/TS: @langchain/core and ecosystem packages
- Next: Building AI Applications with LangChain
Advantages and limitations
- Advantage: fast composition of common LLM patterns
- Advantage: broad integrations and community examples
- Advantage: clear teaching path from chain → agent → graph
- Limitation: abstraction churn across major versions
- Limitation: easy to overbuild agents for simple jobs
- Limitation: durable multi-actor workflows often need LangGraph
Key points
- LangChain composes LLM apps from chains/agents, tools, and memory.
- Chains/LCEL = pipelines; agents = dynamic tool loops.
- Tools need schemas, validation, and safe handlers.
- Memory types: buffer, window, summary, vector/entity.
- Prefer chains when the path is known; agents when it must adapt.
- Complex durable graphs → LangGraph (later lesson).
FAQs
Is LCEL replacing chains?
LCEL/Runnables are the modern way to express what people called chains — composable pipelines with invoke/stream/batch.
Do I need LangChain to build agents?
No. You can call provider APIs directly. LangChain speeds wiring, tools, and integrations — at the cost of learning its abstractions.
LangChain vs LangGraph?
LangChain covers building blocks and simple agents. LangGraph focuses on stateful graphs, cycles, checkpoints, and human-in-the-loop control.
Where should memory live?
Behind a session store keyed by user/thread. Inject only what the next turn needs; summarize or retrieve the rest.
Interview questions
- What is LangChain?
Answer A framework for composing LLM applications from prompts, models, parsers, tools, memory, and related integrations — often via Runnable/LCEL pipelines and agents.
- Chain vs agent in LangChain?
Answer A chain is a largely fixed pipeline of steps. An agent dynamically chooses tools/actions from observations until it finishes or hits a budget.
- What makes a LangChain tool?
Answer A name, description, argument schema, and handler that returns an observation after validated execution.
- Name LangChain memory patterns.
Answer Chat buffer, windowed history, summary memory, and longer-term entity/vector stores keyed by session.
- When should you avoid an agent?
Answer When the tool/call sequence is already known — a chain is cheaper, easier to eval, and more predictable.
- How does this map to Agentic AI theory?
Answer Tools implement the Tool-Use pattern; agents implement ReAct-style loops; memory implements short/long-term state from earlier modules.
Related topics
Practice task
Complete each numbered practice step in a notebook or doc before opening the next lesson.
- Sketch a 3-hop LCEL-style chain for “FAQ answer with citation” (retrieve → prompt → parse).
- List 4 tools for a support agent and write one-line descriptions + why each schema field exists.
- Choose a memory type for a multi-turn shopping assistant and justify it in 2 sentences.
- Write one scenario that should stay a chain and one that needs an agent.
- Answer interview questions 1, 2, and 5 in two sentences each.