LangChain in Practice: A Full Walkthrough from Agent Architecture to Message Passing

LangChain fills the LLM's gaps in knowledge cutoff and external system interaction via its tool layer and ecosystem.
Starting from agent architecture, the article explains that the LLM is the Agent's core for intent recognition and result generation, but it has two hard shortcomings: knowledge limited to its training cutoff and no direct external system interaction—exactly where LangChain fills in, mainly in Agents and RAG. It then lays out a learning path: quick start for model interaction and message roles, async calls, batching, structured output, and finally the distinction that the Agent, not the model, interacts with external tools.
Why LangChain? Starting from the LLM's Shortcomings
Many developers, when first encountering LangChain, wonder what problem it actually solves. To answer that, we need to go back to the architecture of the agent itself. On the Agent core components page of LangChain's official documentation, there is a high-level flowchart ("feel free to check it out"): the user sends a request, the request is passed to the large language model, and the model performs intent recognition; if the task exceeds the model's capabilities (such as querying real-time weather), the model requests a tool to access external resources, the tool returns its results to the model for integration, and finally a response is delivered to the user.
This flow reveals a key fact: the most central role in the entire agent is the model in the middle, which is responsible for intent judgment and result generation. But the model itself has two obvious shortcomings: its knowledge is limited to its training cutoff, and it cannot directly interact with external systems. The value of frameworks like LangChain is to fill in at these two gaps.
Agent refers to a system that uses a large language model as its decision-making core and can autonomously plan and call external tools to accomplish complex tasks. Unlike traditional single-turn Q&A, an Agent iterates repeatedly in a "think—act—observe" loop: the model first judges what information the current state requires, decides which tool to call, and after receiving the tool's return value decides the next step. LangChain's Agent module abstracts this loop into reusable components—developers only need to define the tools and prompts, and the framework handles the back-and-forth between model and tools. This is also the prerequisite for understanding the architecture diagram throughout this article.



