Skip to content

Commit 06d4605

Browse files
feat: add Claude Code CLI as LLM backend (#460)
* feat: add Claude Code CLI as LLM backend Adds ClaudeCodeLLM, a new LLM provider that uses the Claude Code CLI (`claude -p`) for generation. This enables OpenEvolve to use Anthropic's Claude models without requiring direct API keys — authentication is handled by the CLI's existing OAuth session. Users can configure it via `provider: "claude_code"` in config.yaml or by injecting `init_claude_code_client` programmatically. Includes a provider registry in LLMEnsemble for automatic backend selection. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add retry logic, max_budget_usd config, example, and docs for Claude Code backend Add retry/retry_delay support to ClaudeCodeLLM mirroring the OpenAI backend pattern. Expose max_budget_usd in LLMModelConfig so it can be set via YAML. Add claude_code_quickstart example and document the Claude Code CLI provider in the README. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b28ac15 commit 06d4605

10 files changed

Lines changed: 1036 additions & 7 deletions

File tree

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ OpenEvolve implements a sophisticated **evolutionary coding pipeline** that goes
202202
<details>
203203
<summary><b>Advanced LLM Integration</b></summary>
204204

205-
- **Universal API**: Works with OpenAI, Google, local models, and proxies
205+
- **Universal API**: Works with OpenAI, Google, Claude Code CLI, local models, and proxies
206206
- **Intelligent Ensembles**: Weighted combinations with sophisticated fallback
207207
- **Test-Time Compute**: Enhanced reasoning through proxy systems (see [OptiLLM setup](#llm-provider-setup))
208208
- **Plugin Ecosystem**: Support for advanced reasoning plugins
@@ -233,7 +233,7 @@ OpenEvolve implements a sophisticated **evolutionary coding pipeline** that goes
233233

234234
### Requirements
235235
- **Python**: 3.10+
236-
- **LLM Access**: Any OpenAI-compatible API
236+
- **LLM Access**: Any OpenAI-compatible API, or [Claude Code CLI](https://docs.anthropic.com/en/docs/claude-code)
237237
- **Optional**: Docker for containerized runs
238238

239239
### Installation Options
@@ -356,6 +356,35 @@ llm:
356356
357357
</details>
358358
359+
<details>
360+
<summary><b>🔮 Claude Code CLI (No API Key)</b></summary>
361+
362+
Use the [Claude Code CLI](https://docs.anthropic.com/en/docs/claude-code) as the LLM backend — no API keys needed, authentication uses the CLI's OAuth session.
363+
364+
```bash
365+
# Install and authenticate
366+
npm install -g @anthropic-ai/claude-code
367+
claude login
368+
```
369+
370+
```yaml
371+
# config.yaml
372+
llm:
373+
provider: "claude_code"
374+
models:
375+
- name: "sonnet"
376+
weight: 0.8
377+
max_tokens: 16000
378+
max_budget_usd: 1.0
379+
- name: "haiku"
380+
weight: 0.2
381+
max_tokens: 8000
382+
```
383+
384+
See the [Claude Code quickstart example](examples/claude_code_quickstart/) for a complete walkthrough.
385+
386+
</details>
387+
359388
## Examples Gallery
360389
361390
<div align="center">
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Claude Code CLI Quickstart
2+
3+
This example shows how to use the [Claude Code CLI](https://docs.anthropic.com/en/docs/claude-code) as the LLM backend for OpenEvolve. No API keys are needed — authentication uses the CLI's existing OAuth session.
4+
5+
## Prerequisites
6+
7+
1. **Install Claude Code CLI:**
8+
```bash
9+
npm install -g @anthropic-ai/claude-code
10+
```
11+
12+
2. **Authenticate:**
13+
```bash
14+
claude login
15+
```
16+
17+
3. **Install OpenEvolve:**
18+
```bash
19+
pip install openevolve
20+
```
21+
22+
## Run
23+
24+
```bash
25+
python openevolve-run.py \
26+
examples/claude_code_quickstart/initial_program.py \
27+
examples/claude_code_quickstart/evaluator.py \
28+
--config examples/claude_code_quickstart/config.yaml \
29+
--iterations 50
30+
```
31+
32+
## How It Works
33+
34+
The `config.yaml` sets `provider: "claude_code"` which routes all LLM calls through the `claude -p` subprocess instead of the OpenAI-compatible API. The CLI handles authentication, model selection, and billing.
35+
36+
### Key Config Options
37+
38+
| Field | Description | Default |
39+
|-------|-------------|---------|
40+
| `provider` | Set to `"claude_code"` to use the CLI backend | `"openai"` |
41+
| `name` | Claude model name (`sonnet`, `haiku`, `opus`) | `"sonnet"` |
42+
| `max_budget_usd` | Per-call spending cap in USD | `1.0` |
43+
| `timeout` | CLI timeout in seconds | `300` |
44+
| `retries` | Number of retry attempts on failure | `3` |
45+
| `retry_delay` | Seconds between retries | `5` |
46+
47+
### Ensemble Example
48+
49+
You can mix Claude models in an ensemble, just like with OpenAI models:
50+
51+
```yaml
52+
llm:
53+
provider: "claude_code"
54+
models:
55+
- name: "sonnet"
56+
weight: 0.8
57+
max_tokens: 16000
58+
- name: "haiku"
59+
weight: 0.2
60+
max_tokens: 8000
61+
```
62+
63+
### Programmatic Usage
64+
65+
You can also inject the Claude Code backend at runtime without modifying config files:
66+
67+
```python
68+
from openevolve.llm.claude_code import init_claude_code_client
69+
70+
for model_cfg in config.llm.models:
71+
model_cfg.init_client = init_claude_code_client
72+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Configuration for function minimization using Claude Code CLI as the LLM backend.
2+
# No API keys needed — authentication uses `claude login` (OAuth session).
3+
#
4+
# Prerequisites:
5+
# 1. Install Claude Code CLI: npm install -g @anthropic-ai/claude-code
6+
# 2. Authenticate: claude login
7+
#
8+
# Run:
9+
# python openevolve-run.py \
10+
# examples/claude_code_quickstart/initial_program.py \
11+
# examples/claude_code_quickstart/evaluator.py \
12+
# --config examples/claude_code_quickstart/config.yaml \
13+
# --iterations 50
14+
15+
max_iterations: 50
16+
checkpoint_interval: 10
17+
18+
llm:
19+
provider: "claude_code"
20+
models:
21+
- name: "sonnet"
22+
weight: 0.8
23+
max_tokens: 16000
24+
timeout: 300
25+
max_budget_usd: 1.0
26+
- name: "haiku"
27+
weight: 0.2
28+
max_tokens: 8000
29+
timeout: 120
30+
max_budget_usd: 0.5
31+
retries: 3
32+
retry_delay: 5
33+
34+
prompt:
35+
system_message: >
36+
You are an expert programmer specializing in optimization algorithms.
37+
Your task is to improve a function minimization algorithm to find the
38+
global minimum of a complex function with many local minima.
39+
The function is f(x, y) = sin(x) * cos(y) + sin(x*y) + (x^2 + y^2)/20.
40+
Focus on improving the search_algorithm function to reliably find the
41+
global minimum, escaping local minima that might trap simple algorithms.
42+
43+
database:
44+
population_size: 50
45+
archive_size: 20
46+
num_islands: 3
47+
elite_selection_ratio: 0.2
48+
exploitation_ratio: 0.7
49+
similarity_threshold: 0.99
50+
51+
evaluator:
52+
timeout: 60
53+
cascade_thresholds: [1.3]
54+
parallel_evaluations: 3
55+
56+
diff_based_evolution: true
57+
max_code_length: 20000

0 commit comments

Comments
 (0)