Generate and optimize code using LLM-based reflection
GEPA’s optimize_anything API extends beyond prompts to optimize any text artifact, including code. From CUDA kernels to cloud scheduling policies, GEPA can evolve code by reading execution results, compiler errors, and profiling data.
On the 10 problems where multi-task mode performed best, we re-optimized each from scratch in single-task mode:
Multi-task mode consistently outperforms dedicated single-task optimization across all speedup thresholds (1.0x, 1.1x, 1.2x).
Why it works: Optimization patterns discovered for one kernel (e.g., memory coalescing, warp-level operations) transfer to other kernels. The Pareto frontier preserves specialized strategies that excel on different operations.
Discover broadcast routing strategies for multi-cloud data transfer that minimize egress cost.Result: 40.2% cost savings on test set, outperforming expert heuristics and other LLM evolution frameworks.
def evaluate_cloudcast(candidate: str, example: dict) -> tuple[float, dict]: """Simulate data transfer and measure cost.""" transfer_graph = build_transfer_graph(example) exec_globals = {} exec(candidate, exec_globals) routing_fn = exec_globals["route_transfer"] routes = routing_fn(transfer_graph, example["source"], example["destinations"]) total_cost = simulate_transfer(routes, example["pricing"]) baseline_cost = example["baseline_cost"] savings_pct = (baseline_cost - total_cost) / baseline_cost * 100 return savings_pct, { "Cost": f"${total_cost:.2f}", "BaselineCost": f"${baseline_cost:.2f}", "Savings": f"{savings_pct:.1f}%", "RouteTopology": describe_topology(routes), }train_scenarios, test_scenarios = load_cloudcast_dataset()result = oa.optimize_anything( seed_candidate=baseline_dijkstra_routing, evaluator=evaluate_cloudcast, dataset=train_scenarios, valset=test_scenarios, objective="Minimize multi-cloud data transfer cost.", background=""" The candidate is a Python function that takes a transfer graph and returns a routing strategy. Consider: - Provider-specific egress pricing - Network topology constraints - Steiner tree vs shortest-path tradeoffs """,)
Evolution: From baseline Dijkstra routing → provider-aware Steiner tree algorithm with Pareto-frontier candidate selection.
Learn scheduling policies that decide when to use cheap SPOT instances vs reliable ON_DEMAND instances.Result: 7.8% cost savings while meeting all deadlines.
Mode: Single-Task SearchPack n=26 circles to maximize the sum of their radii within a unit square.Result: Score of 2.636+, outperforming AlphaEvolve, ShinkaEvolve, and OpenEvolve.
def evaluate_circle_packing(candidate: str) -> tuple[float, dict]: """Execute packing algorithm and return sum of radii.""" exec_globals = {"n": 26} exec(candidate, exec_globals) packing_fn = exec_globals["pack_circles"] circles = packing_fn(n=26) # Validate constraints violations = check_constraints(circles) if violations: return 0.0, {"Error": violations} total_radius = sum(c.radius for c in circles) return total_radius, { "TotalRadius": f"{total_radius:.5f}", "NumCircles": len(circles), "Packing": visualize_packing(circles), # ASCII art or image "MinSpacing": f"{compute_min_spacing(circles):.5f}", }result = oa.optimize_anything( seed_candidate=naive_packing_code, evaluator=evaluate_circle_packing, objective="Maximize sum of circle radii while fitting in unit square.",)
ASI is the text-optimization analogue of the gradient. Where gradients tell a numerical optimizer which direction to move, ASI tells an LLM proposer why a candidate failed and how to fix it.