We study a class of heterogeneous agent-based models which are based on a basic set of principles, and the most fundamental operations of an economic system: trade and product transformations. A basic guiding principle is scale invariance, which means that the dynamics of the economy should not depend on the units used to measure the diﬀerent products. We develop the idea of a “near-equilibrium” expansion which allow us to study the dynamics of ﬂuctuations around economic equilibrium. This is similar to the familiar “perturbation theory” studied in many areas of physics. We study some simple models of both centralized and decentralized markets. We show the relaxation to equilibrium when appropriate. More interestingly, we study a simple model of a decentralized market that shows a spontaneous transition into a monetary phase. We use mean ﬁeld theory analysis to provide a statistical interpretation of the monetary phase. Furthermore, we show that such phase can be dynamically unstable. Finally, we study some simple centralized ﬁnancial markets, one of which shows a speculative bubble and a crash.