Chapter 5

State-Space Prompt Optimization

Model prompt optimization as a classical AI graph search problem.

The Prompt Space as a Graph

This approach models prompts as nodes in a graph where:

  • States: Individual prompt strings
  • Edges: Transformation operations (make concise, add examples, etc.)
  • Heuristic: Performance score on a dev set
  • Goal: Find the prompt with maximum performance

PromptNode Structure

@dataclass
class PromptNode:
    prompt_text: str
    parent: Optional['PromptNode'] = None
    operator_used: Optional[str] = None
    score: Optional[float] = None

    def get_path(self) -> List[str]:
        """Get the sequence of operators used to reach this node."""
        path = []
        node = self
        while node.parent is not None:
            path.append(node.operator_used)
            node = node.parent
        return list(reversed(path))