TL;DR
Researchers have developed a way for Large Language Models (LLMs) to perform an internal "search" for code solutions by executing intermediate lines of code and feeding the results back into the prompt. This "Within-Prompt Search" (WPS) allows the model to act as its own critic, implicitly backtracking from errors and exploring multiple logic paths simultaneously without needing a separate value network.
The Problem: Auto-regressive Generation Lacks Search
Programming-by-Example (PBE)—generating code that satisfies a set of input-output pairs—is a classic challenge in computer science. While LLMs are surprisingly good at writing code, they suffer from two major flaws in a PBE context:
- Lack of Search: LLMs are auto-regressive; they predict the next token based on what they've already written. If they start down a "dead-end" logic path, they cannot easily hit "undo" and try a different strategy.
- Syntactic Overfitting: LLMs often focus on what the code looks like (syntax) rather than what it does (semantics). If a common pattern in their training data almost fits the examples but fails on a subtle edge case, the model often fails to realize the error because it never "runs" the code in its head.
Traditional PBE synthesizers use complex search algorithms and Domain Specific Languages (DSLs), but they lack the general-purpose flexibility of an LLM. This paper bridges that gap by embedding a search mechanism directly into the LLM's prompt.
Methods: Execution-Guided Within-Prompt Search
The authors introduce Execution-Guided Within-Prompt Search (WPS).
The process works in iterative cycles:
- The LLM as a Policy: The model is prompted to generate several candidate "next lines" of Python code.
- Execution & Annotation: Each candidate line is executed against the provided examples. The resulting variable values are then appended to the code as comments (e.g.,
v1 = a.split('-') # {'e1': ['Part1', 'Part2']}). - The LLM as a Value Function: These annotated lines are bundled into a single, expanding prompt. In the next iteration, the LLM sees the results of all previous attempts. It "implicitly" evaluates which variables are moving closer to the goal and chooses to build upon the most promising ones.
- Canonicalization: To keep the prompt clean, the system de-duplicates lines and simplifies the code using Abstract Syntax Tree (AST) transformations.
This allows the model to perform a "bottom-up" search where it can explore different branches of logic within the same conversation window.
Key Insights and Ideas
- Implicit Backtracking: One of the most striking findings is that the model doesn't need an explicit "delete" command. If it generates a useless line, it simply ignores that variable in the next iteration and tries a different approach from an earlier, more successful variable.
- Parallel Exploration: Because the prompt contains multiple candidate lines, the model can effectively "parallelize" its search, generating several useful intermediate components in one go and then combining them later.
- Execution is the Compass: The researchers found that search without execution results doesn't actually improve performance. The model needs the semantic feedback of the execution results to distinguish between a "good-looking" line and a "useful" line.
- Token Efficiency: Compared to "Tree-of-Thought" (ToT) methods—which require multiple separate API calls and explicit "value prompts" to rank ideas—WPS is much more token-efficient at lower budgets because the "evaluation" happens naturally during the next generation step.
Key Results
The approach was evaluated on five benchmarks covering diverse programming domains: PROSE (string transformations), SyGuS (competitive synthesis), Playgol (inductive logic), Lists (list manipulations), and MBPP (general Python tasks).
- Top-Tier Performance: WPS achieved the best or second-best scores on all five benchmarks when compared to baselines like standard Chain-of-Thought (CoT) or Self-Debug.
- Success in 3/5: Specifically, WPS outperformed all other baselines on the PROSE, Playgol, and MBPP datasets.
- Backtracking Frequency: The authors observed that the model implicitly backtracked in about 10% of successful cases, proving that the search mechanism was actively helping it recover from initial mistakes.
- Scaling: While WPS is highly effective for low-budget scenarios, the authors noted that "Out-of-Prompt" search (like Tree-of-Thought) scales better if you have a massive token budget, as it can explore much deeper trees than a single prompt window allows.
Limitations and Future Work
- Straight-Line Code Only: Currently, the implementation is limited to "straight-line" Python (no loops or complex if-else blocks). Expanding this to full control flow is a major next step.
- Model Sensitivity: The technique requires a highly capable model. Smaller or older models often fail to follow the strict line-by-line formatting or struggle to reason about the execution comments.
- Context Window Limits: As the search expands, the prompt grows. For very complex problems, the model might eventually run out of context window memory.
- Future Directions: The authors suggest fine-tuning smaller models specifically to act as the "policy" for this type of search, potentially bringing these advanced reasoning capabilities to more efficient, local models.
Summary updated