{"record":{"id":"24c56afdd398b376","repo":"datawhalechina/hello-agents","slug":"llm-api-key-24c56a","errorCode":null,"errorMessage":"LLM_API_KEY 环境变量未设置","messagePattern":"LLM_API_KEY 环境变量未设置","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"Co-creation-projects/lcyting-StockSage-agent/agents/agent_system.py","lineNumber":247,"sourceCode":"\n    # ---- 健康检查 ----\n\n    def is_ready(self) -> bool:\n        try:\n            self._ensure_llm()\n            return True\n        except Exception:\n            return False\n\n\ndef _create_default_llm() -> HelloAgentsLLM:\n    model = os.getenv(\"LLM_MODEL_ID\")\n    api_key = os.getenv(\"LLM_API_KEY\")\n    base_url = os.getenv(\"LLM_BASE_URL\")\n    provider = os.getenv(\"LLM_PROVIDER\", \"auto\")\n\n    if not api_key:\n        raise RuntimeError(\"LLM_API_KEY 环境变量未设置\")\n\n    try:\n        from app.config import settings\n\n        raw_timeout = int(settings.LLM_TIMEOUT)\n    except Exception:\n        raw_timeout = int(os.getenv(\"LLM_TIMEOUT\", \"60\"))\n    # ReAct 多轮 + 工具调用 + 协调者多 Agent 串联，默认 60s 极易中途超时\n    timeout = max(raw_timeout, 180)\n\n    return HelloAgentsLLM(\n        model=model,\n        api_key=api_key,\n        base_url=base_url,\n        provider=provider,\n        temperature=0.3,\n        max_tokens=8192,\n        timeout=timeout,","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/lcyting-StockSage-agent/agents/agent_system.py#L229-L265","documentation":"agent_system's default-LLM factory raises the same RuntimeError as the other StockSage agents: LLM_API_KEY is absent from the environment. This coordinator/system agent additionally reads LLM_TIMEOUT from app.config settings with an env fallback, but only the missing key is fatal at this point.","triggerScenarios":"Creating the multi-agent system without LLM_API_KEY set and without injecting an llm instance; note this factory swallows unrelated exceptions when importing app.config (broad except), so misconfigured settings silently fall back to env defaults.","commonSituations":"Running the orchestrator in a fresh environment (no .env); subprocess/scheduler launches that don't inherit the interactive shell env; misnamed key in .env (e.g. LLM_KEY).","solutions":["Set LLM_API_KEY in the environment or .env that the launching process actually reads.","Pass a shared llm instance when constructing the agent system to skip env resolution entirely.","For schedulers/cron/systemd, list the env var explicitly in the unit/job definition.","Add a preflight env check at app startup listing all missing LLM_* variables."],"exampleFix":"# before\nllm = _create_default_llm()  # RuntimeError: LLM_API_KEY not set\n\n# after\nmissing = [v for v in (\"LLM_API_KEY\",) if not os.getenv(v)]\nif missing:\n    raise SystemExit(f\"missing env vars: {missing}; source .env first\")\nllm = _create_default_llm()","handlingStrategy":"validation","validationCode":"missing = [v for v in (\"LLM_API_KEY\", \"LLM_BASE_URL\") if not os.getenv(v)]\nif missing:\n    raise SystemExit(f\"missing env vars for agent system: {missing}\")\nsystem = AgentSystem()","typeGuard":"def agent_system_env_ready() -> bool:\n    return bool(os.getenv(\"LLM_API_KEY\"))","tryCatchPattern":"try:\n    llm = _create_default_llm()\nexcept RuntimeError as e:\n    if \"LLM_API_KEY\" in str(e):\n        raise SystemExit(\"set LLM_API_KEY (see .env.example) before starting\") from e\n    raise","preventionTips":["Declare LLM_* env vars in the systemd unit / cron / compose service that launches the system.","Reuse one shared LLM instance across subagents instead of per-agent env lookups.","Fail fast with an aggregated missing-vars list at startup."],"tags":["env-vars","configuration","multi-agent","python"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}