AI Underlying Architecture
AI Underlying Architecture
When learning AI application development, you often encounter concepts such as LLM, Transformer, Token, Prompt, RAG, MCP, Agent, and Skill. What problems do these technologies solve, and what is the relationship between them?
From foundational large models to AI Agents capable of executing tasks, a complete AI application typically involves multiple components such as model inference, context management, tool calling, task orchestration, and business applications.
For example, when asking AI to "read Excel sales data, analyze growth trends, and generate a report," the following processes may be involved:
- The large language model understands the user's request.
- The system determines the scope of the task by combining context and relevant materials.
- The Agent determines the need to read files and perform data analysis.
- Run Python code using tools to process data and generate charts.
- Organize the analysis results into a report and return it to the user.
For easier understanding, we can divide the related technologies into five layers based on their function.

**Note:** The five-layer architecture shown in the figure is a functional division for teaching purposes and is not a universal technical standard that all AI systems must follow. Different layers can cooperate with each other, and some components can be omitted according to actual needs.
Five Layers of an AI System
| Layer | Core Technology | Main Function |
|---|---|---|
| Foundation Model Layer | LLM, Token, Transformer, Attention | Processes input information, performs model inference, and generates content. |
| Context Layer | Prompt, Context Window, Memory, RAG | Organizes model input, combining conversation history and external knowledge. |
| Tool Layer | Tool Calling, API, MCP, Database | Connect external programs, data sources, and services to perform specific operations. |
| Agent Layer | Agent, Planning, Agent Loop | Select operations based on goals, organize multi-step tasks, and handle feedback. |
| Application Layer | Skill, Workflow, Automation | Combine model and tool capabilities into actual products and business processes. |
These five layers help us understand the process of AI applications from underlying capabilities to actual implementation.
The foundation model layer is responsible for providing inference and generation capabilities; the context layer supplements the model with task information; the tool layer is responsible for interacting with external systems; the agent layer organizes task execution; and the application layer applies these capabilities to specific business needs.
Next, we will introduce each layer separately.
1. Foundation Model Layer (Model)
The foundation model layer is an important component of AI applications, responsible for computing the input and generating text or other forms of output.
Common fundamental concepts in large language model applications include Token, Transformer, and attention mechanism.
Token
A Token is the basic unit used by the model when processing text.
Models usually do not process an entire piece of text as an indivisible whole directly, but first convert it into a sequence of Tokens using a Tokenizer, which is then mapped to a numerical representation that the model can compute.
A Token may correspond to a character, a part of a word, a whole word, or punctuation, depending on the tokenizer used by the model.
For example:
Original prompt (Chinese, left untranslated)
用户输入:
Hello, AI!
Possible results after tokenization:
["Hello", ",", " AI", "!"]
The above is only illustrative and does not represent the actual tokenization results of all models. Different models' Tokenizers may produce different token sequences.
The number of Tokens affects context usage, model calling costs, and input/output length limits.
Transformer
Transformer is a widely used neural network architecture in modern large language models.
It models information in the input sequence through structures such as the attention mechanism.
For example, in the sentence "The programmer is using Python to write code", the model needs to combine context to understand that Python refers to a programming language, not some other meaning.
One of the important characteristics of Transformer is its ability to utilize the attention mechanism to process the information relationship between different positions in a sequence.
Attention
The attention mechanism is used to calculate the degree of information correlation between different positions in a sequence, enabling the model to process by combining relevant context.
Self-Attention is an important component of Transformer.
It should be noted that the attention mechanism is only a part of the model's internal computation and is not equivalent to the model having human-like understanding or reasoning processes.
LLM
LLM (Large Language Model) is a language model obtained through training on large-scale data.
Many generative LLMs predict the probability distribution of subsequent tokens based on the existing input and generated tokens during inference, and generate content step by step.
Common models include series models such as GPT, Claude, DeepSeek, and Qwen.
Foundation models can be used for:
- Natural language question answering and text generation.
- Code generation, explanation, and modification.
- Text classification, information extraction, and content summarization.
- Reasoning and generating structured results based on context.
However, the model itself may not have the ability to access real-time information, read local files, or operate external software. These functions usually require the application to provide relevant tools.
2. Context Layer (Context)
The context layer mainly solves one problem:What information should the model receive when processing the current task?
The model's output is influenced not only by its own capabilities but also by the input instructions, conversation history, and reference materials.
Common context-related technologies include Prompt, Context Window, Memory, and RAG.
Prompt
Prompt is the input content provided to the model, usually used to describe the task, specify requirements, and provide necessary information.
For example:
Original prompt (Chinese, left untranslated)
请分析下面的销售数据。
要求:
1. 计算每个月的销售额。
2. 找出销售额最高的月份。
3. 使用表格输出结果。
4. 不要编造缺失数据。
Prompt can contain task instructions, input data, examples, and output requirements.
In practical applications, model requests may also include system instructions, developer instructions, and multi-turn conversation messages, with different messages having different purposes and priorities.
Context Window
The context window is the scope of context that the model can process in a single inference, usually measured in the number of tokens.
The context may include:
- System instructions and user input.
- Historical conversation messages.
- Retrieved reference materials.
- Tool invocation results.
- Data that the current task needs to process.
For example, a model that supports a longer context window can process more document content at once, but this does not mean the model can permanently remember this information.
When the context is too long, the application may need to summarize, truncate, or retrieve to retain the content most relevant to the current task.
Memory
Memory is used to save and utilize historical information in AI applications.
It can usually be divided into:
- **Short-term memory:** Stores the current session, task status, and intermediate execution results.
- **Long-term memory:** Stores information that needs to be used across sessions, such as user preferences, project materials, and historical task records.
Long-term memory is usually implemented by databases, file systems, or other external storage, and is not an inherent permanent memory capability of the model.
For example, an AI programming assistant can read previously saved project descriptions to understand the programming language, directory structure, and coding standards used in the project.
RAG (Retrieval-Augmented Generation)
RAG (Retrieval-Augmented Generation) is a technology that combines external information retrieval with model generation.
It is mainly used to have the model query relevant materials before answering a question, and then generate content based on the retrieved results.
The typical workflow is as follows:
Original prompt (Chinese, left untranslated)
用户提出问题
↓
检索知识库
↓
获取相关资料
↓
将资料加入模型上下文
↓
模型生成回答
For example, an enterprise can build an internal document knowledge base. When an employee asks about the company's reimbursement policy, the system first retrieves the relevant policy documents, and then the model answers based on the document content.
RAG can reduce errors caused by the model lacking professional information, but it cannot guarantee that all answers are accurate. The quality of retrieval, the timeliness of the materials, and the model's understanding of the materials will all affect the final result.
Note that RAG is a method combining information retrieval and generation, and is not equivalent to long-term memory.
3. Capability Extension Layer (Tools)
The Capability Extension Layer is responsible for allowing AI applications to access external tools and data sources.
For example, a model can generate a piece of Python code, but to actually run the code, it needs to connect to a code execution environment.
Similarly, operations such as reading a database, querying real-time weather, or sending emails must be completed through tools or external services.
Tool Calling
Tool Calling is a common mechanism for models to use external tools.
Developers provide the model with tool names, functional descriptions, and parameter definitions. The model determines whether to call a tool based on the current task and generates the corresponding call request.
For example, define a weather query tool:
Original prompt (Chinese, left untranslated)
{
"name": "get_weather",
"description": "查询指定城市的天气",
"parameters": {
"city": "string"
}
}
When the user requests to query the weather in Beijing, the model can generate a tool call request containing the city parameter.
The application is responsible for validating the parameters, executing the actual weather query, and then returning the results to the model.
Therefore, Tool Calling is not the model directly executing code, but rather a collaborative mechanism between the model and an external execution system.
MCP (Model Context Protocol)
MCP (Model Context Protocol) is an open protocol used to standardize the connection method between AI applications and external tools, resources, and services.
Through MCP, developers can provide capabilities such as file systems, databases, and search services to applications that support MCP via a unified protocol interface.
MCP adopts a client-server collaborative architecture, mainly including the following roles:
- **MCP Host:** Hosts AI applications and is responsible for coordinating connections and interactions.
- **MCP Client:** Establishes and maintains a connection with the MCP Server within the Host.
- **MCP Server:** Provides capabilities such as tools, resources, or prompt templates to clients.
For example, a database MCP Server can provide a database query tool to an AI application, enabling the Agent to query business data through a controlled interface.
**Note:** MCP is not a replacement for Tool Calling; they solve different problems.
- **Tool Calling:** Focuses on how the model selects tools and generates calling parameters.
- **MCP:** Focuses on how external tools and data capabilities are provided to AI applications in a standardized manner.
Developers can also implement tool calling directly through regular Python functions or HTTP APIs and do not necessarily need to use MCP.
API and Database
API is an interface for communication between applications, while the database is used to store and query data.
They can serve as the foundational capability for AI applications to connect with external systems.
For example:
- Calling a weather API to get real-time weather or weather forecasts.
- Querying databases like MySQL or PostgreSQL to retrieve business data.
- Reading and modifying documents through a file API.
- Calling third-party services to complete email sending or schedule management.
Tool invocation must comply with permission restrictions. When performing operations such as modifying data, sending messages, or deleting files, corresponding confirmation and review mechanisms should be set based on task risk.
4. Agent Layer
The Agent Layer is responsible for organizing task execution around user goals.
Compared to single model Q&A, an Agent can select tools, obtain execution results, and continue to decide the next step based on the current task status.
A typical Agent usually involves the following capabilities:
- Goal Understanding: Identifying the task the user wants to accomplish.
- Task Planning: Decomposing complex tasks into executable steps.
- Tool Selection: Choosing appropriate tools based on task requirements.
- Execution Feedback: Read tool results to determine if the operation was successful.
- Task Control: Determine whether to continue execution, request user confirmation, or terminate the task.
Agent Loop
The Agent Loop is a common operational mechanism for organizing model decision-making and tool execution.
The basic process is as follows:
Original prompt (Chinese, left untranslated)
接收任务
↓
分析任务与当前状态
↓
选择下一步操作
↓
调用工具
↓
获取工具结果
↓
判断任务是否完成
├── 否:继续执行
└── 是:输出结果
For example, a user requests the Agent to fix an error in a piece of code.
The Agent may first read the relevant files, then analyze the cause of the error, modify the code, and execute tests. If the test fails, it continues to modify based on the error message.
It should be noted that the Agent does not necessarily generate a complete plan before starting execution; it may also adopt a decision-making approach while executing.
Additionally, the Agent Loop should be configured with a maximum execution step count, call budget, timeout limit, and error handling mechanism to prevent infinite loops.
Planning, Explore, and Act
These terms are typically used to describe the different activities of an Agent during task execution:
| Concept | Function | Example |
|---|---|---|
| Explore | Gather information and understand the current environment | Read project files and search for information |
| Plan | Formulate execution steps and task arrangements | Determine which code files to modify |
| Act | Execute specific operations | Modify code and run tests |
These three concepts do not mean that all Agents must run strictly in the fixed order of Explore, Plan, Act.
During actual execution, the Agent may explore, adjust plans, and call tools multiple times.
5. Application Layer (Applications)
The application layer is responsible for combining models, tools, and Agent capabilities into actual user-facing products.
Common applications include AI programming assistants, document processing tools, data analysis systems, and enterprise business automation platforms.
Agent Skill
A Skill usually refers to reusable task capabilities or operational specifications.
In some Agent systems, a Skill can consist of documentation, operational steps, scripts, and related resources to guide the Agent in completing specific types of tasks.
For example, a Skill for processing PDFs may include:
- Operating instructions for PDF text extraction and analysis.
- Related Python scripts.
- File conversion and output specifications.
- Common errors and handling suggestions.
Note that Skill is not a unified technical standard adopted by all Agent frameworks. The definition and loading method of Skill may differ across various products.
Workflow
A Workflow is an execution process that organizes multiple task steps according to certain rules.
For example, a workflow for automatically generating a weekly report:
Original prompt (Chinese, left untranslated)
读取业务数据
↓
统计关键指标
↓
生成数据图表
↓
整理周报内容
↓
保存报告
A workflow can have fixed steps or can call an Agent at certain nodes to decide the next action based on the actual situation.
Therefore, Workflow and Agent are not completely contradictory. In practical applications, the two are often used together.
Automation
Automation emphasizes automatically executing tasks based on preset conditions, events, or time schedules.
For example:
- Automatically generate a daily sales report every morning.
- Automatically organize order information after receiving a new order.
- Send a notification when a task fails.
- Periodically read data and update business reports.
Automation does not necessarily require large language models. Traditional programs can also implement scheduled tasks, event triggers, and fixed workflows.
When an automation system is combined with an Agent, it can handle more tasks that require natural language understanding, unstructured data analysis, or dynamic tool selection.
Core Terminology Reference
Below are common technical terms in AI application development for quick reference.
| Term | Chinese Name | Main Function |
|---|---|---|
| LLM | Large Language Model | Perform language modeling, inference, and content generation |
| Token | Token | The basic unit for model processing input and output |
| Transformer | Transformer architecture | Process sequence information through mechanisms such as attention |
| Prompt | Prompt or input prompt | Describe the task, provide information, and constraints |
| Context Window | Context window | Limits the context range that can be processed in a single inference |
| Memory | Memory | Saving and using historical information and task status |
| RAG | Retrieval-Augmented Generation | Retrieving external information to assist the model in generating answers |
| Tool Calling | Tool Calling | Enabling the model to request the use of external tools |
| MCP | Model Context Protocol | Standardizing the connection of AI applications with external tools and resources |
| Agent | Intelligent Agent | Organizing decision-making, tool calling, and task execution around a goal |
| Agent Loop | Agent Execution Loop | Organize task decision-making, tool execution, and feedback processing |
| Skill | Reusable task capabilities | Encapsulate operation instructions and related resources for specific tasks |
| Workflow | Workflow | Organize multiple execution steps according to rules |
| Automation | Automation | Execute tasks based on preset conditions or plans |
AI Agent complete execution flow
We have introduced various technologies from foundation models to application layers. Next, we will connect them with a specific example.
Assume the user gives the Agent a task:
Read this month's sales data, calculate the sales amount for each product, generate a bar chart, and save an analysis report.
An Agent equipped with file reading, data analysis, and document generation capabilities can complete the task according to the following flow.
- **Receive Task:** The system obtains the user instruction and relevant file locations or attachment information.
- Prepare context: Organize task requirements, current session information, and necessary reference materials. Retrieval tools or RAG can be used to query external knowledge bases.
- Model decision: The model combines the context to determine whether to read files, process data, or generate a report.
- Task planning: The Agent determines the main steps, including reading data, calculating metrics, generating charts, and saving files.
- Tool execution: Call file tools to read data, and then use tools like Python to complete calculations and plotting.
- Result checking: Check the tool execution status, data calculation results, and whether the output files meet the requirements.
- Output results: Return the generated report or file location to the user, and save the task status as needed.
The entire process can be summarized as:
Original prompt (Chinese, left untranslated)
用户提出任务
↓
准备输入与上下文
↓
LLM 分析任务
↓
Agent 规划与决策
↓
调用工具执行操作
↓
检查执行结果
├── 需要继续:再次规划或调用工具
└── 任务完成:生成最终输出
In a real system, some steps can be controlled by fixed programs or decided by the model. For example, data reading and file saving can use predefined workflows, while the analysis method and chart selection can be determined by the Agent based on the data content.
**Note:** Not every Agent requires RAG, MCP, or long-term memory. The system can select appropriate components based on task scale, data sources, and actual business needs.
Summary
From large language models to AI Agent applications, it can be understood according to the responsibilities undertaken by different technologies:
- **Model Layer:** Provides language modeling, reasoning, and content generation capabilities.
- **Context Layer:** Organizes input information and supplements history and external knowledge as needed.
- **Tool Layer:** Connects external data sources and programs to execute actual operations.
- **Agent Layer:** Organizes decisions around goals, calls tools, and processes feedback.
- **Application Layer:** Combines these capabilities into products and business processes that users can use.
For beginners, you do not need to master all concepts at once.
You can first understand LLM, Token, and Prompt, then learn about API and Tool Calling, subsequently implement tool calling through a simple Agent Loop, and finally introduce technologies like RAG, MCP, Skill, and Workflow based on your needs.
In actual development, choosing the right technology is more important than stacking more components. A simple Agent with clear design, reliable tools, and controlled permissions is usually easier to maintain than a system with complex structure but lacking validation mechanisms.
Related materials: