{"record":{"id":"f7a15658704783db","repo":"shareAI-lab/learn-claude-code","slug":"model-id-is-required-in-the-environment-or-env","errorCode":null,"errorMessage":"MODEL_ID is required in the environment or .env","messagePattern":"MODEL_ID is required in the environment or \\.env","errorType":"exception","errorClass":"GoalError","httpStatus":null,"severity":"error","filePath":"s17_goal_loop/code.py","lineNumber":822,"sourceCode":"            ]\n            return \"\\n\".join(matches[:200]) if matches else \"(no matches)\"\n\n        raise GoalError(f\"unknown tool '{name}'\")\n\n\ndef make_live_session(workdir: Path) -> AgentSession:\n    try:\n        from anthropic import Anthropic\n        from dotenv import load_dotenv\n    except ImportError as error:\n        raise GoalError(\n            \"Install dependencies first: pip install -r requirements.txt\"\n        ) from error\n\n    load_dotenv(override=True)\n    model = os.getenv(\"MODEL_ID\")\n    if not model:\n        raise GoalError(\"MODEL_ID is required in the environment or .env\")\n    evaluator_model = (\n        os.getenv(\"GOAL_EVALUATOR_MODEL_ID\")\n        or os.getenv(\"ANTHROPIC_DEFAULT_HAIKU_MODEL\")\n        or model\n    )\n    if os.getenv(\"ANTHROPIC_BASE_URL\"):\n        os.environ.pop(\"ANTHROPIC_AUTH_TOKEN\", None)\n    client = Anthropic(base_url=os.getenv(\"ANTHROPIC_BASE_URL\"))\n    evaluator = PromptGoalEvaluator(client=client, model=evaluator_model)\n    block_cap = int(\n        os.getenv(\n            \"CLAUDE_CODE_STOP_HOOK_BLOCK_CAP\",\n            str(DEFAULT_STOP_HOOK_BLOCK_CAP),\n        )\n    )\n    goal = GoalController(evaluator=evaluator, block_cap=block_cap)\n    max_turns_value = int(os.getenv(\"MAX_TURNS\", \"0\"))\n    return AgentSession(","sourceCodeStart":804,"sourceCodeEnd":840,"githubUrl":"https://github.com/shareAI-lab/learn-claude-code/blob/985456f4adea6f4df8fbad4112245dbd97444eae/s17_goal_loop/code.py#L804-L840","documentation":"make_live_session loaded .env (load_dotenv(override=True)) and read MODEL_ID from the environment, but it was empty/unset (s17_goal_loop/code.py:822). MODEL_ID names the main agent model for the Anthropic client, and a live session cannot be constructed without it, so it fails fast.","triggerScenarios":"Running the module with no MODEL_ID in the environment and no .env file (or a .env missing the MODEL_ID line); a typo like MODELID= or MODEL_ID= (empty value); .env located in a different directory than the process cwd so load_dotenv never finds it.","commonSituations":"Fresh setup where .env.example was copied incompletely; CI containers lacking the env var; running from a different working directory so python-dotenv does not discover .env; overriding via shell where the variable was unset by a wrapper script.","solutions":["Create .env in the project root with MODEL_ID set (e.g. MODEL_ID=claude-sonnet-4-5), or export MODEL_ID in the shell","Check for typos and empty values: 'grep -c \"^MODEL_ID=\" .env' must find a non-empty line","If .env lives elsewhere, load it explicitly or run the process from the directory containing it","In CI, inject MODEL_ID as a secret/environment variable instead of a file"],"exampleFix":"# before\n# .env\nANTHROPIC_API_KEY=sk-...\n\n# after\n# .env\nANTHROPIC_API_KEY=sk-...\nMODEL_ID=claude-sonnet-4-5","handlingStrategy":"validation","validationCode":"from pathlib import Path\nif not (os.getenv(\"MODEL_ID\") or \"\").strip():\n    raise SystemExit(\"MODEL_ID is missing: set it in .env or the environment\")\nsession = make_live_session(workdir)","typeGuard":"def has_model_id() -> bool:\n    return bool((os.getenv(\"MODEL_ID\") or \"\").strip())","tryCatchPattern":"try:\n    session = make_live_session(workdir)\nexcept GoalError as error:\n    if \"MODEL_ID\" in str(error):\n        raise SystemExit(\"copy .env.example to .env and set MODEL_ID\") from error\n    raise","preventionTips":["Check .env exists in the process cwd before launching; load_dotenv only searches from there","Keep a .env.example listing MODEL_ID (and GOAL_EVALUATOR_MODEL_ID) for new setups","In CI, provide MODEL_ID as a secret env var rather than a file"],"tags":["goal-loop","configuration","environment","setup"],"backgroundTag":null,"analyzedSha":"985456f4adea6f4df8fbad4112245dbd97444eae","analyzedAt":"2026-08-14T22:02:26.028Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}