{"record":{"id":"eb42eb8c06e4e950","repo":"hsliuping/TradingAgents-CN","slug":"openrouter-openrouter-api-key-openai-api-key","errorCode":null,"errorMessage":"使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量","messagePattern":"使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"tradingagents/graph/trading_graph.py","lineNumber":299,"sourceCode":"                **_deep_extra,\n            )\n\n            logger.info(f\"✅ [混合模式] LLM 实例创建成功\")\n\n        elif normalized_provider in {\"openai\", \"siliconflow\", \"openrouter\", \"aihubmix\", \"volcengine\", \"ollama\"}:\n            provider = normalized_provider\n            logger.info(f\"🔧 [{provider}-快速模型] max_tokens={quick_max_tokens}, temperature={quick_temperature}, timeout={quick_timeout}s\")\n            logger.info(f\"🔧 [{provider}-深度模型] max_tokens={deep_max_tokens}, temperature={deep_temperature}, timeout={deep_timeout}s\")\n\n            api_key = None\n            if provider == \"siliconflow\":\n                api_key = os.getenv('SILICONFLOW_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用SiliconFlow需要设置SILICONFLOW_API_KEY环境变量\")\n            elif provider == \"openrouter\":\n                api_key = os.getenv('OPENROUTER_API_KEY') or os.getenv('OPENAI_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用OpenRouter需要设置OPENROUTER_API_KEY或OPENAI_API_KEY环境变量\")\n            elif provider == \"aihubmix\":\n                api_key = os.getenv('AIHUBMIX_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用AiHubMix需要设置AIHUBMIX_API_KEY环境变量\")\n            elif provider == \"volcengine\":\n                api_key = os.getenv('VOLCENGINE_API_KEY') or os.getenv('ARK_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用火山方舟需要设置VOLCENGINE_API_KEY或ARK_API_KEY环境变量\")\n            elif provider == \"volcengine_coding\":\n                api_key = os.getenv('VOLCENGINE_CODING_API_KEY')\n                if not api_key:\n                    raise ValueError(\"使用火山方舟编程需要设置VOLCENGINE_CODING_API_KEY环境变量\")\n\n            self.deep_thinking_llm, self.quick_thinking_llm = _create_provider_pair(\n                provider=provider,\n                config=self.config,\n                quick_temperature=quick_temperature,\n                quick_max_tokens=quick_max_tokens,","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/hsliuping/TradingAgents-CN/blob/74783e8817d6cf6de29867880631cc555153f36b/tradingagents/graph/trading_graph.py#L281-L317","documentation":"TradingAgentsGraph.__init__ raises this when provider is 'openrouter' and neither OPENROUTER_API_KEY nor OPENAI_API_KEY is set. OpenRouter keys are OpenAI-compatible, hence the OpenAI fallback. The check is purely environmental — DB-configured keys are not consulted in this branch.","triggerScenarios":"Constructing the graph with llm_provider=\"openrouter\" while both OPENROUTER_API_KEY and OPENAI_API_KEY are absent from the environment.","commonSituations":"Env var not passed into docker container or background service; user assumed OpenAI key in .env would be picked up but .env isn't loaded; CI runs without secrets configured.","solutions":["export OPENROUTER_API_KEY=sk-or-... (preferred) or OPENAI_API_KEY=sk-... as fallback","Add the variable to .env / docker-compose environment and ensure load_dotenv() runs before constructing TradingAgentsGraph","Confirm the variable name spelling — OPENROUTER_API_KEY, not OPENROUTER_KEY"],"exampleFix":"# before\ngraph = TradingAgentsGraph(config={\"llm_provider\": \"openrouter\"})\n\n# after\n# shell: export OPENROUTER_API_KEY=sk-or-...\ngraph = TradingAgentsGraph(config={\"llm_provider\": \"openrouter\"})","handlingStrategy":"validation","validationCode":"import os\nif provider == \"openrouter\" and not (os.getenv(\"OPENROUTER_API_KEY\") or os.getenv(\"OPENAI_API_KEY\")):\n    raise SystemExit(\"Set OPENROUTER_API_KEY (or OPENAI_API_KEY) before starting\")","typeGuard":"def has_openrouter_key() -> bool:\n    return bool(os.getenv(\"OPENROUTER_API_KEY\") or os.getenv(\"OPENAI_API_KEY\"))","tryCatchPattern":"try:\n    graph = TradingAgentsGraph(config=cfg)\nexcept ValueError as e:\n    if \"OpenRouter\" in str(e):\n        log.error(\"Provision OPENROUTER_API_KEY in the service environment\")\n        raise SystemExit(2) from e\n    raise","preventionTips":["Validate both accepted env names at startup","Document that OPENAI_API_KEY acts as a fallback for OpenRouter","Never assume interactive shell env reaches daemonized processes"],"tags":["api-key","missing-env-var","openrouter","llm","configuration"],"backgroundTag":"missing-api-key","analyzedSha":"74783e8817d6cf6de29867880631cc555153f36b","analyzedAt":"2026-08-28T11:39:07.729Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}