LangChain's Core Components and Ecosystem Positioning
LangChain is not a single-purpose library but a framework aggregated from multiple functional modules. The official documentation clearly lists several core components, and this is the overall picture you must establish first when learning LangChain.
From an ecosystem standpoint, LangChain lands in two main directions within LLM applications: Agents and RAG (Retrieval-Augmented Generation) knowledge bases. The former gives the model the ability to call tools, while the latter enables the model to answer questions based on external knowledge. Once you understand these two scenarios, you understand why LangChain is designed the way it is.
The Two Hard Shortcomings of LLMs
The first shortcoming is knowledge timeliness. Taking DeepSeekAd as an example, different model versions have different training cutoffs (V1 in the second half of 2023, V2 and V3 in 2024, R1 in January 2025). When you use R1, it only has information from before January 2025. The reason it can sometimes answer real-time questions is that deep thinking or intelligent search is enabled, letting it query the internet.
The second shortcoming is the lack of ability to interact with external systems. If you turn off intelligent search and ask about tomorrow's weather in Beijing, the model will explicitly tell you it cannot obtain real-time data. Only when intelligent search is checked will it seek help from tools ("after handing it to the model"). This comparison intuitively shows that a pure model has no external interaction capability, and only after adding a tool layer is this gap filled ("now we've reached the position of our tools").
RAG (Retrieval-Augmented Generation) is an application paradigm that combines "retrieval" with "generation": first, external documents are chunked and vectorized into a vector store; when a user asks a question, the most relevant chunks are retrieved, and these chunks are spliced into the prompt as context for the LLM to generate an answer. This way the model can answer based on private or up-to-date knowledge without retraining, while also reducing hallucinations. LangChain provides a full set of components—document loaders, text splitters, vector stores, retrievers, and more—that string the various stages of RAG into a pipeline. This is also its most important landing point besides Agents.
Quick Start: Model Interaction and Message Passing
After understanding the framework's positioning, the second step is to go through the basic code for interacting with models in LangChain. This part has a low cost and is a quick way to build hands-on intuition.
More importantly, you need to understand the message passing mechanism during interaction. There are multiple types of messages exchanged with the model: user questions, AI responses, tool call requests, and tool call results. Each message type corresponds to a specific role, and LangChain provides corresponding APIs to distinguish them. Without clarifying this role system, it's easy to get confused when writing Agent logic later.
LangChain's message role system mainly includes four types: SystemMessage sets the model's overall behavior and constraints, HumanMessage represents user input, AIMessage carries the model's returned text or tool call requests, and ToolMessage encapsulates the results of tool execution passed back to the model. A complete tool call often consists of an AIMessage initiating the request, a ToolMessage returning the result, and then a new AIMessage providing the final answer. Distinguishing these roles is not just for code readability—the model itself relies on role markers to understand the source and priority of the conversation context.
Optimization Directions for Program-Model Interaction
By default, program-model interaction is synchronous and blocking: after you submit a task, you can only wait for the model to return, and you can't do anything else in the meantime. For large-volume interaction scenarios, this approach is clearly not viable, so you need to understand asynchronous calls and batch operations.
Another pain point is parsing model responses. The LLM returns natural language, essentially a large string ("DeepSeek"), which is hard for programs to parse directly. This is also one of the problems LangChain aims to solve—through approaches like structured output, results become consumable by programs.
Structured Output means having the model return results according to a predefined schema (such as a JSON Schema or Pydantic model) rather than free text. LangChain uses methods like with_structured_output to convert the schema into a format the model can understand, constrain its output, and then automatically parse it into objects in the program. Compared with manual string parsing, structured output greatly improves reliability and type safety. It is a key means of making model results truly consumable by downstream code and a foundation for building stable Agent pipelines.
How Agents Interact with External Resources
Pay special attention here: it is the Agent, not the model itself, that can interact with external resources. The model is only responsible for judgment and generation; what actually accesses external systems is the tool layer. Understanding this is essential to thinking clearly about how to design the tool-calling chain in a program, so that the model can obtain external results and perform further analysis. This is also the key watershed between "being able to use a model" and "being able to build an agent."
Conclusion
The thread of this tutorial is clear: first establish a high-level understanding of Agent architecture, then understand LangChain's core components and ecosystem positioning, then build hands-on intuition through quick-start code, then dive into message passing, async batching, and response parsing, and finally land on implementing Agent interaction with external resources. For beginners, this path avoids the detour of diving straight into API documentation—first figure out the "why," then get hands-on with the "how."
One-line Summary
LangChain compensates for the LLM's limited knowledge and inability to interact with external systems through its tool layer and ecosystem components.
Paragraph Summary
Starting from agent architecture, the article points out that the core of an Agent is the LLM responsible for intent recognition and result generation, and that the model has two hard shortcomings: knowledge limited to its training cutoff and inability to directly interact with external systems—which is exactly where LangChain fills in. Its ecosystem mainly lands in two directions: Agents and RAG knowledge bases. The article then outlines a learning path: first build hands-on intuition for model interaction through quick start and understand message roles such as user, AI, tool requests, and tool results; then master async calls, batch operations, and structured output to optimize interaction; and finally clarify that it is the Agent, not the model itself, that interacts with external resources, with the tool layer being the key watershed.
Related articles

Next.js 16.1 in Practice: Building the Frontend of an AI Event Booking App
Based on a Bilibili English tutorial, a complete breakdown of building the frontend for a Next.js 16.1 event booking app: create-next-app configuration, default template cleanup, Tailwind verification, Montserrat font and global font weight setup, plus practical tips.

LangChain+LangGraph+MCP+Agent Enterprise Practice: LLM Selection and Deployment Guide
Based on a Bilibili course, this guide analyzes LLM selection for enterprise LangChain, LangGraph, MCP, and Agent development, comparing DeepSeek and Qwen3 capabilities including Function Calling, MoE, thinking-mode switching, and MCP updates.

Half-Price USB-C EarPods: Why Wired Earphones Are Still a Practical Choice
Apple USB-C EarPods are half-price at Best Buy during Cyber Monday for just $9.99. This article analyzes the practical value of wired earphones in the wireless era, who they suit, and buying advice.