Chemotaxis, in which cells steer using chemical gradients, drives fundamental biological processes like embryogenesis, metastasis and immune responses. Self-generated chemotaxis, where cells break down abundant attractants to create gradients, is an important but under-studied aspect of physiological navigation. Here we show that self-generated gradients allow cells to navigate arbitrarily complex paths and, remarkably, make accurate choices about pathways they have not yet encountered. This enables cells to solve microfluidic mazes, even with initially homogeneous environments and distant correct destinations. We combine computational models and experiments to understand how cells anticipate environmental features, and how decision accuracy is determined by path complexity, attractant diffusibility and cell speed. This permits mazes that are easy or hard for cells to resolve, despite similar appearances. Counterintuitively, slowly diffusing attractants can generate a "mirage", making cells prefer dead ends over correct paths. In vivo environments resemble complex mazes, and only self-generated gradients realistically explain cell behaviour.