[KongchangAI]
· 4 min read· 2,437 words

Agent Full-Stack Primer: LLM, RAG, MCP and Memory Management

Agent Full-Stack Primer: LLM, RAG, MCP and Memory Management

From LLM to Agent: a layer-by-layer breakdown of Function Calling, MCP, Skill, Harness and memory management.

Based on a hands-on course, this article systematically organizes the full-stack concepts of Agent. It explains why the LLM is only a "brain" and needs an outer Agent system, distinguishes Function Calling from Tool, and covers two memory solutions: summary compression and long-term memory via RAG. It also explains MCP as AI's USB-C interface with hot-pluggable tools, Skill as an on-demand reusable capability unit, CLI as a lightweight execution channel, and Harness as a dual-layer safety mechanism, closing with a full-stack concept map.

Agent Full-Stack Primer: LLM, RAG, MCP and Memory Management

Large models can write poetry and answer questions, but they can't help you check the weather or book a flight—the problem isn't intelligence, it's that they lack a mechanism to "take action." In this hands-on lesson, Bilibili creator 智博AI starts from the most fundamental workings of large models and traces the entire path up through Agent, Function Calling, MCP, Skill, CLI and Harness, taking apart every component of the agent tech stack and laying it out. This article restates the course content while adding some key points for understanding.

1. LLM: The Agent's Brain—But Not the Whole Story

智博AI likens the large model to the Agent's "core brain": it is trained on massive amounts of text via the Transformer architecture, and its core capability is natural language understanding and generation. But this brain has three innate characteristics: it can only generate output according to instructions, with no ability to initiate tasks on its own; it operates mainly through single interactions and lacks long-term memory management; and its knowledge is limited by the time range of its training data, so it can't get real-time information.

The process from input to output is broken down in fine detail in the course: text is first preprocessed and split into Tokens, each Token is mapped to an ID, then mapped to a set of vectors; these numerical representations enter the model, and the model outputs a probability distribution over each Token, sampling by probability and generating step by step. Precisely because it is essentially "sampling by probability," large models are inherently stochastic.

This layer is the foundation of the entire tech stack. The memory management, tool calling, and protocol encapsulation that appear later are all essentially patches applied to this "function that only answers once per call."

The Transformer architecture is a neural network structure proposed by Google in the 2017 paper "Attention Is All You Need." Its core is the self-attention mechanism: every Token in a sequence can directly "see" all other Tokens and compute relevance weights, thereby modeling long-range dependencies in parallel and breaking through the efficiency bottleneck of RNNs' word-by-word sequential processing. Almost all modern large models are based on the Decoder-only variant of Transformer, stacking dozens of layers of attention and feed-forward networks, combined with self-supervised pretraining on massive corpora to acquire language ability. Understanding this helps clarify: the model's output probability distribution comes from layer-by-layer computation over these attention weights, not from lookup tables or rule matching, so its "knowledge" is inherently statistical and can be wrong.

2. Where the Boundary Between Agent and LLM Lies

An Agent can be understood as the whole of "large model + outer Agent system." Its working loop is: clarify the task goal → plan and decompose → call tools → get feedback → confirm whether the goal is achieved; if not, return to planning and continue the loop until the task is complete, then hand the result to the user.

The course uses a weather-check demo: the user asks to check Beijing's weather for the day and decide whether to bring an umbrella. The Agent first confirms the task, thinks, then calls the weather query tool, gets the result and thinks again, calls the send tool to push the information to the user, and finally gives a conclusion. This is a typical ReAct process.

Finally it reached the final conclusion of this task

The positioning difference between the two can be laid out in a comparison table: an LLM is a tool that is used, existing as a brain, responding passively with stateless memory; once wrapped in an Agent system, it becomes a system that can be delegated to, able to decide actions autonomously, execute proactively, maintain stateful memory, and also use tools and reflect for optimization.

The course offers a nice boundary judgment: a person without hands and feet is still a person, but if a large model loses the ability to call tools and act autonomously, it is no longer an Agent and can only degrade into a large model. This sentence highlights that the difference between the two is conceptual, not a difference in the number of features.

