Skip to main content
The context builder turns program state into LLM prompts. It’s called once per iteration and returns {"system": ..., "user": ...}. For most tasks, you don’t need to touch this. Just set the system prompt in config.yaml:
config.yaml
Only write a custom builder if your algorithm has search-state data (tree path, island ID, rejection history) that the LLM should see.

Architecture

1

Controller calls context_builder.build_prompt()

Once per iteration, before calling the LLM
2

Context builder renders templates

Fills in placeholders with parent program, context programs, and metrics
3

Returns {system, user} dict

Sent directly to the LLM

Directory Structure

Each builder owns its own TemplateManager. Later directories override earlier ones on filename conflicts.

Default Templates


When to Write a Custom Builder

You need a custom builder if:
Examples:
  • Multi-island search: “You are on island 3 (exploration mode)”
  • Tree search: “Current path: root → node_5 → node_12”
  • Acceptance gating: “Last 5 attempts were rejected because…”
Examples:
  • Stagnation response: “No improvement in 20 iterations. Try a radical change.”
  • Evaluator feedback: “Previous solution failed validation: timeout”
  • Paradigm hints: “Consider using dynamic programming instead of greedy.”
Examples:
  • Ranked context: “Top 3 programs: #1 (score=0.95), #2 (score=0.92), …”
  • Sibling context: “3 siblings from the same parent, all failed”
  • Diverse context: “Programs from 3 different search islands”

Writing a Custom Builder

The most common pattern is extending DefaultContextBuilder and injecting extra guidance via the {search_guidance} placeholder. The default templates already include this slot—an empty string makes it disappear cleanly.

Basic Pattern

Here’s a builder that tells the LLM which island it’s on:
my_algo/builder.py

Template with Placeholder

Create my_algo/templates/diff_user_message.txt (overrides default):
diff_user_message.txt
Metrics:

CONTEXT PROGRAMS

TASK

Generate SEARCH/REPLACE blocks to improve the program.
The _prompt_context dict is automatically passed to build_prompt() as the context parameter.

Registration via Config

To make a builder available via config (instead of hardcoding in controller), add it to _init_context_builder() in search/default_discovery_controller.py:
default_discovery_controller.py
Then activate with:
config.yaml

Advanced: Dynamic Guidance

For guidance that changes based on search progress:

Template Variables

Available placeholders in templates:

Real-World Examples

AdaEvolve Builder

Adds evaluator feedback, paradigm guidance, and sibling context:
adaevolve/builder.py

EvoX Builder

Adds LLM-generated search strategy summaries:
evox/builder.py

TemplateManager API

The TemplateManager loads and renders .txt templates:

Next Steps

Custom Algorithms

Implement your own search strategies

Custom Benchmarks

Add your own optimization tasks