Skip to content

fix: parse OpenRouter/Nous "in the output" error format in parse_available_output_tokens_from_error() - #38659

Closed
Xeron2000 wants to merge 1 commit into
NousResearch:mainfrom
Xeron2000:fix/openrouter-output-cap-error-parsing
Closed

fix: parse OpenRouter/Nous "in the output" error format in parse_available_output_tokens_from_error()#38659
Xeron2000 wants to merge 1 commit into
NousResearch:mainfrom
Xeron2000:fix/openrouter-output-cap-error-parsing

Conversation

@Xeron2000

Copy link
Copy Markdown

Summary

parse_available_output_tokens_from_error() in agent/model_metadata.py only recognized Anthropic's output-cap error format ("available_tokens: N"). OpenRouter-compatible providers (Nous Research, OpenRouter) use a different format:

5683 of text input, 13410 of tool input, 262000 in the output

The function returned None for these errors because:

  1. The is_output_cap_error guard required literal "max_tokens" — OpenRouter format doesn't contain it
  2. No extraction path existed for computing available = context - text_input - tool_input

Bug impact

On fresh sessions:

  1. User configures max_tokens larger than context_length - input_tokens
  2. API returns 400 → classified as context_overflow
  3. parse_available_output_tokens_from_error() returns None → falls through to input-too-large compression path ❌
  4. Nothing to compress on fresh session → returns "Context length exceeded (4,882 tokens). Cannot compress further."
  5. Gateway triggers Session auto-reset
  6. Next message → same error → infinite loop

User sees repeated "Session auto-reset" and /new has no effect because the actual fix is reducing max_tokens, not clearing history.

Changes

  • Guard: is_output_cap_error now also matches '(\d+) in the output' as an output-cap error indicator
  • Extraction: New regex path computes available_output = context_length - text_input - tool_input from the OpenRouter/Nous error format
  • Tests: Added 7 tests covering Anthropic format, OpenRouter/Nous format, and negative cases

Fixes #38652

…lable_output_tokens_from_error()

OpenRouter-compatible providers (Nous Research, OpenRouter) return
output-cap-too-large errors in a format that was not recognized:

  "... requested about N tokens (A of text input, B of tool input, K in the output)"

Previously, the is_output_cap_error guard required the literal string
"max_tokens" to appear in the error, which the OpenRouter format does not
contain. This caused the function to return None, falling through to the
input-too-large compression path instead of the max_tokens auto-reduction
path. On fresh sessions with nothing to compress, this led to an infinite
Session auto-reset loop.

Changes:
- Guard now also recognizes '(\d+) in the output' as an output-cap error
- New extraction path computes available = context_length - text_input - tool_input
- Added 7 tests for Anthropic format, OpenRouter/Nous format, and negative cases

@tonydwb tonydwb left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review of PR #38659

Title: fix: parse OpenRouter/Nous "in the output" error format in parse_available_output_tokens_from_error()

Looks Good

  • Clean diff (85 additions / 1 deletions across 2 files).

Reviewed by Hermes Agent

@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/openrouter OpenRouter aggregator provider/nous Nous Research API (OAuth) labels Jun 4, 2026
@teknium1

Copy link
Copy Markdown
Contributor

This appears to be implemented on current main by a later merged fix.

Automated hermes-sweeper review evidence:

  • agent/model_metadata.py:1053 now recognizes the OpenRouter/Nous output-cap phrasing via "in the output" plus "maximum context length".
  • agent/model_metadata.py:1086 parses maximum context length is N ... (A of text input, B of tool input, C in the output) and computes available = context - text - tool.
  • tests/test_output_cap_parsing.py:5 covers the OpenRouter/Nous breakdown format and the existing Anthropic available_tokens format.
  • The implementation landed in 1fb99b1f229a700f8ee1a3e90ff1d44f85e963d4 (fix(stream+output-cap): guard empty streams and parse OpenRouter output-cap errors) and is contained in v2026.6.19.

Closing as implemented on main. Thanks for the clear report and PR.

@teknium1 teknium1 closed this Jun 21, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P1 High — major feature broken, no workaround provider/nous Nous Research API (OAuth) provider/openrouter OpenRouter aggregator sweeper:implemented-on-main Sweeper: behavior already present on current main type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: parse_available_output_tokens_from_error() misses OpenRouter/Nous "in the output" format — causes infinite auto-reset loop

4 participants