ReAct (Reasoning + Acting) is an Agent paradigm proposed in 2022. Its core idea is to have the model alternately produce "Thought—Action—Observation" triples: first reason about what to do now, then call a tool, and after getting the returned result continue reasoning, looping until a conclusion is reached. It interleaves the reasoning trajectory with external tool feedback in the same context, enabling the model to dynamically revise its plan based on real execution results rather than answering once based on internal knowledge. The weather-check case is a concrete embodiment of this loop. Later patterns such as Plan-and-Execute and Reflexion can all be seen as variants or improvements on ReAct.

3. Function Calling and Tool: How the Model "Takes Action"

A large model only outputs probability distributions and Tokens, while executing a function is an action that interacts with reality—the model can't do it itself, so only the Agent system can do it on its behalf. The process is: the user describes the available tools to the Agent; when the model wants to call a tool, it returns a string; the Agent system parses the string, converts it to JSON, extracts the function name and parameters; the Agent system executes the function on the model's behalf, then returns the result to the model.

The course uses an analogy for the relationship between Tool and Function Calling: when you call a driver to pick you up, the act of "making the call" is Function Calling, and "the driver picks you up and takes you to the destination" is the Tool. A Tool is a function written in advance by a human that exposes an interface to the Agent. Clarifying these two concepts will make it much smoother to work with MCP and write Skills later.

4. Memory Management: Giving the Model the Ability to "Not Forget"

Why is memory management needed? Two reasons. First, the context window limit: the number of input Tokens is finite (the course mentions common limits on the order of 8,000 to 120,000), and beyond that the model either discards the earliest information or compresses it, and over time it "loses memory." Second, the model itself tends to forget older conversation content.

Its context window limit

There are two corresponding solutions. Memory compression: when approaching the window limit, the Agent system sends early multi-turn conversations to the model to generate a brief summary, replacing the original conversation that consumed thousands of Tokens with a summary of a few Tokens, freeing up space to continue chatting. Long-term memory: write memories to external storage, retrieve them via RAG (retrieval-augmented generation) when needed, and inject them into the model context, forming a layered memory structure. RAG is only touched on briefly in this lesson; the course explicitly says it will be covered in depth later.

RAG (retrieval-augmented generation) has a basic pipeline: first chunk external documents (Chunking), use an Embedding model to encode each chunk into a vector and store it in a vector database; when the user asks a question, encode the question into a vector as well, retrieve the most relevant fragments via similarity search (such as cosine similarity), splice them into the prompt as context, and then have the model generate an answer based on these fragments. Its value lies in letting the model bypass the time boundary of training data, access private or real-time knowledge, and reduce hallucinations. The "long-term memory" in memory management is exactly an application of RAG: vectorize and store historical conversations, and recall them by semantic relevance when needed rather than stuffing everything back into the context.

5. MCP: The USB-C Interface of the AI Era

MCP stands for Model Context Protocol, proposed by Anthropic, positioned to enable LLMs to integrate with external data sources and tools without bespoke integration. It has three major primitives: Prompt (predefined instruction templates that introduce tool usage to the model), Resource (providing structured context data), and Tool (integrating tools for the model to call on demand).

The biggest difference between MCP and directly handing tools to the model is "hot plugging." Directly defined tools are fixed, and adding new tools often requires restarting the entire service; but an MCP service can add tools directly on the server side, and when the Agent requests, it can obtain the new tool information and invocation methods without restarting the host and client. This is exactly why it is called "AI's USB-C interface"—standardized and extensible.

So how does NCP work

The workflow is: the Agent first asks the MCP server what tools or resources it provides and gets a list; after the user asks a question, the model recalls the tool definitions and calls them appropriately; the Agent, as the client, sends a standard request to the MCP service, the server parses and executes it, and the result is returned; the Agent injects the information into the model, and the model summarizes and replies to the user.

(The original text writes MCP as "NCP" in several places, which should be a slip of the tongue or transcription error; here it is understood as MCP.)

