The modern foreign approach to the problem of quality management, widely accepted, studied and applied in Russia, unambiguously links quality and competitiveness, because the size of profit and market value of the enterprise directly depends on it in market conditions. The article considers the mechanism of minimizing the cost of quality in the construction of a quality management system in the enterprise. Quality management is regarded as an economic problem and appropriate approaches and methods should be applied to solve it. From this standpoint, minimization of cost of quality can be expressed in the form of an optimization problem that consists in determining the function minimum under a number of constraints. The goal of the study is to develop an optimization problem of minimizing the cost of quality. The article presents a hierarchical management scheme in the quality management system at the enterprise, where three main groups of factors are the objects of management: product, production process and factors of production. In our opinion, the management process itself can be divided into two categories: analysis of available results and direct control action. We have also given a classification of costs for quality, constructed a scheme of interaction of elements of costs for quality in a quality management system at an enterprise, where the element of «preventive» costs is a key element in the cost effectiveness mechanism, since the costs of prevention can increase given stable relative costs of quality control and steadily decreasing costs arising from defects and the cost of quality in general. The article presents the optimization problem of minimizing the cost of quality, which is reduced to optimal distribution of the limited limit of allocated resources to improve the production process between different groups of quality costs, the task is solved by the method of dynamic programming. The optimal solution corresponds to a minimum of costs for quality with a different allocation of resources.