datawhalechina/hello-agents · critical · RuntimeError
LLM_API_KEY 环境变量未设置,请先设置环境变量: export LLM_API_KEY=your_llm_ap
Error message
LLM_API_KEY 环境变量未设置,请先设置环境变量: export LLM_API_KEY=your_llm_api_key_here 或在创建Agent时传入 llm 参数
What it means
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.
Source
Thrown at Co-creation-projects/lcyting-StockSage-agent/agents/data_analysis_agent.py:123
system_prompt=prompt,
config=Config(temperature=0.2, max_tokens=4096),
max_steps=max_steps,
)
return agent
def _create_default_llm() -> HelloAgentsLLM:
"""从环境变量创建默认LLM实例"""
import os
model = os.getenv("LLM_MODEL_ID")
api_key = os.getenv("LLM_API_KEY")
base_url = os.getenv("LLM_BASE_URL")
provider = os.getenv("LLM_PROVIDER", "auto")
if not api_key:
raise RuntimeError(
"LLM_API_KEY 环境变量未设置,请先设置环境变量:\n"
"export LLM_API_KEY=your_llm_api_key_here\n"
"或在创建Agent时传入 llm 参数"
)
return HelloAgentsLLM(
model=model,
api_key=api_key,
base_url=base_url,
provider=provider,
temperature=0.2,
)
def analyze_data_stream(
agent: ReActAgent,
stock_code: str = "",
stock_name: str = "",View on GitHub (pinned to 606a07d341)
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.
Example fix
# before
agent = DataAnalysisAgent() # RuntimeError with export instructions
# after
llm = HelloAgentsLLM(model=os.getenv("LLM_MODEL_ID"), api_key=os.getenv("LLM_API_KEY"), base_url=os.getenv("LLM_BASE_URL"))
agent = DataAnalysisAgent(llm=llm) Defensive patterns
Strategy: validation
Validate before calling
import os
if not os.getenv("LLM_API_KEY"):
raise SystemExit("LLM_API_KEY not set; export it or pass llm= to DataAnalysisAgent")
agent = DataAnalysisAgent() Type guard
def can_build_default_llm() -> bool:
return bool(os.getenv("LLM_API_KEY")) Try / catch
try:
agent = DataAnalysisAgent()
except RuntimeError as e:
if "LLM_API_KEY" in str(e):
agent = DataAnalysisAgent(llm=preconfigured_llm)
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- LLM client is not configured. Check .env.
- API密钥和服务地址必须被提供或在.env文件中定义。
- LLM_API_KEY 环境变量未设置
- LLM_API_KEY 环境变量未设置,请先设置环境变量: export LLM_API_KEY=your_llm_ap
- 未配置 AMiner API Key。请前往 https://open.aminer.cn/ 注册获取,然后在 .env
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/6bd453f6128b7a1f.
Report an issue: GitHub.