{"record":{"id":"cc62994b46046ebf","repo":"FoundationAgents/MetaGPT","slug":"please-set-your-api-key-in-root-config-path-if","errorCode":null,"errorMessage":"Please set your API key in {root_config_path}. If you also set your config in {repo_config_path}, \nthe former will overwrite the latter. This may cause unexpected result.\n","messagePattern":"Please set your API key in (.+?)\\. If you also set your config in (.+?), \nthe former will overwrite the latter\\. This may cause unexpected result\\.\n","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"metagpt/configs/llm_config.py","lineNumber":123,"sourceCode":"\n    # Compress request messages under token limit\n    compress_type: CompressType = CompressType.NO_COMPRESS\n\n    # For Messages Control\n    use_system_prompt: bool = True\n\n    # reasoning / thinking switch\n    reasoning: bool = False\n    reasoning_max_token: int = 4000  # reasoning budget tokens to generate, usually smaller than max_token\n\n    @field_validator(\"api_key\")\n    @classmethod\n    def check_llm_key(cls, v):\n        if v in [\"\", None, \"YOUR_API_KEY\"]:\n            repo_config_path = METAGPT_ROOT / \"config/config2.yaml\"\n            root_config_path = CONFIG_ROOT / \"config2.yaml\"\n            if root_config_path.exists():\n                raise ValueError(\n                    f\"Please set your API key in {root_config_path}. If you also set your config in {repo_config_path}, \\n\"\n                    f\"the former will overwrite the latter. This may cause unexpected result.\\n\"\n                )\n            elif repo_config_path.exists():\n                raise ValueError(f\"Please set your API key in {repo_config_path}\")\n            else:\n                raise ValueError(\"Please set your API key in config2.yaml\")\n        return v\n\n    @field_validator(\"timeout\")\n    @classmethod\n    def check_timeout(cls, v):\n        return v or LLM_API_TIMEOUT\n","sourceCodeStart":105,"sourceCodeEnd":137,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/configs/llm_config.py#L105-L137","documentation":"LLMConfig's api_key field validator rejects empty/None/'YOUR_API_KEY' keys. This variant fires when the global user config at CONFIG_ROOT/config2.yaml (~/.metagpt or METAGPT_CONFIG_PATH location) exists but still contains a placeholder/empty api_key; the long message also warns that the root config overrides the repo-level config/config2.yaml, which is a common double-config trap.","triggerScenarios":"Creating any LLM-backed component (LLM(), an Action with an LLM, starting the software-company flow) while CONFIG_ROOT/config2.yaml exists with api_key: '' or 'YOUR_API_KEY'. The root file takes precedence, so fixing only the repo copy does not help.","commonSituations":"Copying the shipped config2.yaml template to ~/.metagpt without filling the key; setting the key only in the repo config and being silently overridden; env var METAGPT_CONFIG_PATH pointing at an old template.","solutions":["Open the root config path shown in the message and set a real api_key under the provider you use.","Remove or comment the api_key entry in the root config so the repo-level config2.yaml takes effect.","Alternatively supply the key programmatically: LLMConfig(api_key=os.environ['OPENAI_API_KEY']) or via environment variable if supported by your provider config."],"exampleFix":"# before (~/.metagpt/config2.yaml)\nopenai:\n  api_key: 'YOUR_API_KEY'\n\n# after\nopenai:\n  api_key: 'sk-...'","handlingStrategy":"validation","validationCode":"from metagpt.configs.llm_config import LLMConfig\ncfg = LLMConfig.default()\nassert cfg.api_key not in ('', None, 'YOUR_API_KEY'), 'set a real api_key before running'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fill api_key in the root config (~/.metagpt/config2.yaml), which wins over the repo copy.","Avoid keeping two configs with different keys.","Add a startup check that fails fast with a clear message before any LLM call."],"tags":["config","api-key","llm","setup"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}