Source: https://aiprimetech.io/zh/learn/use-claude-api-with-langchain/ [首页](/zh/) / [学习](/zh/learn/) / LangChain 中的 ChatAnthropic:导入、base_url、流式输出、工具与错误 # LangChain 中的 ChatAnthropic:导入、base_url、流式传输、工具和错误 ChatAnthropic 来自 langchain-anthropic 包,是 LangChain 的 Claude 聊天模型。它封装了官方 Anthropic Python SDK,因此只需设置一个参数即可将其指向网关: base_url,将其设为主机根地址。链、智能体和工具中的其他部分均保持不变。 ## 安装与导入 终端复制 ``` pip install -U langchain-anthropic ``` app.py复制 ``` from langchain_anthropic import ChatAnthropic llm = ChatAnthropic( model="claude-sonnet-5", base_url="https://aiprimetech.io", # host root: the SDK adds /v1/messages api_key="your_gateway_key", # or set ANTHROPIC_API_KEY ) print(llm.invoke("Say hello in one word.").text) ``` 否 /v1 在 base_url 此处。底层 Anthropic SDK 会自行追加 /v1/messages ;添加 /v1 会产生 /v1/v1/messages 以及一个 404 page not found. ## 通过环境变量配置 base_url 是以下参数的别名: anthropic_api_url,以及 api_key 是以下参数的别名: anthropic_api_key。如果未传入这些参数,ChatAnthropic 会先读取 ANTHROPIC_API_URL ,然后读取 ANTHROPIC_BASE_URL,并从以下变量读取密钥: ANTHROPIC_API_KEY。显式参数始终优先。 终端复制 ``` export ANTHROPIC_API_KEY=your_gateway_key export ANTHROPIC_API_URL=https://aiprimetech.io ``` app.py复制 ``` from langchain_anthropic import ChatAnthropic llm = ChatAnthropic(model="claude-sonnet-5") # picks up the gateway from the environment ``` ## 流式传输 app.py复制 ``` for chunk in llm.stream("Explain backpressure in two sentences."): print(chunk.text, end="", flush=True) ``` .text 仅收集每个分块中的文本块,因此即使模型同时输出思考块或工具使用块,结果仍然保持干净。对于较长的回答,请使用流式输出:耗时较长的非流式请求通常是导致超时的原因。 ## 工具与结构化输出 tools.py复制 ``` from langchain_core.tools import tool @tool def get_time(city: str) -> str: """Return the current local time in a city.""" return "12:00" msg = llm.bind_tools([get_time]).invoke("What time is it in Tokyo?") print(msg.tool_calls) ``` structured.py复制 ``` from pydantic import BaseModel class Weather(BaseModel): city: str temperature_c: float print(llm.with_structured_output(Weather).invoke("It is 21 degrees in Lisbon today.")) ``` ## 重试、超时与 max_tokens max_retries 默认值为 2,与 Anthropic SDK 相同;该 SDK 会对连接错误、429 和 5xx 响应进行重试。 timeout (是以下参数的别名: default_request_timeout)的单位为秒。如果你未设置 max_tokens ,则取自模型的配置档案。 app.py复制 ``` llm = ChatAnthropic( model="claude-sonnet-5", base_url="https://aiprimetech.io", max_retries=4, timeout=120, max_tokens=8000, ) ``` 对于批量请求,请限制并行度,以免超出密钥的并发限制: llm.batch(inputs, config={"max_concurrency": 3}). ## ChatOpenAI 替代方案 如果你的代码统一使用 ChatOpenAI,网关的 OpenAI 兼容端点也支持 Claude。此方式要求在末尾添加 /v1 ,这与 ChatAnthropic 相反。 app.py复制 ``` from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="claude-sonnet-5", base_url="https://aiprimetech.io/v1", api_key="your_gateway_key") ``` 使用 Claude 时,优先选择 ChatAnthropic:它保留了提示词缓存和思考块等原生功能。 ## 故障排查 | 你会看到什么 | 原因 | 解决方法 | 404 page not found | /v1 在 ChatAnthropic 的以下参数中: base_url | 使用主机根地址, https://aiprimetech.io | Model "..." is not supported | 密钥所属组不支持的 ID,或带有旧日期的名称 | 使用下表中的当前 ID | AuthenticationError | ANTHROPIC_API_KEY 在运行中的进程里为空,或使用了其他端点的密钥 | 传入 api_key= ,通过显式传入进行测试 | RateLimitError: Concurrency limit exceeded | 并行调用过多(batch、异步 gather) | max_concurrency 在批处理配置中 | APITimeoutError | 耗时较长的非流式生成 | 使用流式传输,或增大 timeout | 模型 ID | 适用场景 | claude-sonnet-5 | 编程和智能体任务的默认选择:速度快、能力强、价格适中。 | claude-opus-5-5 | 更复杂的推理、大规模重构、长时间运行的智能体任务。 | claude-fable-5-1 | 能力最强,价格也最高;留给最困难的任务使用。 | claude-haiku-4-5 | 价格低、速度快:标题、摘要、分类和小步骤。 ## 常见问题 如何导入 ChatAnthropic? 先运行 pip install -U langchain-anthropic,然后使用 from langchain_anthropic import ChatAnthropic。 ChatAnthropic 的 base_url 需要包含 /v1 吗? 不需要。传入主机根地址,例如 https://aiprimetech.io;,Anthropic SDK 会追加 /v1/messages。ChatOpenAI 则相反,需要包含 /v1。 ChatAnthropic 会读取哪些环境变量? 使用 ANTHROPIC_API_KEY 获取密钥;未传入 base_url 时,依次使用 ANTHROPIC_API_URL、ANTHROPIC_BASE_URL 获取端点。 base_url 与 anthropic_api_url 相同吗? 是的。base_url 是 anthropic_api_url 的别名,api_key 是 anthropic_api_key 的别名。 几分钟内开始使用 Claude 获取 API 密钥,无需 Anthropic 账户,也无需加入候补名单。[获取你的 API 密钥](/register) AI Prime Tech 是独立的 API 网关,与 Anthropic 无关联,未获得其背书,也不是其经销商。Claude 及相关模型名称是其各自所有者的商标。 ## 更多 Claude API 指南 [如何获取 Claude API 密钥](/zh/learn/how-to-get-a-claude-api-key/)[Claude API 定价详解](/zh/learn/claude-api-pricing-explained/)[使用 Claude API 最便宜的方式](/zh/learn/cheapest-way-to-use-claude-api/)[通过 Claude API 不限量使用 Cursor](/learn/use-claude-api-with-cursor/)[不限量访问 Claude Code](/zh/learn/use-claude-api-with-claude-code/)[Claude API 网关与 Anthropic 直连对比](/zh/learn/claude-api-vs-anthropic-direct/)[通过兼容 OpenAI 的 API 使用 Claude](/zh/learn/openai-compatible-claude-api/)[Claude 模型详解:Claude Opus、Sonnet、Haiku 与 Fable 的模型对比——全部 Claude API 模型、Anthropic 模型名称与价格(2026)](/zh/learn/claude-api-models-compared/)[使用加密货币支付 Claude API 费用](/zh/learn/claude-api-with-crypto-payment/)[无需候补名单即可访问 Claude API](/zh/learn/claude-api-without-waitlist/)[如何减少 Claude 词元用量](/zh/learn/how-to-reduce-claude-token-usage/)[通过 Claude API 不限量使用 Cline](/zh/learn/use-claude-api-with-cline/)[通过 Claude API 不限量使用 Roo Code](/zh/learn/use-claude-api-with-roo-code/)[在 JetBrains IDE 中不限量使用 Claude](/zh/learn/use-claude-api-with-jetbrains/)[通过 Claude API 无限制使用 Aider](/zh/learn/use-claude-api-with-aider/)[在 VS Code 中不限量使用 Claude](/zh/learn/use-claude-api-with-vscode/)[OpenCode 的 Claude 配置:opencode.json 设置、模型与常见错误](/zh/learn/use-claude-api-with-opencode/)[在 Claude 上运行不受限制的 Hermes 智能体](/zh/learn/run-hermes-agent-with-claude-api/)[使用 Claude 不限量运行 OpenClaw](/zh/learn/run-openclaw-with-claude-api/)[兼容 Anthropic 的 API 意味着什么](/zh/learn/anthropic-compatible-api/)[Claude Code API 密钥 — ANTHROPIC_API_KEY:如何获取、配置,以及何时应改用 claude setup-token](/zh/learn/claude-code-api-key-setup/)[Claude Code API 成本 — 定价与降低成本的方法](/zh/learn/claude-code-api-costs/)[Claude API 错误详解:服务器错误 500、过载 529、速率限制 429、错误请求 400 — 以及各自的修复方法](/zh/learn/claude-api-errors-troubleshooting/)[如何购买 Claude API 额度,以及为什么购买 Claude 额度比任何“购买 Claude 账户”的方案都更好](/zh/learn/buy-claude-api-credits/)[Claude 免费 API:有免费的 Claude API 密钥吗?2026 年究竟有哪些内容免费?](/zh/learn/claude-api-free-trial/)[Claude API 与 Claude Pro/Max 订阅——如何选择](/zh/learn/claude-api-vs-subscription/)[使用 Claude 运行 Codex CLI](/zh/learn/use-claude-api-with-codex/)[将 Claude 连接到 n8n](/zh/learn/use-claude-api-with-n8n/)[通过 LiteLLM 路由到 Claude](/zh/learn/use-claude-api-with-litellm/)[在 Kilo Code 中使用 Claude](/zh/learn/use-claude-api-with-kilo-code/)[将 Claude 接入 GitHub Copilot](/zh/learn/use-claude-api-with-github-copilot/)[通过网关将 Zed 连接到 Claude](/zh/learn/use-claude-api-with-zed/)[将 Cherry Studio 连接到 Claude](/zh/learn/use-claude-api-with-cherry-studio/)[Anthropic Python SDK:安装、设置 base_url、流式传输和错误处理](/zh/learn/anthropic-python-sdk/)[通过 Vercel AI SDK 使用 Claude:createAnthropic baseURL、流式传输和问题修复](/zh/learn/use-claude-api-with-vercel-ai-sdk/)[使用 Claude 运行 Factory Droid](/zh/learn/use-claude-api-with-factory-droid/)[Claude Code API 详解](/zh/learn/claude-code-api/)[将 Claude API 集成到您的应用中](/zh/learn/claude-api-integration/)[如何不受限制地使用 Claude,以及为什么不存在“Claude Code 破解版本”](/zh/learn/get-unlimited-claude-usage/)