diff --git a/gateway/run.py b/gateway/run.py index 735832744..c42510709 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -2284,6 +2284,29 @@ class GatewayRunner: _hyg_api_key = _hyg_runtime.get("api_key") except Exception: pass + + # Check custom_providers per-model context_length + # (same fallback as run_agent.py lines 1171-1189). + # Must run after runtime resolution so _hyg_base_url is set. + if _hyg_config_context_length is None and _hyg_base_url: + try: + _hyg_custom_providers = _hyg_data.get("custom_providers") + if isinstance(_hyg_custom_providers, list): + for _cp in _hyg_custom_providers: + if not isinstance(_cp, dict): + continue + _cp_url = (_cp.get("base_url") or "").rstrip("/") + if _cp_url and _cp_url == _hyg_base_url.rstrip("/"): + _cp_models = _cp.get("models", {}) + if isinstance(_cp_models, dict): + _cp_model_cfg = _cp_models.get(_hyg_model, {}) + if isinstance(_cp_model_cfg, dict): + _cp_ctx = _cp_model_cfg.get("context_length") + if _cp_ctx is not None: + _hyg_config_context_length = int(_cp_ctx) + break + except (TypeError, ValueError): + pass except Exception: pass