@@ -39,7 +39,7 @@ raises, everything is rolled back - the workspace is untouched.
3939
4040## Agent patterns
4141
42- BranchContext ships with five high-level patterns that cover the most common
42+ BranchContext ships with six high-level patterns that cover the most common
4343agent workflows. Each is a callable class: instantiate with config, call with
4444a workspace.
4545
@@ -142,6 +142,42 @@ outcome = TreeOfThoughts(
142142)(ws)
143143```
144144
145+ ### Beam Search
146+
147+ Keep the top-K branches alive at each depth level instead of just one
148+ winner. Interpolates between BestOfN (all parallel, one level) and
149+ TreeOfThoughts (one winner per level). At each level, all candidates
150+ across all beams are scored globally and only the top-K survive.
151+
152+ Inspired by [ EnCompass] ( https://arxiv.org/abs/2512.03571 ) , which showed
153+ that multi-level beam search outperforms both BestOfN and single-winner
154+ tree search for hierarchical agent tasks.
155+
156+ Use when the problem has hierarchical structure * and* you want to hedge
157+ across multiple promising directions: multi-step code migrations where
158+ several rewrite strategies look viable at each stage, planning tasks where
159+ pruning to one path too early loses good alternatives, or any setting where
160+ TreeOfThoughts' single-winner-per-level is too aggressive.
161+
162+ ``` python
163+ from branching import BeamSearch
164+
165+ def strategy_a (path : Path) -> tuple[bool , float ]:
166+ apply_approach_a(path)
167+ return run_tests(path), evaluate_quality(path)
168+
169+ def strategy_b (path : Path) -> tuple[bool , float ]:
170+ apply_approach_b(path)
171+ return run_tests(path), evaluate_quality(path)
172+
173+ outcome = BeamSearch(
174+ [strategy_a, strategy_b, strategy_c, strategy_d],
175+ expand = lambda path , depth : generate_refinements(path),
176+ beam_width = 2 ,
177+ max_depth = 3 ,
178+ )(ws)
179+ ```
180+
145181### Tournament (pairwise elimination)
146182
147183Generate N candidates in parallel, then narrow to one through pairwise
@@ -313,6 +349,7 @@ All patterns: instantiate with config, call with a `Workspace`, get a
313349| ** ` BestOfN ` ** | ` (task, n=3, *, timeout=None) ` | Run N copies; commit highest-scoring success |
314350| ** ` Reflexion ` ** | ` (task, max_retries=3, *, critique=None) ` | Retry with critique feedback loop |
315351| ** ` TreeOfThoughts ` ** | ` (strategies, *, evaluate=None, expand=None, max_depth=1, timeout=None) ` | Parallel strategy tree with optional depth expansion |
352+ | ** ` BeamSearch ` ** | ` (strategies, *, expand, evaluate=None, beam_width=3, max_depth=2, timeout=None) ` | Multi-level beam search; top-K branches survive each depth |
316353| ** ` Tournament ` ** | ` (task, n=4, *, judge, timeout=None) ` | Generate N candidates; pairwise elimination picks winner |
317354
318355### Result types
0 commit comments