{"record":{"id":"6bd453f6128b7a1f","repo":"datawhalechina/hello-agents","slug":"llm-api-key-export-llm-api-key-y","errorCode":null,"errorMessage":"LLM_API_KEY 环境变量未设置，请先设置环境变量：\nexport LLM_API_KEY=your_llm_api_key_here\n或在创建Agent时传入 llm 参数","messagePattern":"LLM_API_KEY 环境变量未设置，请先设置环境变量：\nexport LLM_API_KEY=your_llm_api_key_here\n或在创建Agent时传入 llm 参数","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"Co-creation-projects/lcyting-StockSage-agent/agents/data_analysis_agent.py","lineNumber":123,"sourceCode":"        system_prompt=prompt,\n        config=Config(temperature=0.2, max_tokens=4096),\n        max_steps=max_steps,\n    )\n\n    return agent\n\n\ndef _create_default_llm() -> HelloAgentsLLM:\n    \"\"\"从环境变量创建默认LLM实例\"\"\"\n    import os\n\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(\n            \"LLM_API_KEY 环境变量未设置，请先设置环境变量：\\n\"\n            \"export LLM_API_KEY=your_llm_api_key_here\\n\"\n            \"或在创建Agent时传入 llm 参数\"\n        )\n\n    return HelloAgentsLLM(\n        model=model,\n        api_key=api_key,\n        base_url=base_url,\n        provider=provider,\n        temperature=0.2,\n    )\n\n\ndef analyze_data_stream(\n    agent: ReActAgent,\n    stock_code: str = \"\",\n    stock_name: str = \"\",","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/lcyting-StockSage-agent/agents/data_analysis_agent.py#L105-L141","documentation":"data_analysis_agent's default-LLM factory raises RuntimeError with explicit remediation text when LLM_API_KEY is unset. The message offers the two supported fixes: export the variable or pass an llm argument at agent construction.","triggerScenarios":"Constructing the data-analysis agent with no LLM_API_KEY in the environment and no llm= argument; env vars present in the dev shell but not in the deployed process.","commonSituations":"Notebook/server restarts losing exported vars; containerized deploys missing env plumbing; .env file present but never loaded because python-dotenv isn't invoked in this code path.","solutions":["export LLM_API_KEY=your_key (or add to .env and load it) before launching.","Construct HelloAgentsLLM yourself and pass llm=... to the agent.","In docker-compose, add LLM_API_KEY to environment: or env_file:.","Add a startup assertion for LLM_API_KEY to fail fast with one aggregated message."],"exampleFix":"# before\nagent = DataAnalysisAgent()  # RuntimeError with export instructions\n\n# after\nllm = HelloAgentsLLM(model=os.getenv(\"LLM_MODEL_ID\"), api_key=os.getenv(\"LLM_API_KEY\"), base_url=os.getenv(\"LLM_BASE_URL\"))\nagent = DataAnalysisAgent(llm=llm)","handlingStrategy":"validation","validationCode":"import os\nif not os.getenv(\"LLM_API_KEY\"):\n    raise SystemExit(\"LLM_API_KEY not set; export it or pass llm= to DataAnalysisAgent\")\nagent = DataAnalysisAgent()","typeGuard":"def can_build_default_llm() -> bool:\n    return bool(os.getenv(\"LLM_API_KEY\"))","tryCatchPattern":"try:\n    agent = DataAnalysisAgent()\nexcept RuntimeError as e:\n    if \"LLM_API_KEY\" in str(e):\n        agent = DataAnalysisAgent(llm=preconfigured_llm)\n    else:\n        raise","preventionTips":["Load .env in the entrypoint so notebook/server restarts keep the variable.","Pass llm explicitly in deployments for deterministic configuration.","Add a startup env checklist covering every LLM_* variable the agents read."],"tags":["env-vars","configuration","llm-client","python"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}