byterover-cli icon indicating copy to clipboard operation
byterover-cli copied to clipboard

OpenRouter API returns 400 provider error

Open ngduyanhece opened this issue 1 year ago • 1 comments

Priority: High

Description: OpenRouter API call fails with a 400 error from the provider.

Error Message: ERROR: Error in OpenRouter API call (Attempt 1/3): 400 Provider returned error

Root Cause: The OpenRouter service is likely receiving an invalid request format, possibly due to:

  1. Invalid function schema (same issue as OpenAI)
  2. Unsupported parameters for the selected model
  3. Authentication or configuration issues

Location: src/core/brain/llm/services/openrouter.ts

Investigation Required:

  1. Check if the same schema validation issue affects OpenRouter
  2. Verify OpenRouter API key configuration
  3. Confirm the selected model supports function calling
  4. Review request payload formatting

ngduyanhece avatar Jul 11 '25 11:07 ngduyanhece

OpenRouter 402 Error - Token Limit Issue

This issue likely comes from token limits rather than a bug in the code. For example, I ran a test myself using a free API key from OpenRouter and also got this error:

10:07:06 ERROR: Error in OpenRouter API call (Attempt 1/3): 402 This request requires more credits, or fewer max_tokens. You requested up to 65536 tokens, but can only afford 3934.
10:07:08 ERROR: Error in OpenRouter API call (Attempt 2/3): 402 This request requires more credits, or fewer max_tokens. You requested up to 65536 tokens, but can only afford 3934.
10:07:10 ERROR: Error in OpenRouter API call (Attempt 3/3): 402 This request requires more credits, or fewer max_tokens. You requested up to 65536 tokens, but can only afford 3934.
10:07:10 ERROR: Failed to get response from OpenRouter after 3 attempts.

Analysis:

  • Retry logic working (3 attempts made)
  • Consistent 402 error (credit limit, not code bug)
  • 65,536 tokens requested vs 3,934 affordable
  • Missing max_tokens parameter in API call

Fix: The best fix is to limit the tokens according to the free plan from OpenRouter (about 3000)

// src/core/brain/llm/services/openrouter.ts
max_tokens: 3000, // Add this parameter

RyanNg1403 avatar Jul 13 '25 16:07 RyanNg1403