llm_factory: cache parsed llm_config per profile#671
Merged
Conversation
Production logs were full of repeated INFO/WARNING noise on
every Luigi task start:
INFO planexe_config - Optional file '.env' not found …
WARN planexe_llmconfig - Environment variable 'OPENAI_API_KEY' not found.
WARN planexe_llmconfig - Environment variable 'DASHSCOPE_API_KEY' not found.
WARN planexe_llmconfig - Environment variable 'DEEPSEEK_API_KEY' not found.
INFO planexe_llmconfig - Applied PLANEXE_LLM_CONFIG_WHITELISTED_CLASSES=…
…each LLM lookup was re-reading the JSON, re-substituting env
vars, and re-applying the class whitelist.
Cache the parsed `PlanExeLLMConfig` in a module-level dict
keyed by resolved `ModelProfileEnum`. The pricing and
Anthropic-usage-hook side effects only need to run on first
load, so they move into the cache-miss branch — same end state.
Side effect: `get_llm` was mutating `config["arguments"]` in
place when merging caller kwargs. Pre-cache that was harmless
(the dict was rebuilt every call); with caching it would leak
state across calls. Shallow-copy the dict before `update()`.
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
Production logs were full of repeated INFO/WARNING noise at every Luigi task start:
…because every LLM lookup was re-reading the JSON, re-substituting env vars, and re-applying the class whitelist.
Now
_load_llm_configcaches the parsedPlanExeLLMConfigin a module-level dict keyed by resolvedModelProfileEnum. Subsequent calls hit the cache. Pricing and Anthropic usage-hook side effects move into the cache-miss branch — same end state, just no longer redone per call.Latent mutation bug fixed in passing
get_llmwas mutatingconfig["arguments"]in place when merging caller kwargs. Pre-cache that was harmless (the dict was rebuilt every call). With caching it would leak state across calls. Shallow-copy the dict beforeupdate().Not in this PR
The
OPENAI_API_KEY/DASHSCOPE_API_KEY/DEEPSEEK_API_KEYwarnings still fire on the first load even though those entries are dropped by the OpenRouter class whitelist. Fixing that needsfilter_by_whitelisted_classesto run beforesubstitute_env_varsinPlanExeLLMConfig.load. Saved for a separate PR.Test plan
Optional file '.env' not found/Environment variable …_API_KEY not foundlines now appear at most once per profile per worker (instead of per task).get_llm("foo", max_tokens=8192)followed byget_llm("foo")— the second call must NOT seemax_tokens=8192leaked in.🤖 Generated with Claude Code