Because of uncertainty inherent in perception, our immediate observations must be supplemented with contextual information to resolve ambiguities. However, often context too is ambiguous, and thus it should be inferred itself to guide behavior. We developed a novel hierarchical task where participants should infer a higher-level, contextual variable to inform probabilistic inference about a hidden dependent variable at a lower level. By controlling the reliability of the past sensory evidence through sample size, we found that humans estimate the reliability of the context and combine it with current sensory uncertainty to inform their confidence reports. Indeed, behavior closely follows inference by probabilistic message passing between latent variables across hierarchical state representations. Despite the sophistication of our task, commonly reported inferential fallacies, such as sample size insensitivity, are not present, and neither do participants appear to rely on simple heuristics. Our results reveal ubiquitous probabilistic representations of uncertainty at different hierarchical levels and temporal scales of the environment.