Chunking as a Segmentation and Optimization Problem
Where to cut a document, posed as a coherence-maximizing segmentation with an exact dynamic-programming optimum — and the proxy it secretly optimizes
Overview & motivation
Every retrieval pipeline begins by cutting documents into chunks, and the standard recipe — split every tokens — is the one step in the stack with no mathematics behind it. Yet the choice matters: a boundary dropped in the middle of an argument splits the evidence a query needs across two chunks, and a chunk that spans two topics dilutes the embedding of both. The question where to cut deserves to be posed precisely, and when it is, it turns out to be a clean optimization problem with an exact solution.
The framing is segmentation. Represent the document as a sequence of sentence embeddings , L2-normalized so they live on the unit sphere, and choose boundaries that maximize within-chunk coherence — keep each chunk’s sentences pointing the same way. We will see that this coherence is exactly the mean resultant length of the chunk, the von Mises–Fisher concentration statistic, so an optimal chunk is a tight cluster on the sphere; that the resulting additive objective has a globally optimal dynamic-programming solution; and that the heuristics in common use — TextTiling and fixed-size splitting — cannot beat it. Then the honest part: the coherence we optimize is a proxy, and optimizing it past the true number of sections makes the chunks more coherent and the segmentation worse.
Before the algebra, drag the chunk-count slider and watch the optimal boundaries find the topic shifts:
The first panel is the document as a colored strip with the adjacent-gap dissimilarity beneath it; at the true count of five sections the dynamic program lands its boundaries exactly on the topic shifts, while fixed-size cuts fall wherever the arithmetic puts them. The second panel is the catch — coherence cost falls forever as you add chunks, but boundary-recovery accuracy peaks at the true count and then declines. The third compares the methods head to head.
What we cover
- Why within-chunk coherence is the mean resultant length, and the closed form it gives.
- The optimal segmentation as a dynamic program, proved optimal.
- TextTiling and fixed-size chunking, and the gap to the optimum.
- The proxy problem: coherence is not retrieval quality.
- A finance case study on a synthetic 10-K filing.
Coherence is the mean resultant length
Fix a candidate chunk — the sentences , written — and measure its incoherence by how far its sentences point from the chunk’s average direction. Let be the unit vector along the sum . The cost is the total angular spread, , which is zero when every sentence aligns with and grows as they disagree.
Proposition 1 (The chunk cost is its length minus its resultant length).
For L2-normalized embeddings, the chunk cost has the closed form
where is the mean resultant length of the chunk — the von Mises–Fisher concentration statistic.
Proof.
Because is the unit vector along the sum, the total alignment is
Subtracting from the chunk length (each of the unit embeddings contributes to ) gives . Dividing the sum by turns the norm into the mean resultant length , so .
∎This is the bridge to the prerequisite topic. There, was the maximum-likelihood estimate of a von Mises–Fisher distribution’s concentration : a tight topical cluster has near , a diffuse one near . Here the same quantity is a chunk’s coherence, and minimizing the total cost over a segmentation is maximizing the total resultant length — carving the document into tight vMF clusters on the sphere. The cost is also cheap: with prefix sums of the embeddings, and hence is computed in time proportional to the embedding dimension, independent of the chunk’s length.
The optimal segmentation is a dynamic program
A segmentation into chunks is a choice of internal boundaries, and its total cost is the sum of the per-chunk costs — an additive objective over contiguous pieces. Additivity is exactly the structure dynamic programming exploits.
Theorem 1 (Optimal segmentation by dynamic programming).
Let be the minimum total cost of segmenting the prefix into contiguous chunks. Then
and the globally optimal -segmentation of the whole document is recovered from in time. With a per-chunk penalty in place of a fixed , the single recurrence runs in .
Proof.
The argument is optimal substructure. Consider any optimal -segmentation of and let its last chunk be . The remaining chunks form a -segmentation of , and it must itself be optimal: if some other -segmentation of had strictly smaller cost, swapping it in — the last chunk is unchanged, so its cost is unchanged — would give a -segmentation of with smaller total cost, contradicting optimality. Hence the optimal cost decomposes as for the true split point , and since we do not know we minimize over all valid , which is the recurrence. Filling the table over and , each entry a minimization over , is ; back-pointers reconstruct the boundaries.
∎The companion harness checks this the only way that settles it: against brute force. For small documents it enumerates every way to place the boundaries, and the dynamic program matches the exhaustive optimum at every — it is the global minimum, not a good guess. That is the dividing line between this and the methods practitioners actually use.
What practitioners use, and the gap
Two heuristics dominate real pipelines, and neither optimizes the objective above. Fixed-size chunking cuts every tokens; it is semantics-free, and on a document whose sections have uneven lengths — every real document — its boundaries land on the topic shifts only by accident. TextTiling (Hearst 1997) is smarter: it slides a window across the document, scores each gap by the dissimilarity of the text before and after, and places boundaries at the gaps of locally greatest dissimilarity. That greedy, local rule often finds the obvious shifts, but it cannot see the global trade-off the dynamic program optimizes, so at the same number of chunks its total coherence is no better — and usually worse — than the optimum.
On the synthetic document in the laboratory, at the true count of five chunks, the dynamic program places its boundaries exactly on the planted topic shifts (boundary ), the greedy heuristic recovers three of four (), and fixed-size chunking, fooled by the uneven section lengths, recovers two () — while the optimum’s coherence cost is strictly the lowest of the three, as the theorem guarantees. The “Boundary recovery” panel shows the gap; it is the value of optimizing globally rather than locally.
Coherence is a proxy
Here is the catch that keeps this honest, and it is visible the moment you keep splitting. The optimal coherence cost is monotone decreasing in the number of chunks — more boundaries can only reduce within-chunk spread, all the way down to singletons of cost zero. So the objective, taken literally, wants to cut the document into individual sentences. What stops it is that coherence is a proxy for the thing we actually care about, which is whether the chunks are good units of retrieval, and that target is not monotone in the chunk count at all.
The “Granularity tradeoff” panel makes the divergence concrete: as grows, the coherence cost slides down forever, but the boundary-recovery rises to a peak at the true number of sections and then falls, because past that point the optimizer is splitting coherent sections into even-more-coherent fragments — lowering the cost while destroying the structure. Minimizing the proxy past the true is actively harmful. This is the segmentation analog of every proxy-objective problem in the field: the dynamic program will give you the exact optimum of what you asked for, and what you asked for is not quite what you wanted. The practical consequence is that the penalty , or the chunk count , is the load-bearing hyperparameter, and it must be set by the downstream retrieval metric, not by the coherence the algorithm optimizes.
Finance case study
Honest caveats
Implementation
The companion module chunking_as_segmentation.py owns every number this page and the laboratory cite. Its six assertions encode the claims in order: the chunk cost equals the sum of one-minus-cosine to the mean direction, hence the mean resultant length (to the decimal); the dynamic program matches an exhaustive brute-force optimum at every ; neither the greedy nor the fixed-size heuristic beats the DP’s objective value; the optimal cost is monotone in the chunk count; the DP recovers planted boundaries better than the baselines on a document with uneven sections; and the finance filing. A trap worth flagging, since it shaped the experiment: with equal-length sections, fixed-size chunking hits the boundaries by accident and looks as good as the optimum, so the documents are deliberately uneven — which is also what makes them realistic. Both the module and the narrative notebook run in well under a second and must exit cleanly before the topic ships.
Connections
- the within-segment coherence this topic minimizes is exactly the mean resultant length developed there — the norm of the average of unit embeddings, the maximum-likelihood concentration estimate of a von Mises-Fisher cluster — so an optimal chunk is a tight vMF cluster on the sphere, and segmentation is the problem of cutting the document where the mean direction shifts hypersphere-vmf-geometry
References & Further Reading
- paper TextTiling: Segmenting Text into Multi-paragraph Subtopic Passages — Hearst (1997) The greedy depth-score segmentation heuristic — the baseline this topic contrasts with the optimal DP
- paper On the Approximation of Curves by Line Segments Using Dynamic Programming — Bellman (1961) The origin of optimal one-dimensional segmentation by dynamic programming
- paper On Grouping for Maximum Homogeneity — Fisher (1958) Exact optimal one-dimensional clustering by DP — contiguity-constrained grouping, the segmentation problem's statistical ancestor
- book Directional Statistics — Mardia & Jupp (2000) The mean resultant length and the von Mises-Fisher distribution — the coherence measure the segment cost reduces to
- paper Selective Review of Offline Change Point Detection Methods — Truong, Oudre & Vayatis (2020) Segmentation as change-point detection — the signal-processing framing and the cost/penalty design space
- paper Statistical Models for Text Segmentation — Beeferman, Berger & Lafferty (1999) Probabilistic text segmentation and the Pk evaluation metric — the measurement side of the problem
- documentation How to Split Text Based on Semantic Similarity (Semantic Chunker) — LangChain (2024) The modern embedding-similarity chunking practice this topic formalizes and contrasts with the global optimum