[Bug]: an unset API key surfaces as "Connection error" after up to 5 minutes of retries

Author: sxh313Created Sep 28, 2026Updated Sep 28, 2026

What happened?

An unset API key is not reported as a configuration problem. The client is constructed happily, then the first request dies inside httpx while building the Authorization header, and what the user reads is Connection error. — so a missing key looks like a proxy, VPN or firewall problem, and it looks that way after the retry loop has slept for up to five minutes.

Measured on main @ e839e55, Python 3.12.13, openai==1.86.0, with a real OpenRouterClient and api_key="" (which is exactly what an unset OPENROUTER_API_KEY resolves to — see below):

client constructed without complaint: OpenRouterClient
failed after 56.8s with openai.APIConnectionError
user-visible message: Connection error.
underlying cause    : httpx.LocalProtocolError: Illegal header value b'Bearer '

56.8s is with max_retries=2. trae_config.yaml.example:34 and :43 both ship max_retries: 10, and retry_utils.py:44-50 sleeps a random 3-30 s between attempts, so the shipped configuration turns one typo-free mistake into 30-300 s of waiting, each round logging OpenRouter API call failed: Connection error. Will sleep for 24 seconds and will retry. plus a full httpx traceback (retry_utils.py:45-47).

Three things make this worth a fail-fast rather than a better message:

  • The request never leaves the process. I pointed the SDK at a local HTTP server that answers /v1/chat/completions and asked it twice:

    api_key=''  : openai.APIConnectionError: Connection error.
    api_key='x' : OK -> 'ok'
    requests the server actually saw: ['Bearer x']

    So an empty key is not a key for any OpenAI-compatible provider — not even a local, no-auth one, which is what ollama_client.py:33-34 assumes when its comment says "by default ollama doesn't require any api key". Nothing rejects a configuration that could otherwise work.

  • The config layer resolves the empty string silently. ModelConfig.resolve_config_values (trae_agent/utils/config.py:97-113) builds OPENROUTER_API_KEY as the env-var name and then does if resolved_api_key: self.model_provider.api_key = str(resolved_api_key) — a falsy resolution (no env var, api_key: "" in the YAML) leaves the empty string in place and proceeds. Meanwhile :88 raises ConfigError("To register a new model provider, an api_key should be provided") — so the check exists on the registration branch and not on the normal one.

  • No client validates the key. BaseLLMClient.__init__ (base_client.py:17) just stores model_config.model_provider.api_key, and none of the seven clients check it before handing it to an SDK. Construction-time validation is already this package's style for the neighbouring field — azure_client.py:22 raises ValueError("base_url is required for AzureClient") — it is only the key that is let through empty. Google fails anyway, by accident of its SDK: genai.Client(api_key='') raises ValueError: Missing key inputs argument! To use the Google AI API, provide (api_key) arguments. That one case is even codified in tests/utils/test_google_client.py::test_google_client_init_no_key_raises_error, which asserts that constructing without a key raises — so the intent exists for one provider and not for the other six.

What did you expect to happen?

Constructing a client whose provider has no key should raise a ConfigError that names the value to set — the environment variable (OPENROUTER_API_KEY), the --api-key / -k flag (trae_agent/cli.py:132), or the api_key field of that provider in the config file — before any SDK client exists and before the retry loop can start. "Connection error." is the wrong diagnosis to hand a user for a config file they left half-filled, and the repo already has both the exception type (config.py:12) and the precedent (config.py:88) for saying it properly.

Traceback

Traceback (most recent call last):
  File "...\openai\_base_client.py", line 1004, in request
    raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

(the httpx.LocalProtocolError: Illegal header value b'Bearer ' cause is visible only in the printed traceback the retry decorator emits, retry_utils.py:47)

What is your system, Python, dependency version?

  • OS: Windows 11 (Windows-11-10.0.26200-SP0)
  • Python: 3.12.13 (repo venv from uv sync --all-extras); openai==1.86.0, anthropic==0.54.0, google-genai==1.24.0
  • Commit: e839e55 (main)

Host-independent: it is the SDK's header construction refusing an empty value, not a locale, path or line-ending artifact.

Additional information that you believe is relevant to this bug

Why CI does not see any of this: Makefile:42 (make uv-test, which .github/workflows/unit-test.yml runs) sets SKIP_OLLAMA_TEST=true SKIP_OPENROUTER_TEST=true SKIP_GOOGLE_TEST=true, and each of those three files is decorated with a module-level skipIf, so 17 tests are skipped on every run — including the only one that asserts the no-key contract, test_google_client_init_no_key_raises_error. The empty-key path is therefore unexercised in CI in both directions.

I plan to fix it in BaseLLMClient.__init__ (one place, covers all seven providers rather than seven copies of the same check), with the message following the existing <PROVIDER>_API_KEY convention from config.py:97. Two scope notes I want to agree on before I write it:

  • The shipped examples keep working: trae_config.yaml.example / trae_config.json.example use non-empty placeholders (your_openai_api_key, and "ollama" for Ollama), so nothing documented newly fails. A user who edits an example to api_key: "" today gets a misleading connection error; that is the case being fixed.
  • I am deliberately not touching the retry loop here. retry_utils.py:37 catches bare Exception, so a permanent AuthenticationError (bad key) is retried exactly like a transient timeout — real, but a different change with its own blast radius, and I would rather file it separately than widen this one.