← Master Index
Vol. 15 Module 15.4 Lecture

LangChain

Agent Frameworks (cross-ref Vol. 14.3)

How This Lesson Fits the Module & Volume

Volume 14.3 taught LangChain as RAG orchestration—LCEL chains, retrievers, linear Q&A. Do not retake that lecture. Here LangChain is the agent toolkit: bind tools, run a ReAct / tool-calling loop, attach working and long-term memory, and optionally call MCP tools.

When the loop needs durable state, HITL interrupts, or explicit branches, graduate to LangGraph (and its Vol. 14.3 counterpart). For multi-role crews, see CrewAI and AutoGen.

Learning Objectives

By the end of this lesson, students should be able to:

  • Contrast LangChain chains (Vol. 14.3) with LangChain agents (this lecture).
  • Describe the tool-calling / ReAct loop: reason → act → observe → repeat.
  • Wire tools (including MCP-shaped ones) into a LangChain agent.
  • Place chat history vs retrievers vs episode stores in the 15.2 memory model.
  • Know when to stay on LangChain agents vs move to LangGraph.
  • Set iteration caps, HITL hooks, and tracing for production loops.
Definition

A LangChain agent is a runnable that lets an LLM choose tools in a loop until it can answer or hits a stop condition. Unlike an LCEL RAG chain (retrieve → prompt → generate once), the control flow is model-directed: each step may call a tool, read the observation, and continue—the agent loop from Module 15.1, with LangChain glue.

Vol. 14.3 vs Vol. 15.4 — Same Product, Different Job

Vol. 14.3 LangChainThis lecture (agents)
Control flowMostly linear LCEL DAGCyclic tool loop (ReAct / tool-calling)
Primary I/ORetriever + promptTools + observations (+ optional retrieve)
MemoryChain “memory” for chat RAGWM scratch + LTM/episodes as tools
Stop conditionChain returnsFinal answer, max steps, or HITL
Next upgradeLangGraph for RAG critique loopsLangGraph for durable agent graphs

ReAct vs Native Tool Calling

ReAct (text)

  • Thought / Action / Observation
  • Works with weaker tool APIs
  • Parse-fragile; verbose WM

Tool calling (API)

  • Vendor function-call fields
  • Preferred default today
  • Matches MCP ↔ LLM schemas

When to graph

  • Retries, branches, HITL pause
  • Multi-actor shared state
  • Use LangGraph

Minimal Tool-Calling Agent

API names shift across LangChain versions (create_tool_calling_agent, create_agent, etc.). Learn the pattern: tools with schemas, a model that can call them, a loop executor, a hard step limit.

from langchain_core.tools import tool from langchain_openai import ChatOpenAI @tool def lookup_ticket(ticket_id: str) -> str: """Read a support ticket by id. Read-only.""" return f"#{ticket_id} open: contractor SSO, missing okta-admins" @tool def semantic_lookup(query: str) -> str: """Search long-term semantic memory (policies). Not user history.""" return retriever.invoke(query) # vector store = LTM semantic @tool def episode_lookup(query: str) -> str: """Search this user's past run recaps (episodic LTM).""" hits = episode_store.similar(query, k=3) return "\n".join(e.recap for e in hits) or "No episodes." llm = ChatOpenAI(model="gpt-4o-mini").bind_tools( [lookup_ticket, semantic_lookup, episode_lookup] ) def run_agent(goal: str, max_steps: int = 8) -> str: messages = [{"role": "user", "content": goal}] for _ in range(max_steps): ai = llm.invoke(messages) messages.append(ai) if not getattr(ai, "tool_calls", None): return ai.content # final answer for call in ai.tool_calls: fn = {"lookup_ticket": lookup_ticket, "semantic_lookup": semantic_lookup, "episode_lookup": episode_lookup}[call["name"]] obs = fn.invoke(call["args"]) messages.append({"role": "tool", "tool_call_id": call["id"], "content": obs}) return "Stopped: max steps (check traces / HITL)." print(run_agent("Can we enable SSO for this contractor on ticket 881?"))

Memory Wiring (15.2 Recap in LangChain)

Chat message lists are working memory—compact them. Retrievers are long-term semantic stores (Vol. 14 RAG). Episode tools are long-term episodic. Do not dump the full thread into every tool. For checkpoints and interrupts, do not stretch LangChain agents; use LangGraph.

Pick LangChain agents when

  • Single actor, short tool loops
  • You already live in LCEL / LC tools
  • MCP or vendor tools just need a loop
  • Prototype before a full graph

Move on when

  • HITL pause/resume is required
  • Branchy workflows or multi-agent
  • You need typed durable state
  • Hidden loops are undebuggable
Common Misconception

“LangChain agents replace LangGraph.” Classic agents hide the loop inside an executor. LangGraph makes the loop a graph you can inspect, interrupt, and checkpoint. Vol. 14.3 already warned that serious cyclic agents belong on graphs; Volume 15 is where that warning becomes the default architecture for production HITL systems.

Knowledge Check

  1. Short Answer: How does a LangChain agent differ from a Vol. 14.3 RAG chain? Answer: The agent runs a model-directed tool loop; the chain is mostly linear retrieve→generate.
  2. True/False: This lecture is a repeat of LCEL RAG orchestration. Answer: False—it is the agent/tool-loop angle.
  3. Multiple Choice: ReAct traces are primarily: (a) working memory, (b) GPU kernels, (c) MCP transports. Answer: (a).
  4. Short Answer: Name two stop conditions for a tool loop. Answer: Final answer (no tool calls) and max steps / HITL abort.
  5. True/False: A vector retriever tool is long-term semantic memory. Answer: True.
  6. Multiple Choice: Durable HITL interrupts are best in: (a) unbounded LC agent executor, (b) LangGraph, (c) pooling layers. Answer: (b).
  7. Short Answer: How do MCP tools show up in a LangChain agent? Answer: Client maps tools/list to LLM tool schemas; loop calls tools/call.
  8. True/False: Native tool calling is generally more robust than parsed ReAct text. Answer: True.
  9. Multiple Choice: episode_lookup is: (a) semantic wiki, (b) episodic recall, (c) stdio transport. Answer: (b).
  10. Short Answer: Which next lecture is the agent control plane? Answer: LangGraph.

Key Takeaways

  • Vol. 14.3 = chains/RAG; Vol. 15.4 = tool-calling / ReAct agents.
  • Bind tools (semantic, episodic, MCP) and cap the loop.
  • Chat history is working memory—compact it.
  • Graduate to LangGraph for state, HITL, and inspectable cycles.
  • Continue with LangGraph.
Trainer’s Guide

Lab: Run the three-tool sketch; force max_steps=2 vs 8 and discuss incomplete work vs runaway cost. Optionally swap lookup_ticket for a fake MCP client.invoke.

Whiteboard: Side-by-side Vol. 14.3 LCEL RAG vs this loop. Circle the upgrade path to LangGraph interrupts.

Recap: LangChain agents are tool loops, not RAG chains. Continue with LangGraph.