consensus sends no max_tokens — OpenRouter default (131072) exceeds kimi-k2-thinking's actual backend caps (400)

Author: JR91387Created Sep 6, 2026Updated Sep 6, 2026
Labelsbug

Project Version

9.8.2

Bug Description

kimi-k2-thinking has been broken in consensus for days. Same exact failure both times:

max_tokens: 100352 exceeds maximum 98304 ... provider: Novita
previous_errors: [{"message":"Requested maximum tokens of 131072 exceeds the maximum output tokens limit: 102400", provider: Google}]

Claude determined root cause:: tools/consensus.py calls generate_content() and never passes max_output_tokens. In openai_compatible.py:

python

if max_output_tokens and supports_sampling:
    completion_params["max_tokens"] = max_output_tokens

None → key never gets set → the request goes out with no max_tokens at all. So OpenRouter decides for you. It decided 131072. Nobody asked for that, and nobody checked it against what the backend it actually routes to can serve — because there was nothing in the request to check it against.

Here's the part that gets me: the right number is already sitting right there in conf/openrouter_models.json — max_output_tokens: 32768 for this model. It's configured. It's correct. It's just never used, because there's no fallback when the caller skips the parameter.

Proposed Fix:

effective_max_output_tokens = max_output_tokens
if effective_max_output_tokens is None and capabilities is not None and capabilities.max_output_tokens:
    effective_max_output_tokens = capabilities.max_output_tokens

if effective_max_output_tokens and supports_sampling:
    completion_params["max_tokens"] = effective_max_output_tokens

Verified locally — kimi completes through consensus fine once the request actually carries a cap.

This isn't kimi-specific, it just happens to be the model that exposed it. Any OpenRouter reasoning model routed through a tool that skips max_output_tokens inherits whatever OpenRouter feels like defaulting to that day, with zero validation against the routed backend.

Relevant Log Output

bash
── Incident 1: 2026-09-03 17:28:28 ──
Request options: {'method': 'post', 'url': '/chat/completions', ...,
  'json_data': {'messages': [...], 'model': 'moonshotai/kimi-k2-thinking',
                'stream': False, 'temperature': 1.0}}
  # note: no 'max_tokens' key present in the outgoing request

httpx - INFO - HTTP Request: POST https://openrouter.ai/api/v1/chat/completions "HTTP/1.1 400 Bad Request"

httpx.HTTPStatusError: Client error '400 Bad Request' for url 'https://openrouter.ai/api/v1/chat/completions'

root - ERROR - OpenRouter API error for model moonshotai/kimi-k2-thinking after 1 attempt: Error code: 400 -
{'error': {'message': 'Provider returned error', 'code': 400, 'metadata':
  {'raw': '{"message":"max_tokens: 100352 exceeds maximum 98304 trace_id: be6df772cfe9b5d2452f13106825e463","type":"invalid_request_error"}\n',
   'provider_name': 'Novita', 'is_byok': False,
   'previous_errors': [{'code': 400, 'message': 'Provider returned error', 'provider_name': 'Google',
     'raw': '[{"error":{"code":400,"message":"Requested maximum tokens of 131072 exceeds the maximum output tokens limit: 102400.","status":"INVALID_ARGUMENT"}}]'}]}},
 'user_id': 'user_3Al9UemuP3OiE2O1M0JSutntQp6'}

tools.consensus - ERROR - Error consulting model {'model': 'moonshotai/kimi-k2-thinking', 'stance': 'against', ...}

── Incident 2: 2026-09-06 15:57:11 (same session as the report just filed) ──
Request options: {'method': 'post', 'url': '/chat/completions', ...,
  'json_data': {'messages': [...], 'model': 'moonshotai/kimi-k2-thinking',
                'stream': False, 'temperature': 1.0}}
  # again: no 'max_tokens' key present

httpx - INFO - HTTP Request: POST https://openrouter.ai/api/v1/chat/completions "HTTP/1.1 400 Bad Request"

root - ERROR - OpenRouter API error for model moonshotai/kimi-k2-thinking after 1 attempt: Error code: 400 -
{'error': {'message': 'Provider returned error', 'code': 400, 'metadata':
  {'raw': '{"message":"max_tokens: 100352 exceeds maximum 98304 trace_id: 827962eb6154e5c97324a98a52abea3b","type":"invalid_request_error"}\n',
   'provider_name': 'Novita', 'is_byok': False,
   'previous_errors': [{'code': 400, 'message': 'Provider returned error', 'provider_name': 'Google',
     'raw': '[{"error":{"code":400,"message":"Requested maximum tokens of 131072 exceeds the maximum output tokens limit: 102400.","status":"INVALID_ARGUMENT"}}]'}]}},
 'user_id': 'user_3Al9UemuP3OiE2O1M0JSutntQp6'}

tools.consensus - ERROR - Error consulting model {'model': 'moonshotai/kimi-k2-thinking', 'stance': 'against'}

Source: /srv/shared/projects/pal-mcp-server/git_pull/logs/mcp_server.log:600-655 and :9096-9129. Both requests are consensus tool calls to moonshotai/kimi-k2-thinking, three days apart, byte-identical failure signature.

Operating System

Linux

Sanity Checks

  • I have searched the existing issues and this is not a duplicate.
  • I am using GEMINI_API_KEY
  • I am using OPENAI_API_KEY
  • I am using OPENROUTER_API_KEY
  • I am using CUSTOM_API_URL

Source: BeehiveInnovations/pal-mcp-server