Constraint satisfaction problems (CSPs) are well studied problems, at the interface between computer science, optimization, and machine learning. Typically, one has a set of N variables, which have to satisfy a number M of constraints. For example, the variables could be the weights of a neural network, and each constraint imposes that the network satisfies the correct input-output relation on one of M training examples, for instance distinguishing images of cats from dogs. The idea behind our work is that the economy can also be thought of as a constraint satisfaction problem. Agents in the economy have to satisfy budget and/or production constraints, and they thus have to adjust their strategies to maximize their profit while satisfying these constraints. Our objective is to incorporate a budget constraint in the simplest possible way and explore whether this model undergoes a phase transition. Phase transitions appear in the macroeconomy as ``dark corners’’ where the economy becomes strongly non-linear and the response to small fluctuations can become catastrophically large. Our present model confirms the central role that debt levels play in the stability of the economy: too high a debt level and we have periodic crises, too low a debt level and the agents cannot sustain themselves long enough for long-lived structures to appear and survive. Our work presents a break from previous studies on the leverage cycle by coupling the production output and trading within the economy with agents’ budgetary constraints. In this work, we formulate a simple macroeconomic Agent-Based Model based on a modification of the well-known ‘perceptron’ model of machine learning. The variables generally used in the study of the perceptron are given a precise economic interpretation. All agents in our model subject to a budgetary constraint and are allowed to incur debt. They are boundedly rational, and we use heuristics to rules to model agent behavior. We find that the model displays an interesting phase transition between a ‘good’ phase characterised by a low bankruptcy rate and a ‘bad’ phase in which the economy collapses leading to frequent bankruptcies. The transition from one phase to the other is controlled by the average level of debt in the economy. The ‘bad’ phase is found for low levels of debt. On the other hand, a higher level of debt allows agents to be flexible in their behavior and bankruptcy rates are low. Moreover, this transition is robust to a change in other parameters of the model such as the average production cost of goods. The phase with high-debt is characterized by long-lived agents with very rich dynamics at the aggregate and the micro level. In this phase, there are dynamical switches between goods - spontaneously a high-demand good can experience a fall and another low-demand good will take its place. Interestingly, at the aggregate level, the distributions of the demand ( supply) also vary dynamically and are found to be bimodal. Finally, the ‘good’ phase also has two distinct regimes for intermediate levels of debt, the economy is stable with a low bankruptcy rate and with low volatility. However, for extremely high levels of debt, the economy enters a regime where the bankruptcy rate is still low but periodically undergoes crashes. These endogenously created cycles make the economy highly volatile. We conclude that there exists a ‘Goldilocks’ zone: a sustainable range of debt can be found without the economy undergoing boom-bust cycles.