Skip to content

fix: count evaluator feedback and novelty judge calls in the token total - #604

Merged
codelion merged 1 commit into
algorithmicsuperintelligence:mainfrom
kvishalv:fix-token-usage-all-llm-calls
Nov 20, 2026
Merged

codelion merged 1 commit into
algorithmicsuperintelligence:mainfrom
kvishalv:fix-token-usage-all-llm-calls

Conversation

@kvishalv

Copy link
Copy Markdown
Contributor

The run's token total (Total LLM Token Usage (N calls), logged at the end of run_evolution) counts only the code-generation call of each iteration. Two features make their own LLM calls, and neither reaches the total:

  • With evaluator.use_llm_feedback, Evaluator._llm_evaluate asks every feedback model through LLMEnsemble.generate_all_with_context, which never recorded usage.
  • With database.embedding_model set, the novelty judge (ProgramDatabase._llm_judge_novelty) calls novelty_llm inside database.add(), in the main process.

Both are off by default, so a default run is not affected. With LLM feedback on and 2 feedback models, an iteration makes 3 calls and the total counts 1 of them. The N calls in the log also counted iterations, not calls.

Changes:

  • LLMEnsemble sums the usage of every call it makes, including each model in generate_all_with_context. take_usage() returns that sum and starts a new one. last_usage is unchanged.
  • The worker adds the evaluator's feedback usage to the iteration's token_usage, with a calls count.
  • The run loop adds the novelty judge's usage after each database.add() and counts calls instead of iterations. It reads token_usage only when it is a dict.

Tests: test_llm_ensemble.py (sums across feedback models and across single calls, and the two helpers), test_diff_format_validation_worker.py (evaluator usage is added to an iteration), and test_process_parallel.py (a one-iteration run logs 4 calls and the right totals for 3 worker calls and 1 novelty call).

Verified on macOS with Python 3.12.0, on main at 01583a4:

  • python -m unittest tests.test_llm_ensemble tests.test_diff_format_validation_worker tests.test_process_parallel: 47 tests OK.
  • python -m unittest discover -s tests -p "test_*.py": 1,109 tests, 1 failure, 7 skipped. The failure is test_alphaevolve_reverse.test_unexpected_awaitable_result_is_closed_and_rejected ('CORO_CREATED' != 'CORO_CLOSED'). It fails the same way on main at 01583a4 without this change.
  • black --check passes on the 5 changed files.

Only the OpenAI-compatible backend reports usage today, so runs on the Claude Code, Codex app server and Copilot CLI backends still show no tokens. This PR does not change that.

This also gives a correct base for a run-level token budget, which I'd like to propose separately.

Claude Code helped prepare this change. I directed it and ran the checks above.

The run's token total counted only each iteration's code-generation
call. LLMEnsemble.generate_all_with_context (evaluator feedback) never
recorded usage, and the novelty judge's calls inside database.add() were
never added. LLMEnsemble now sums usage over every call (take_usage()),
the worker adds the evaluator's usage to the iteration, and the run loop
adds the novelty judge's usage and counts calls instead of iterations.
@CLAassistant

CLAassistant commented Nov 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codelion
codelion merged commit ac5f8f5 into algorithmicsuperintelligence:main Nov 20, 2026
5 checks passed
will-rice added a commit to will-rice/openevolve that referenced this pull request Nov 21, 2026
Upstream's fixes: a NaN combined_score ranks worst (algorithmicsuperintelligence#603), the evaluator's and
the novelty judge's LLM calls count in the token totals (algorithmicsuperintelligence#604), Mistral API
support (algorithmicsuperintelligence#602), responses cut off at max_tokens are reported (algorithmicsuperintelligence#599), nested
candidate definitions dedent (algorithmicsuperintelligence#601), llm.system_message is no longer dropped
(algorithmicsuperintelligence#598), Gemini seeding without a trailing slash (algorithmicsuperintelligence#600) and an R example fix (algorithmicsuperintelligence#597).

Two conflicts, both where the fork had moved upstream's code:
- llm/ensemble.py: upstream's per-call usage sum (_record_usage, take_usage)
  beside the fork's last_message and last_backend, both kept.
- process_parallel.py: the fork evaluates a child in _evaluate_child (generated
  and injected children alike); upstream's count of the evaluator's own LLM calls
  moves there, after the evaluation, into the result's and the metadata's usage.

The fork's suite fails the same 23 tests before and after the merge (upstream's
own, unrelated to it).

Claude-Session: https://claude.ai/code/session_01RnaESRvvoubZsW6wWrQAPQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants