feat: auto-detect and display context length for custom providers #2513 - #2514
Closed
InB4DevOps wants to merge 1 commit into
Closed
InB4DevOps wants to merge 1 commit into
InB4DevOps wants to merge 1 commit into
Conversation
InB4DevOps
marked this pull request as draft
March 22, 2026 16:07
InB4DevOps
force-pushed
the
feature/context_length
branch
2 times, most recently
from
March 23, 2026 07:19
dfff9c3 to
c87f373
Compare
InB4DevOps
force-pushed
the
feature/context_length
branch
from
March 23, 2026 07:19
c87f373 to
fb2c0db
Compare
InB4DevOps
marked this pull request as ready for review
March 23, 2026 07:28
1 task
Closed
1 task done
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.
What does this PR do?
This PR adds two related improvements to the custom provider workflow:
Automatic context length detection when users save custom providers via
/modelwithout specifying a context length. The system now attempts to auto-detect it usingget_model_context_length()and displays the result with appropriate visual indicators.Enhanced flag parsing for
/modelcommand, supporting--context-length,--base-url, and--api-keyflags with both=and space separators.Improved credential resolution for direct endpoints (custom providers with base_url), requiring explicit
delegation.api_keyinstead of falling back toOPENAI_API_KEYenv var.Better test coverage for the new features and edge cases.
Changes
Core Implementation
hermes_cli/cli.py--context-length,--base-url,--api-key(supports both=and space separators)base_url_flagfirstcontext_lengthtovalidate_requested_model()when saving custom providerscontext_lengthisNoneand model name is providedhermes_cli/main.py_model_flow_custom()for custom providershermes_cli/models.pysuggested_base_urllogic inprobe_api_models()- changed condition fromalternate_base != candidate_basetois_fallbackcontext_lengthparameter tovalidate_requested_model()tools/delegate_tool.py_resolve_delegation_credentials()to require explicitdelegation.api_keyfor direct endpointsOPENROUTER_API_KEYif provider is explicitlyopenrouter, notOPENAI_API_KEYfor custom endpointsdelegation.api_keyin your config. Note:OPENAI_API_KEYenvironment variable is NOT used for direct endpoints"tools/transcription_tools.py_resolve_openai_api_key()to check onlyVOICE_TOOLS_OPENAI_KEY(notOPENAI_API_KEY)_get_provider()that checksOPENAI_API_KEYfor backwards compatibilityTests
tests/test_model_context_length_detection.py(new file)New comprehensive test suite with 127 lines covering:
probe_api_models()behavior with fallback URLsDEFAULT_FALLBACK_CONTEXTconstant validationtests/tools/test_delegate.pytest_direct_endpoint_falls_back_to_openai_api_key_envtotest_direct_endpoint_requires_explicit_api_key_not_openai_envdelegation.api_keyinstead of falling back toOPENAI_API_KEYtests/tools/test_transcription.pytest_no_keywith proper temp file cleanupDocumentation
AGENTS.mdAdded new policy under "Known Pitfalls" documenting:
User Experience
Before:
After:
Or when fallback is used:
Flag Support
Now supports both formats:
Technical Details
Context Length Auto-Detection
get_model_context_length()fromagent.model_metadataDEFAULT_FALLBACK_CONTEXTconstant (128K) for consistencycontext_lengthisNonemodel_nameis provided"custom"Enhanced Flag Parsing
The
/modelcommand now parses flags before extracting the model part, supporting:--context-lengthor--context_length(both with=or space separator)--base-urlor--base_url(both with=or space separator)--api-keyor--api_key(both with=or space separator)Credential Resolution
Direct endpoints (custom providers with base_url) now:
delegation.api_keyfirstOPENROUTER_API_KEYonly if provider is explicitly"openrouter"ValueErrorif no key found, with clear guidance to usedelegation.api_keyTesting
tests/test_model_context_length_detection.py/modelcommand with custom providersRelated
Related Issue
Fixes #2513
Type of Change: ✨ New feature (non-breaking change that adds functionality)