Conversation
… runs - Capture prompt, completion, and total token counts in OpenAILLM.last_usage and log per-call usage - Propagate last_usage through LLMEnsemble to worker results in ProcessParallelController - Record token usage in Program metadata and ProgramDatabase prompt logs - Log per-iteration token counts and cumulative token usage summary at the end of evolution - Add unit tests for token usage tracking
|
raul ramos seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces end-to-end tracking, propagation, persistence, and logging of LLM token usage (
prompt_tokens,completion_tokens, andtotal_tokens) across OpenEvolve runs. Token metrics are captured directly from OpenAI-compatible API responses, propagated from worker processes back to the main controller, stored in program metadata and prompt logs, and reported both per-iteration and as a cumulative summary at the end of evolution.Key Changes
1. LLM & Ensemble Token Capture
OpenAILLM(openevolve/llm/openai.py):self.last_usageattribute to store the most recent call's token breakdown (prompt_tokens,completion_tokens,total_tokens, andmodel).response.usagein_generate_chat(), with a fallback toprompt_tokens + completion_tokensiftotal_tokensis not explicitly provided.INFO-level logging per API call:LLM Token Usage (<model>): prompt_tokens=..., completion_tokens=..., total_tokens=...(or logs when usage data is omitted by the provider).LLMEnsemble(openevolve/llm/ensemble.py):self.last_usageand updatedgenerate()andgenerate_with_context()to propagatelast_usagefrom whichever underlying model was sampled for the request.2. Worker-to-Controller Propagation & Logging
SerializableResult& Worker (openevolve/process_parallel.py):token_usage: Optional[Dict[str, Any]] = NonetoSerializableResult._run_iteration_worker(), captured_worker_llm_ensemble.last_usageimmediately after LLM generation and attached it to allSerializableResultreturn paths—including failed iterations (e.g.,Noneresponse, diff parsing errors, missingchanges_description, or code length limit violations) so token costs from discarded generations are still accounted for.token_usagein the childProgrammetadata dictionary.ProcessParallelController(openevolve/process_parallel.py):total_prompt_tokens,total_completion_tokens,total_tokens, andtotal_llm_callsacross all completed worker futures.| tokens: <total> (prompt: <prompt>, completion: <completion>)) to both successful iteration completion logs and iteration warning/error logs.📊 Total LLM Token Usage (<N> calls): total_tokens=... (prompt_tokens=..., completion_tokens=...)3. Database & Prompt Log Persistence
ProgramDatabase(openevolve/database.py):log_prompt()to accepttoken_usage: Optional[Dict[str, Any]] = None(and broadened thepromptdict type annotation fromDict[str, str]toDict[str, Any]).token_usagealongside prompt/response entries whenlog_promptsis enabled.4. Unit Tests
tests/test_llm_ensemble.py):TestTokenUsageLogging(IsolatedAsyncioTestCase) to verify thatOpenAILLMaccurately parsesresponse.usage, populateslast_usage, emits the expectedINFOlog, and thatLLMEnsembleforwardslast_usageupon generation.Testing
pytest tests/test_llm_ensemble.py), confirming token extraction, log formatting, and ensemble propagation pass.