Fosowl/agenticSeek · error · Exception

OpenRouter API error: {str(e)}

Error message

OpenRouter API error: {str(e)}

What it means

The catch-all in openrouter_fn: any exception raised inside the try block (network error from the OpenAI SDK, the empty-response error, HTTP 4xx/5xx) is re-wrapped as 'OpenRouter API error: <details>' with the original exception chained via 'from e'. This is the standard wrapper you'll see for all OpenRouter call failures.

Source

Thrown at sources/llm_provider.py:439

        """
        client = OpenAI(api_key=self.api_key, base_url="https://openrouter.ai/api/v1")
        if self.is_local:
            # This case should ideally not be reached if unsafe_providers is set correctly
            # and is_local is False in config for openrouter
            raise Exception("OpenRouter is not available for local use. Change config.ini")
        try:
            response = client.chat.completions.create(
                model=self.model,
                messages=history,
            )
            if response is None:
                raise Exception("OpenRouter response is empty.")
            thought = response.choices[0].message.content
            if verbose:
                print(thought)
            return thought
        except Exception as e:
            raise Exception(f"OpenRouter API error: {str(e)}") from e

    def minimax_fn(self, history, verbose=False):
        """
        Use MiniMax API to generate text via OpenAI-compatible interface.

        Supported models:
        - MiniMax-M3: Latest flagship model with enhanced reasoning and coding (default)
        - MiniMax-M2.7: Previous flagship, kept for compatibility
        - MiniMax-M2.7-highspeed: High-speed version of M2.7 for low-latency scenarios

        Note: temperature must be in range (0.0, 1.0], default is 1.0
        """
        load_dotenv()
        base_url = os.getenv("MINIMAX_BASE_URL", "https://api.minimax.io/v1")

        client = OpenAI(api_key=self.api_key, base_url=base_url)
        if self.is_local:
            raise Exception("MiniMax is not available for local use. Change config.ini")

View on GitHub (pinned to ae57a23577)

Solutions

  1. Read the chained cause (__cause__) for the precise HTTP status and message
  2. Verify the API key is set and valid for openrouter.ai
  3. Confirm the model slug in config.ini is valid and you have credits
  4. Check network connectivity/proxy settings for reaching openrouter.ai
  5. Back off and retry on 429 rate-limit errors

Example fix

// before
client = OpenAI(api_key=None, base_url="https://openrouter.ai/api/v1")
// after
client = OpenAI(api_key=os.getenv("OPENROUTER_API_KEY"), base_url="https://openrouter.ai/api/v1")
Defensive patterns

Strategy: try-catch

Validate before calling

import os, requests
def openrouter_ok():
    key = os.getenv('OPENROUTER_API_KEY')
    if not key:
        return False
    r = requests.get('https://openrouter.ai/api/v1/auth/key', headers={'Authorization': f'Bearer {key}'}, timeout=10)
    return r.ok

Try / catch

try:
    out = provider.openrouter_fn(history)
except Exception as e:
    cause = e.__cause__
    status = getattr(cause, 'status_code', None)
    if status == 401:
        log.error('OpenRouter auth failed: check API key')
    elif status == 429:
        time.sleep(30); retry()
    else:
        raise

Prevention

When it happens

Trigger: Any failure in openrouter_fn's try block: invalid API key (401), rate limits (429), unknown model, server 5xx, connection failures to openrouter.ai, or the internal 'OpenRouter response is empty.' raise.

Common situations: OPENROUTER/OPENAI_API_KEY not set or revoked; no internet access to openrouter.ai; invalid model slug; exceeding rate limits on free models; key lacking credit balance.

Related errors


AI-assisted analysis of Fosowl/agenticSeek@ae57a23577 (2026-08-30). Data as JSON: /api/errors/84134f6fa9354a21. Report an issue: GitHub.