Skip to content

refactor(llm): promote decorator chain settings from NearAiConfig to top-level LlmConfig - #1749

Merged
serrrfirat merged 3 commits into
nearai:stagingfrom
anthhub:fix-llm-config
Apr 14, 2026
Merged

serrrfirat merged 3 commits into
nearai:stagingfrom
anthhub:fix-llm-config

Conversation

@anthhub

@anthhub anthhub commented Mar 30, 2026

Copy link
Copy Markdown

Summary

Retry, circuit breaker, and response cache settings were nested under NearAiConfig with NEARAI_* env var prefixes, but are applied to all LLM backends via build_provider_chain(). This confused users on non-NearAI backends (Gemini, OpenAI, Bedrock, etc.) who had to set NEARAI_MAX_RETRIES to control retry behavior.

  • Added 6 top-level fields to LlmConfig: max_retries, circuit_breaker_threshold, circuit_breaker_recovery_secs, response_cache_enabled, response_cache_ttl_secs, response_cache_max_entries
  • New LLM_* env vars resolve these fields, with automatic fallback to existing NEARAI_* / bare-name vars for backward compatibility
  • build_provider_chain() now reads from LlmConfig directly instead of config.nearai
  • No breaking changes — existing env vars continue to work

Fixes #1554

Test plan

  • All 48 LLM/config tests pass
  • cargo check (default features)
  • cargo fmt
  • Backward compatible: NEARAI_MAX_RETRIES still works when LLM_MAX_RETRIES is unset

@github-actions github-actions Bot added scope: llm LLM integration size: M 50-199 changed lines risk: low Changes to docs, tests, or low-risk modules contributor: experienced 6-19 merged PRs labels Mar 30, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the LLM configuration by introducing top-level fields for retries, circuit breaker settings, and response caching. It adds environment variable parsing for these new fields with fallbacks to maintain backward compatibility. A review comment correctly identified an inconsistency in an error message regarding the circuit breaker threshold, suggesting it be updated to reflect that non-negative integers are valid.

Comment thread src/config/llm.rs
.transpose()
.map_err(|e| ConfigError::InvalidValue {
key: "LLM_CIRCUIT_BREAKER_THRESHOLD".to_string(),
message: format!("must be a positive integer: {e}"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error message states that LLM_CIRCUIT_BREAKER_THRESHOLD must be a positive integer. However, a value of 0 is a valid u32 and represents a valid configuration (tripping the breaker on the first failure). To be more accurate, this message should indicate a "non-negative integer".

Suggested change
message: format!("must be a positive integer: {e}"),
message: format!("must be a non-negative integer: {e}"),

@zmanian zmanian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review -- REQUEST_CHANGES

The refactoring rationale is sound -- these decorator chain settings apply to all LLM backends, not just NearAI, so LLM_* prefixed env vars make more sense. The backward compatibility approach (LLM_* takes priority, falls back to NEARAI_*) is correct.

Medium

  1. No tests for the new env var parsing paths -- 6 new optional_env("LLM_*") blocks with fallback logic, but zero new tests verifying: LLM_* overrides NEARAI_, fallback works when only NEARAI_ is set, invalid LLM_* values produce correct ConfigError::InvalidValue. This is the core behavior the PR introduces.

  2. Module spec (src/llm/CLAUDE.md) is stale -- Lines 112, 130, 170-171, 204-206 still reference NearAiConfig fields and NEARAI_* env vars as the configuration source. The "Provider Chain Construction" diagram still says NEARAI_CIRCUIT_BREAKER_THRESHOLD.

  3. NearAiConfig fields not deprecated -- The 6 fields still exist on NearAiConfig with no deprecation marker. Future code can accidentally read config.nearai.max_retries instead of config.max_retries. Consider #[deprecated] or a follow-up tracking issue.

  4. Failover settings inconsistency -- failover_cooldown_secs, failover_cooldown_threshold, fallback_model are still read from config.nearai.*. Same rationale applies -- users on non-NearAI backends face the same confusing prefix.

Low

  1. .env.example not updated with the new LLM_* vars.
  2. response_cache_max_entries default is 100 in for_testing() but 1000 everywhere else (pre-existing, but propagated).

Required

  • Add at least one config-level test for the LLM_* override / NEARAI_* fallback behavior
  • Update src/llm/CLAUDE.md to reflect the new env vars

@github-actions github-actions Bot added the scope: docs Documentation label Mar 31, 2026
zmanian
zmanian previously approved these changes Mar 31, 2026

@zmanian zmanian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-Review -- APPROVE

Both requested items addressed in 5d6a74e:

  1. Tests added: 3 new tests covering LLM_* override, NEARAI_* fallback, and invalid value error. Well-structured with lock_env() guards.
  2. Module spec updated: src/llm/CLAUDE.md now documents LLM_* env vars for circuit breaker, retry, and response cache settings, including fallback behavior. Provider chain ASCII diagram updated.

Both branches added new tests at the end of the test module:
- PR: decorator chain env var tests (LLM_MAX_RETRIES, etc.)
- staging: DB > ENV priority tests (builtin_overrides)

Kept both sets of tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@zmanian zmanian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-Review -- APPROVE

The two new commits since my last review (5d6a74e: tests + docs, 5bf240a: merge conflict resolution) address both required items from my previous review:

  1. Tests: 3 new tests added -- llm_max_retries_overrides_nearai, nearai_max_retries_used_as_fallback, llm_max_retries_invalid_value_produces_error. These cover the override, fallback, and error paths for the new env var parsing. Properly guarded with lock_env().

  2. Module spec updated: src/llm/CLAUDE.md now documents LLM_* env vars with fallback behavior for circuit breaker, retry, and response cache settings. Provider chain diagram updated.

  3. Merge commit (5bf240a): Clean conflict resolution keeping both the new decorator chain tests and the staging DB>ENV priority tests.

Minor (non-blocking)

  • src/config/llm.rs:130 -- circuit breaker threshold error message still says "must be a positive integer" but u32 accepts 0. Should be "non-negative integer" for consistency with the other error messages. (Gemini flagged this too.)

  • Medium items from previous review (NearAiConfig deprecation markers, failover settings inconsistency, .env.example, for_testing() cache entries default of 100 vs 1000 elsewhere) remain open but are not blocking for this PR.

@serrrfirat
serrrfirat merged commit 019c048 into nearai:staging Apr 14, 2026
14 checks passed
This was referenced Apr 14, 2026
This was referenced Apr 16, 2026
theredspoon pushed a commit to theredspoon/ironclaw that referenced this pull request Jun 21, 2026
…top-level LlmConfig (nearai#1749)

* refactor(llm): promote decorator chain settings from NearAiConfig to top-level LlmConfig

* review: add env var override/fallback tests and update module spec

---------

Co-authored-by: Firat Sertgoz <f@nuff.tech>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor: experienced 6-19 merged PRs risk: low Changes to docs, tests, or low-risk modules scope: docs Documentation scope: llm LLM integration size: M 50-199 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: promote decorator chain settings from NearAiConfig to top-level LlmConfig

3 participants