> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pipellm.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain（Anthropic）

> 使用 LangChain 的 ChatAnthropic 接入 PipeLLM 的 Anthropic 兼容路由

认证和模型列表请参考 [Anthropic 概述](/api-reference/anthropic/overview.zh)。

<Note>
  这页是框架集成文档。原始 HTTP 端点说明请看
  [Messages](/api-reference/anthropic/messages.zh)。
</Note>

## 安装

```bash theme={null}
pip install langchain-anthropic
```

## 代码示例

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import os
    from langchain_anthropic import ChatAnthropic

    llm = ChatAnthropic(
      model='claude-sonnet-4-5-20250929',
      anthropic_api_key=os.getenv('PIPELLM_API_KEY'),
      anthropic_api_url='https://api.pipellm.ai'
    )

    response = llm.invoke([
      ('user', 'Why is the sky blue?')
    ])

    print(response.content)
    ```
  </Tab>

  <Tab title="Node.js">
    ```javascript theme={null}
    import { ChatAnthropic } from '@langchain/anthropic';

    const llm = new ChatAnthropic({
      model: 'claude-sonnet-4-5-20250929',
      anthropicApiKey: process.env.PIPELLM_API_KEY,
      anthropicApiUrl: 'https://api.pipellm.ai'
    });

    const response = await llm.invoke([
      { role: 'user', content: 'Why is the sky blue?' }
    ]);

    console.log(response.content);
    ```
  </Tab>
</Tabs>

## 流式响应

```python theme={null}
import os
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(
  model='claude-sonnet-4-5-20250929',
  anthropic_api_key=os.getenv('PIPELLM_API_KEY'),
  anthropic_api_url='https://api.pipellm.ai',
  streaming=True
)

for chunk in llm.stream('Tell me a story'):
  print(chunk.content, end='', flush=True)
```

## 函数调用

```python theme={null}
import os
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool

@tool
def get_weather(location: str) -> str:
  """获取指定地点的天气"""
  return f"{location} 的天气：22°C，晴天"

llm = ChatAnthropic(
  model='claude-sonnet-4-5-20250929',
  anthropic_api_key=os.getenv('PIPELLM_API_KEY'),
  anthropic_api_url='https://api.pipellm.ai'
)

llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("东京现在天气怎么样？")

print(response.tool_calls)
```

## 相关文档

<Columns cols={3}>
  <Card title="Anthropic 概述" icon="message" href="/api-reference/anthropic/overview.zh">
    查看 Header、模型和原生路由
  </Card>

  <Card title="Messages" icon="code" href="/api-reference/anthropic/messages.zh">
    查看 `POST /v1/messages` 的端点说明
  </Card>

  <Card title="开发工具总览" icon="terminal" href="/integrations/overview.zh">
    查看 PipeLLM 上的更多工具与框架集成
  </Card>
</Columns>
