Abstract : Using the theory of negative association for measures and the notion of random weak limits of sparse graphs, we establish the validity of the cavity method for counting spanning subgraphs subject to local constraints in asymptotically tree-like graphs. Specifically, the corresponding free entropy density is shown to converge along any sequence of graphs whose random weak limit is a tree, and the limit is directly expressed in terms of the unique solution to a limiting cavity equation. On a Galton-Watson tree, the latter simplifies into a recursive distributional equation which can be solved explicitely. As an illustration, we provide an explicit-limit formula for the $b-$matching number of an Erd\H{o}s-Rényi random graph with fixed average degree and diverging size, for any $b\in\mathbb N$.