Skip to content

fix(gateway): /model command fails on custom providers — api_key not read from config #18681

@liizfq

Description

@liizfq

Bug Description

The /model command in the messaging gateway (Telegram, Discord, Matrix, etc.) fails to switch models on custom providers (e.g. LiteLLM) when the endpoint requires authentication.

Error message:

Note: could not reach this custom endpoint's model listing at http://127.0.0.1:8000/v1/models. Hermes will still save glm-5.1, but the endpoint should expose /models for verification.

Root Cause

In gateway/run.py, the /model command handler reads default, provider, and base_url from the model: section of config.yaml, but does not read api_key. The variable current_api_key stays as an empty string, so probe_api_models() sends unauthenticated requests to the endpoint. Servers that require a bearer token (like LiteLLM) reject the probe with HTTP 401.

Affected code (gateway/run.py, inside the /model command handler):

model_cfg = cfg.get("model", {})
if isinstance(model_cfg, dict):
    current_model = model_cfg.get("default", "")
    current_provider = model_cfg.get("provider", current_provider)
    current_base_url = model_cfg.get("base_url", "")
    # ⬇️ missing: current_api_key = model_cfg.get("api_key", "")

Note: the TUI gateway (tui_gateway/server.py) does not have this bug — it correctly reads api_key from getattr(agent, "api_key", "").

Reproduction

  1. Configure a custom provider with auth:
    model:
      default: glm-5-turbo
      provider: custom
      base_url: http://127.0.0.1:8000/v1
      api_key: <your-key>
  2. Start the messaging gateway
  3. Send /model glm-5.1 via any connected platform
  4. Observe: model switch rejected with "could not reach this custom endpoint's model listing"

Proposed Fix

Add one line after the current_base_url assignment:

                     current_base_url = model_cfg.get("base_url", "")
+                    current_api_key = model_cfg.get("api_key", "")

Environment

  • Hermes Agent v0.12.0+ (2026.04.30)
  • Provider: custom (LiteLLM)
  • Gateway platforms: Matrix (likely affects all platforms)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existsarea/configConfig system, migrations, profilescomp/gatewayGateway runner, session dispatch, deliverytype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions