Skip to content

feat: auto-detect and display context length for custom providers #2513 - #2514

Closed
InB4DevOps wants to merge 1 commit into
NousResearch:mainfrom
InB4DevOps:feature/context_length
Closed

InB4DevOps wants to merge 1 commit into
NousResearch:mainfrom
InB4DevOps:feature/context_length

Conversation

@InB4DevOps

@InB4DevOps InB4DevOps commented Mar 22, 2026 •

Copy link
Copy Markdown

What does this PR do?

This PR adds two related improvements to the custom provider workflow:

  1. Automatic context length detection when users save custom providers via /model without specifying a context length. The system now attempts to auto-detect it using get_model_context_length() and displays the result with appropriate visual indicators.

  2. Enhanced flag parsing for /model command, supporting --context-length, --base-url, and --api-key flags with both = and space separators.

  3. Improved credential resolution for direct endpoints (custom providers with base_url), requiring explicit delegation.api_key instead of falling back to OPENAI_API_KEY env var.

  4. Better test coverage for the new features and edge cases.

Changes

Core Implementation

hermes_cli/cli.py

  • Added flag parsing for --context-length, --base-url, --api-key (supports both = and space separators)
  • Improved custom endpoint detection by checking base_url_flag first
  • Pass context_length to validate_requested_model() when saving custom providers
  • Auto-detect and display context length when context_length is None and model name is provided

hermes_cli/main.py

  • Added context length auto-detection in _model_flow_custom() for custom providers
  • Displays detected length with 💡 emoji or fallback with 📏 emoji for clear visual feedback

hermes_cli/models.py

  • Fixed suggested_base_url logic in probe_api_models() - changed condition from alternate_base != candidate_base to is_fallback
  • Added context_length parameter to validate_requested_model()

tools/delegate_tool.py

  • Modified _resolve_delegation_credentials() to require explicit delegation.api_key for direct endpoints
  • Changed fallback behavior: only uses OPENROUTER_API_KEY if provider is explicitly openrouter, not OPENAI_API_KEY for custom endpoints
  • Updated error message to clarify: "Set delegation.api_key in your config. Note: OPENAI_API_KEY environment variable is NOT used for direct endpoints"

tools/transcription_tools.py

  • Modified _resolve_openai_api_key() to check only VOICE_TOOLS_OPENAI_KEY (not OPENAI_API_KEY)
  • Added auto-detection fallback in _get_provider() that checks OPENAI_API_KEY for backwards compatibility

Tests

tests/test_model_context_length_detection.py (new file)

New comprehensive test suite with 127 lines covering:

  • Context length auto-detection success case
  • Context length fallback when detection fails
  • probe_api_models() behavior with fallback URLs
  • DEFAULT_FALLBACK_CONTEXT constant validation

tests/tools/test_delegate.py

  • Renamed test from test_direct_endpoint_falls_back_to_openai_api_key_env to test_direct_endpoint_requires_explicit_api_key_not_openai_env
  • Updated behavior: direct endpoints now require explicit delegation.api_key instead of falling back to OPENAI_API_KEY

tests/tools/test_transcription.py

  • Improved test for test_no_key with proper temp file cleanup
  • Added module namespace clearing to ensure clean test state

Documentation

AGENTS.md

Added new policy under "Known Pitfalls" documenting:

  • When context length auto-detection runs
  • What visual indicators users will see (💡 and 📏 emojis)
  • Reference to implementation location

User Experience

Before:

✓ Custom provider saved.

After:

✓ Custom provider saved. 💡 Context length auto-detected: 200,000 tokens

Or when fallback is used:

✓ Custom provider saved. 📏 Context length: Using default of 128,000 tokens

Flag Support

Now supports both formats:

# With = separator
/model openrouter:anthropic/claude-opus-4.6 --base-url=https://example.com/api/v1 --api-key=sk-... --context-length=200k

# With space separator
/model openrouter:anthropic/claude-opus-4.6 --base-url https://example.com/api/v1 --api-key sk-... --context-length 200k

Technical Details

Context Length Auto-Detection

  • Uses existing get_model_context_length() from agent.model_metadata
  • Respects DEFAULT_FALLBACK_CONTEXT constant (128K) for consistency
  • Auto-detection only runs when:
    • context_length is None
    • model_name is provided
    • Provider is set to "custom"

Enhanced Flag Parsing

The /model command now parses flags before extracting the model part, supporting:

  • --context-length or --context_length (both with = or space separator)
  • --base-url or --base_url (both with = or space separator)
  • --api-key or --api_key (both with = or space separator)

Credential Resolution

Direct endpoints (custom providers with base_url) now:

  1. Use configured delegation.api_key first
  2. Fall back to OPENROUTER_API_KEY only if provider is explicitly "openrouter"
  3. Raise ValueError if no key found, with clear guidance to use delegation.api_key

Testing

  • New test suite in tests/test_model_context_length_detection.py
  • Manual testing via /model command with custom providers
  • Updated existing tests for changed behavior in delegation and transcription tools

Related

  • Addresses the need for better user feedback when saving custom providers
  • Aligns with the principle of providing clear visual indicators for system behavior

Related Issue

Fixes #2513


Type of Change: ✨ New feature (non-breaking change that adds functionality)

@InB4DevOps
InB4DevOps marked this pull request as draft March 22, 2026 16:07
@InB4DevOps
InB4DevOps force-pushed the feature/context_length branch 2 times, most recently from dfff9c3 to c87f373 Compare March 23, 2026 07:19
@InB4DevOps
InB4DevOps force-pushed the feature/context_length branch from c87f373 to fb2c0db Compare March 23, 2026 07:19
@InB4DevOps
InB4DevOps marked this pull request as ready for review March 23, 2026 07:28
@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/cli CLI entry point, hermes_cli/, setup wizard area/config Config system, migrations, profiles tool/delegate Subagent delegation labels May 3, 2026
@teknium1 teknium1 closed this May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/cli CLI entry point, hermes_cli/, setup wizard P3 Low — cosmetic, nice to have tool/delegate Subagent delegation type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Custom providers lack context length auto-detection when saved via /model command

3 participants