fix(agent): parse OpenRouter/Nous output-cap errors to prevent infinite reset loop - #38667
Closed
liuhao1024 wants to merge 1 commit into
Closed
fix(agent): parse OpenRouter/Nous output-cap errors to prevent infinite reset loop#38667liuhao1024 wants to merge 1 commit into
liuhao1024 wants to merge 1 commit into
Conversation
…te reset loop The parse_available_output_tokens_from_error() function only recognized Anthropic's 'available_tokens' keyword. OpenRouter/Nous providers return errors with 'N in the output' format, which was misclassified as a prompt-too-long error. This caused infinite session reset loops when max_tokens exceeded the available context window. Added regex-based extraction that parses context_length, text_input, and tool_input components from the OpenRouter error format, then calculates available output tokens as context_length - text_input - tool_input. Fixes NousResearch#38652
Collaborator
2 tasks
Contributor
Author
|
Thanks for the flag @alt-glitch. Comparing the two PRs:
The production code changes are functionally equivalent — both extend |
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?
Adds OpenRouter / Nous Research inference error format support to
parse_available_output_tokens_from_error(). Previously, the function only recognized Anthropic's"available_tokens"keyword. When using OpenRouter-compatible providers, errors with"N in the output"format were misclassified as prompt-too-long errors, causing infinite session reset loops whenmax_tokensexceeded the available context.Related Issue
Fixes #38652
Type of Change
Changes Made
agent/model_metadata.py: Extendedparse_available_output_tokens_from_error()to detect OpenRouter/Nous error format ("N of text input, M of tool input, K in the output") and calculate available output tokens ascontext_length - text_input - tool_input. Refactored detection into two phases: (1) identify error format, (2) extract available tokens.tests/test_ctx_halving_fix.py: Added 4 tests covering the OpenRouter/Nous format: canonical format with all components, format without tool input, format with only text input, and format without context length (returns None).How to Test
pytest tests/test_ctx_halving_fix.py -v— all 31 tests should pass (4 new + 27 existing)pytest tests/agent/test_model_metadata.py -v— all 93 tests should pass (no regressions)max_tokensexceeding the context window and confirm the error recovery loop no longer occursChecklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/ACode Intelligence
parse_available_output_tokens_from_errorinagent/model_metadata.py(callers:conversation_loop.py:2989,anthropic_adapter.py:2119)Nonereturn gracefullyparse_context_limit_from_error()(sibling function, same file),_ephemeral_max_output_tokens(consumer in conversation loop)