The ability to flexibly learn the structure of one’s surroundings (structure learning) is crucial foradaptive behaviour. Use of an inaccurate model of the environment can lead to incorrect inferences,and thus maladaptive actions. Despite this, relatively little is understood about how structure learningoccurs in human cognition. As a first step towards addressing this, we built on existing approaches tocreate an online clustering algorithm, in which we included a working memory component, allowingbelief update about past stimuli (retrospective inference) in contrast with the more widespread fullyonline approach. We used this model to simulate behaviour on a novel structure learning task, whereoptimal performance required estimating the number and properties of discrete clusters of continuousstimuli. In this work we show how our algorithm outperforms a parametric one (i.e. with fixednumber of clusters). We further demonstrate how retrospective inference benefits structure learning,with performance increasing with working memory capacity. We finally discuss trial-by trial measuresthat can be derived from our model, which provide testable predictions for future empirical studies.