We introduce a prototype agent-based model of the macroeconomy, with budgetary constraints at its core. The model is related to a class of constraint satisfaction problems (CSPs), which has been thoroughly investigated in computer science. The CSP paradigm allows us to propose an alternative price-setting mechanism: given agents' preferences and budgets, what set of prices satisfies the maximum number of agents? Such an approach permits the coupling of production and output within the economy to the allowed level of debt in a simplified framework. Within our model, we identify three different regimes upon varying the amount of debt that each agent can accumulate before defaulting. In presence of a very loose constraint on debt, endogenous crises leading to waves of synchronized bankruptcies are present.In the opposite regime of very tight debt constraining, the bankruptcy rate is extremely high and the economy remains structure-less. In an intermediate regime, the economy is stable with very low bankruptcy rate and no aggregate-level crises. This third regime displays a rich phenomenology: the system spontaneously and dynamically self-organizes in a set of cheap and expensive goods (i.e. some kind of ``speciation''), with switches triggered by random fluctuations and feedback loops. Our analysis confirms the central role that debt levels play in the stability of the economy. More generally, our model shows that constraints at the individual scale can generate highly complex patterns at the aggregate level.