MCP uses a client-server architecture: a Host (such as an IDE or desktop assistant) embeds an MCP Client and establishes connections with one or more MCP Servers; the Server declares the Prompts, Resources, and Tools it provides in a standardized way, and the Client exposes capabilities to the model accordingly. The transport layer commonly uses stdio (local process) and HTTP-based SSE/Streamable HTTP (remote services), so the same protocol can connect both local scripts and cloud services. This layering completely decouples the "provider" and "consumer" of tools—a tool author only needs to implement a Server once, and any Host that supports MCP can plug and play, which is also the technical basis for the hot-plugging capability.

6. Skill: A Capability Unit Loaded on Demand

A Skill is a set of instructions and resources loaded on demand, encapsulating goals, processes, tools, knowledge, and constraints into a reusable capability unit. It can be simply understood as "a prompt for process control + the corresponding tool structure." It has four major characteristics: on-demand loading (the model finds and loads it according to the task), reusable (prompts and tools packaged into a folder can be shared), versionable (supports multi-version management), and composable (multiple Skills collaborate to complete a task).

In the standard folder structure, skill.md stores the prompt, reference holds reference materials, tool holds Python scripts or other scripts, and there are also static resources (images, reference text, videos, etc.). The workflow is: user input → the model identifies the intent → loads the required skill.md → injects it into the system prompt (steps, role setting, output format) → registers the tools within the Skill → reasons according to the prompt and calls tools → combines results and loops reasoning → outputs.

7. CLI and Harness: Execution Channel and Safety Shell

The CLI command-line interface has taken on new meaning in the AI era. A large amount of CLI data exists in AI training corpora, so models are naturally good at writing CLI commands and can complete predefined operations such as file reading and writing and browser manipulation with extremely low Token consumption, with an interaction path much lighter than MCP.

When the user expresses what to do

Harness (harness engineering) is a safe and controllable shell wrapped around an Agent; the classic definition is Harness Agent = Model + Harness. It constrains model behavior through reinforced prompts and external detection mechanisms. The course gives an example: when developing a to-do API system, first write out the architectural layering, mandatory dependency rules, and current requirements—this is constraint at the prompt level; the Harness agent parses the task intent and executes accordingly; the system can designate other Agents to verify or launch an application review, and the Harness agent autonomously fixes defects based on the review results and submits.

In one sentence: Harness ≈ prompt constraints + engineering constraints. Prompt constraints tell the model what it can and cannot do; engineering constraints use code to detect and intercept out-of-bounds behavior, sending erroneous results back to the model to avoid catastrophic consequences.

8. One Diagram to Tie Together the Full-Stack Concepts

The course ends with a synthesis: LLM is the brain for language understanding and generation, the most basic capability layer; Agent is the whole of LLM plus the Agent system (including Harness); Function Calling is the bridge for the model to call external functions; Tool is a tool that directly serves the model; MCP is the USB-C interface for unified discovery and invocation of tools; Skill is a highly encapsulated, reusable process and application; CLI is an important interaction method for the model to execute commands; Memory handles memory management, compensating for context window limits and helping the model remember context. RAG, as a key technology for retrieval-augmented generation, is left for subsequent lessons.

The value of this concept map is that it breaks "building an Agent" down from mysticism into modules that can be tackled one by one. When actually getting started, what most people get stuck on is not model capability but engineering details such as tool registration, memory layering, and safety constraints—which is exactly the part this lesson focuses on most.

One-line Summary

From LLM to Agent, this article breaks down the tech stack of Function Calling, MCP, Skill, Harness and memory management layer by layer.

Paragraph Summary

Based on the hands-on course by Bilibili creator 智博AI, this article systematically organizes the full-stack concepts of Agent. It first points out that although the LLM is the "brain," it can only generate once, lacks proactivity and long-term memory, and therefore needs an outer Agent system to fill the gap; it then distinguishes Function Calling (the calling action) from Tool (the function being called), explaining how the model "takes action." In the memory management section, it proposes two solutions: summary compression when the context approaches its limit, and long-term memory formed via external storage and RAG retrieval. MCP is analogized to the USB-C interface of the AI era, with its core advantage being hot plugging and unified discovery of tools; Skill is a capability unit that is loaded on demand, reusable, and composable. Finally, it introduces CLI as a lightweight execution channel and Harness as a dual-layer mechanism of prompt constraints and engineering constraints that keeps Agent behavior safe and controllable, closing with a concept map of the full stack.

Share:

Related articles