{"record":{"id":"87b5c574de9ce927","repo":"666ghj/MiroFish","slug":"api-key-env-llm-api-key-87b5c5","errorCode":null,"errorMessage":"缺少 API Key 配置，请在项目根目录 .env 文件中设置 LLM_API_KEY","messagePattern":"缺少 API Key 配置，请在项目根目录 \\.env 文件中设置 LLM_API_KEY","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"backend/scripts/run_reddit_simulation.py","lineNumber":457,"sourceCode":"        - LLM_API_KEY: API密钥\n        - LLM_BASE_URL: API基础URL\n        - LLM_MODEL_NAME: 模型名称\n        \"\"\"\n        # 优先从 .env 读取配置\n        llm_api_key = os.environ.get(\"LLM_API_KEY\", \"\")\n        llm_base_url = os.environ.get(\"LLM_BASE_URL\", \"\")\n        llm_model = os.environ.get(\"LLM_MODEL_NAME\", \"\")\n        \n        # 如果 .env 中没有，则使用 config 作为备用\n        if not llm_model:\n            llm_model = self.config.get(\"llm_model\", \"gpt-4o-mini\")\n        \n        # 设置 camel-ai 所需的环境变量\n        if llm_api_key:\n            os.environ[\"OPENAI_API_KEY\"] = llm_api_key\n        \n        if not os.environ.get(\"OPENAI_API_KEY\"):\n            raise ValueError(\"缺少 API Key 配置，请在项目根目录 .env 文件中设置 LLM_API_KEY\")\n        \n        if llm_base_url:\n            os.environ[\"OPENAI_API_BASE_URL\"] = llm_base_url\n        \n        print(f\"LLM配置: model={llm_model}, base_url={llm_base_url[:40] if llm_base_url else '默认'}...\")\n        \n        return ModelFactory.create(\n            model_platform=ModelPlatformType.OPENAI,\n            model_type=llm_model,\n        )\n    \n    def _get_active_agents_for_round(\n        self, \n        env, \n        current_hour: int,\n        round_num: int\n    ) -> List:\n        \"\"\"","sourceCodeStart":439,"sourceCodeEnd":475,"githubUrl":"https://github.com/666ghj/MiroFish/blob/b5b53acc57189a4a42e44a23e149dc655c98fe82/backend/scripts/run_reddit_simulation.py#L439-L475","documentation":"Identical guard in backend/scripts/run_reddit_simulation.py: the Reddit simulation copies LLM_API_KEY into OPENAI_API_KEY for camel-ai's OpenAI backend and raises this ValueError when os.environ.get(\"OPENAI_API_KEY\") is still falsy after the copy. It fires before ModelFactory.create, so no simulation round starts without credentials.","triggerScenarios":"Launching run_reddit_simulation.py in a process where LLM_API_KEY is unset/empty and OPENAI_API_KEY was never exported.","commonSituations":"Missing or misnamed .env entries; running under systemd/cron where the environment is stripped; a copied config that sets LLM_BASE_URL and LLM_MODEL_NAME but forgets the key.","solutions":["Set LLM_API_KEY in .env or the process environment before launching the Reddit simulation.","Alternatively export OPENAI_API_KEY directly — the check reads that variable.","Preflight-print missing LLM variables instead of discovering them mid-run."],"exampleFix":"# before\n$ python scripts/run_reddit_simulation.py\n\n# after\nexport LLM_API_KEY=sk-...\n$ python scripts/run_reddit_simulation.py","handlingStrategy":"validation","validationCode":"import os\n\ndef llm_env_ready() -> bool:\n    return bool(os.environ.get(\"LLM_API_KEY\") or os.environ.get(\"OPENAI_API_KEY\"))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep one shared .env with the LLM variables; verify it loads before simulation start.","For scheduled runs, wrap the launch in a script that exports the env.","Preflight-print missing variables instead of discovering them mid-run."],"tags":["configuration","authentication","environment","llm","missing-api-key","camel-ai"],"backgroundTag":null,"analyzedSha":"b5b53acc57189a4a42e44a23e149dc655c98fe82","analyzedAt":"2026-08-14T22:29:33.146Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}