> For the complete documentation index, see [llms.txt](https://docs.ipwo.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ipwo.net/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-langchain-xiang-mu-zhong-pei-zhi-ipwo-dai-li.md).

# 如何在 LangChain 项目中配置 IPWO 代理

LangChain 是目前最流行的大语言模型应用开发框架之一，广泛用于构建 AI Agent、RAG（检索增强生成）、自动化工作流和多模型应用。

在某些网络环境下，开发者可能需要通过代理访问 OpenAI、Claude、Gemini 等模型服务，或者访问外部 API、搜索引擎和数据源。通过配置 IPWO 住宅代理，您可以为 LangChain 应用提供稳定的网络连接，并根据业务需求选择不同国家或地区的出口 IP。

本文将介绍如何在 LangChain 项目中配置 IPWO 住宅代理。

***

### 为什么在 LangChain 中使用代理？

LangChain 本身不会直接访问互联网，但其集成的模型、工具和数据源通常需要网络连接。

常见场景包括：

* 调用 OpenAI API
* 调用 Claude API
* 调用 Gemini API
* 使用 Web 搜索工具
* 访问第三方 API
* AI Agent 浏览网页
* 数据采集与分析

通过代理，可以统一管理这些请求的网络出口。

***

### 环境准备

开始之前，请确保已经获取：

```
Host: us.ipwo.net
Port: 7878

Username: username
Password: password
```

安装 LangChain：

```bash
pip install langchain
```

根据所使用的模型安装对应 SDK：

```bash
pip install langchain-openai
```

```bash
pip install langchain-anthropic
```

```bash
pip install langchain-google-genai
```

***

### 通过环境变量配置代理

这是最简单且推荐的方式。

Linux/macOS：

```bash
export HTTP_PROXY=http://username:password@us.ipwo.net:7878
export HTTPS_PROXY=http://username:password@us.ipwo.net:7878
```

Windows：

```cmd
set HTTP_PROXY=http://username:password@us.ipwo.net:7878
set HTTPS_PROXY=http://username:password@us.ipwo.net:7878
```

配置后，大多数 LangChain 集成组件会自动通过代理访问网络。

***

### OpenAI + LangChain 使用代理

```python
import os

os.environ["HTTP_PROXY"] = "http://username:password@us.ipwo.net:7878"
os.environ["HTTPS_PROXY"] = "http://username:password@us.ipwo.net:7878"

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4o-mini",
    api_key="YOUR_OPENAI_API_KEY"
)

response = llm.invoke("What is a residential proxy?")

print(response.content)
```

***

### Claude + LangChain 使用代理

```python
import os

os.environ["HTTP_PROXY"] = "http://username:password@us.ipwo.net:7878"
os.environ["HTTPS_PROXY"] = "http://username:password@us.ipwo.net:7878"

from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(
    model="claude-sonnet-4-0",
    api_key="YOUR_CLAUDE_API_KEY"
)

response = llm.invoke("Explain residential proxies.")

print(response.content)
```

***

### Gemini + LangChain 使用代理

```python
import os

os.environ["HTTP_PROXY"] = "http://username:password@us.ipwo.net:7878"
os.environ["HTTPS_PROXY"] = "http://username:password@us.ipwo.net:7878"

from langchain_google_genai import ChatGoogleGenerativeAI

llm = ChatGoogleGenerativeAI(
    model="gemini-2.5-flash",
    google_api_key="YOUR_GEMINI_API_KEY"
)

response = llm.invoke("Explain residential proxies.")

print(response.content)
```

***

### LangChain Agent 使用代理

当 Agent 调用搜索工具、API 或浏览器工具时，同样可以继承系统代理配置。

```python
import os

os.environ["HTTP_PROXY"] = "http://username:password@us.ipwo.net:7878"
os.environ["HTTPS_PROXY"] = "http://username:password@us.ipwo.net:7878"
```

之后启动 Agent：

```python
from langchain.agents import initialize_agent

# 初始化 Agent
agent = initialize_agent(
    tools=[],
    llm=llm
)
```

所有支持系统代理的工具都将自动通过代理访问网络。

***

### Browser Use + LangChain 使用代理

如果您的 Agent 集成 Browser Use，可以直接为浏览器配置代理。

```python
from browser_use.browser.browser import Browser

browser = Browser(
    proxy={
        "server": "http://us.ipwo.net:7878",
        "username": "username",
        "password": "password"
    }
)
```

这样浏览器自动化流量也会通过代理访问目标网站。

***

### Docker 部署 LangChain 应用

如果 LangChain 项目运行在 Docker 容器中，可以在启动时配置代理。

```bash
docker run \
-e HTTP_PROXY=http://username:password@us.ipwo.net:7878 \
-e HTTPS_PROXY=http://username:password@us.ipwo.net:7878 \
your-langchain-app
```

容器中的所有网络请求都会通过代理转发。

***

### [验证代理](/kai-fa-zhe-wen-dang/kuai-su-kai-shi/kuai-su-yan-zheng.md)是否生效

在启动 LangChain 应用前，建议先验证代理连接。

```bash
curl -x "us.ipwo.net:7878" \
-U "username:password" \
ipinfo.io
```

如果返回代理出口 IP 信息，则说明代理配置正常。

***

### 常见问题

#### 模型调用超时

可能原因：

* 网络无法访问代理节点
* 代理账号已过期
* 目标服务响应较慢

建议：

* 测试代理连通性
* 更换代理节点
* 增加请求超时时间

***

#### Proxy Authentication Failed

通常表示：

* 用户名错误
* 密码错误
* 代理格式配置错误

请检查：

```
http://username:password@host:port
```

格式是否正确。

***

#### OpenAI、Claude 或 Gemini 返回 401

此类错误通常与 API Key 有关，而不是代理配置问题。

请检查：

* API Key 是否有效
* API Key 是否正确加载
* API 配额是否充足

***

#### Agent 无法访问外部网站

请确认：

* 系统代理已正确配置
* Browser Use 或 Playwright 已正确设置代理
* Docker 容器已继承代理环境变量

***

### 最佳实践

* 推荐通过环境变量统一管理代理配置。
* 长时间运行的 Agent 建议使用 Sticky Session。
* 多 Agent 应用建议为不同任务分配独立 Session。
* 在 Docker、Kubernetes 和云服务器环境中统一管理代理配置。
* 对于网页浏览和数据采集场景，建议结合 Browser Use 或 Playwright 使用住宅代理。

***

### 相关阅读

完成 LangChain 代理配置后，您可能还需要：

* [OpenAI SDK 使用代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-openai-sdk-zhong-shi-yong-ipwo-dai-li.md)
* [Claude API 使用代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-claude-api-zhong-shi-yong-ipwo-dai-li.md)
* [Gemini API 使用代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-gemini-api-zhong-shi-yong-ipwo-dai-li.md)
* [获取和配置住宅代理](/kai-fa-zhe-wen-dang/kuai-su-kai-shi/huo-qu-dai-li-xin-xi.md)
* [如何检测代理是否生效](/dai-li-jian-ce-yu-yin-si-an-quan/ru-he-jian-ce-dai-li-shi-fou-sheng-xiao.md)
* [407 Proxy Authentication Required](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/407-proxy-authentication-required.md)
* [超时 Timeout](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/chao-shi-timeout.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ipwo.net/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-langchain-xiang-mu-zhong-pei-zhi-ipwo-dai-li.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
