{"record":{"id":"83815ae18d6e7299","repo":"binary-husky/gpt_academic","slug":"anthropic-api-key","errorCode":null,"errorMessage":"没有设置ANTHROPIC_API_KEY选项","messagePattern":"没有设置ANTHROPIC_API_KEY选项","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"critical","filePath":"request_llms/bridge_claude.py","lineNumber":88,"sourceCode":"\n\ndef predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt=\"\", observe_window=None, console_silence=False):\n    \"\"\"\n    发送至chatGPT，等待回复，一次性完成，不显示中间过程。但内部用stream的方法避免中途网线被掐。\n    inputs：\n        是本次问询的输入\n    sys_prompt:\n        系统静默prompt\n    llm_kwargs：\n        chatGPT的内部调优参数\n    history：\n        是之前的对话列表\n    observe_window = None：\n        用于负责跨越线程传递已经输出的部分，大部分时候仅仅为了fancy的视觉效果，留空即可。observe_window[0]：观测窗。observe_window[1]：看门狗\n    \"\"\"\n    watch_dog_patience = 5 # 看门狗的耐心, 设置5秒即可\n    if len(ANTHROPIC_API_KEY) == 0:\n        raise RuntimeError(\"没有设置ANTHROPIC_API_KEY选项\")\n    if inputs == \"\":     inputs = \"空空如也的输入栏\"\n    headers, message = generate_payload(inputs, llm_kwargs, history, sys_prompt, image_paths=None)\n    retry = 0\n\n\n    while True:\n        try:\n            # make a POST request to the API endpoint, stream=False\n            from .bridge_all import model_info\n            endpoint = model_info[llm_kwargs['llm_model']]['endpoint']\n            response = requests.post(endpoint, headers=headers, json=message,\n                                     proxies=proxies, stream=True, timeout=TIMEOUT_SECONDS);break\n        except requests.exceptions.ReadTimeout as e:\n            retry += 1\n            traceback.print_exc()\n            if retry > MAX_RETRY: raise TimeoutError\n            if MAX_RETRY!=0: logger.error(f'请求超时，正在重试 ({retry}/{MAX_RETRY}) ……')\n    stream_response = response.iter_lines()","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/request_llms/bridge_claude.py#L70-L106","documentation":"RuntimeError raised at the start of Claude predict_no_ui_long_connection when the module-level ANTHROPIC_API_KEY (loaded from config at import time) is an empty string. The bridge needs the key to build the x-api-key headers in generate_payload, so it refuses before issuing the POST to the Anthropic messages endpoint. Unlike OpenAI keys, there is no type-it-in-the-chat-box fallback here — configuration is the only route.","triggerScenarios":"ANTHROPIC_API_KEY missing or left as '' in config.py/config_private.py while selecting a Claude model; config_private.py created but the variable omitted; environment-specific config not loaded because the file is in the wrong location.","commonSituations":"Fresh installs trying the newly added Claude bridge without editing config; users who configured only OpenAI keys; docker deployments where config_private.py isn't mounted.","solutions":["Set ANTHROPIC_API_KEY = \"sk-ant-...\" in config_private.py at the repo root and restart gpt_academic.","Verify config_private.py is in the project root so it shadows config.py and its value is actually loaded.","Confirm the key is active in the Anthropic Console (console.anthropic.com) and has credit.","If behind a proxy, also check the anthropic endpoint reachability so the subsequent request doesn't fail on network."],"exampleFix":"# config_private.py\nANTHROPIC_API_KEY = \"sk-ant-api03-your-real-key\"","handlingStrategy":"validation","validationCode":"from toolbox import get_conf\nassert get_conf('ANTHROPIC_API_KEY') != '', \"set ANTHROPIC_API_KEY in config_private.py before using Claude models\"","typeGuard":null,"tryCatchPattern":"try:\n    result = predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window)\nexcept RuntimeError as e:\n    if 'ANTHROPIC_API_KEY' in str(e):\n        block_claude_models_until_configured()\n    else:\n        raise","preventionTips":["Add ANTHROPIC_API_KEY to your config_private.py template checklist on day one.","Validate the key non-empty at startup, before any model selection is possible.","Hide/disable Claude models in the UI when the key is absent.","Remember there is no in-chat key fallback for this bridge — config is the only route."],"tags":["claude","anthropic","api-key","configuration","authentication"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}