> 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-langgraph-xiang-mu-zhong-pei-zhi-ipwo-dai-li.md).

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

LangGraph 是一款基于知识图谱的 AI Agent 开发框架，可与 LangChain、Claude、Gemini 等模型协同工作，支持网页抓取、数据处理、工具调用和自动化任务。

在国内或海外环境中访问外部 API 或网页时，配置 IPWO 住宅代理可以保证 LangGraph 的网络请求稳定，并根据业务需求选择不同国家或地区的出口 IP。

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

***

### 为什么为 LangGraph 配置代理？

LangGraph 的节点可能会：

* 调用 OpenAI / Claude / Gemini API
* 使用 Web Scraping 或搜索工具
* 请求第三方 API
* 访问数据库或文件服务器
* 执行自动化任务

如果网络环境限制访问或需要切换国家节点，配置代理可以：

* 提供稳定访问网络服务的能力
* 避免因 IP 局限导致调用失败
* 管理多节点或多 Agent 的请求出口
* 结合 Sticky Session 或 IP 轮换，提高稳定性

***

### 环境准备

确保已获取 IPWO 住宅代理信息：

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

Username: username
Password: password
```

安装 LangGraph：

```bash
pip install langgraph
```

根据所使用模型安装 SDK：

```bash
pip install langchain-openai
pip install langchain-anthropic
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
```

LangGraph 节点将自动继承这些代理配置，所有 HTTP/HTTPS 请求都会通过代理发出。

***

### OpenAI / LangChain 节点代理配置示例

```python
import os
from langgraph.nodes import LLMNode
from langchain_openai import ChatOpenAI

# 配置系统代理
os.environ["HTTP_PROXY"] = "http://username:password@us.ipwo.net:7878"
os.environ["HTTPS_PROXY"] = "http://username:password@us.ipwo.net:7878"

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

node = LLMNode(llm=llm)
result = node.run("Explain residential proxies")
print(result)
```

***

### Claude 节点代理配置示例

```python
from langgraph.nodes import LLMNode
from langchain_anthropic import ChatAnthropic

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

node = LLMNode(llm=llm)
result = node.run("Explain residential proxies")
print(result)
```

***

### Gemini 节点代理配置示例

```python
from langgraph.nodes import LLMNode
from langchain_google_genai import ChatGoogleGenerativeAI

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

node = LLMNode(llm=llm)
result = node.run("Explain residential proxies")
print(result)
```

***

#### Browser Use / Web Scraping 节点代理配置

如果 LangGraph 节点使用 Browser Use 或 Playwright 进行网页操作，可直接为浏览器设置代理：

```python
from browser_use.browser.browser import Browser
from langgraph.nodes import BrowserNode

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

browser_node = BrowserNode(browser=browser)
browser_node.run("Visit https://ipinfo.io to check IP")
```

***

### Docker 部署 LangGraph 节点

如果节点部署在 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-langgraph-image
```

***

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

启动 LangGraph 节点后，可通过以下方式验证：

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

返回出口 IP 和地理信息表示代理正常。

***

### 常见问题

#### 节点无法访问外部 API

* 检查系统代理是否生效
* 检查代理账号是否有效
* 测试网络通畅性

#### Connection Timeout

* 更换代理节点
* 增加请求超时设置
* 检查防火墙或网络策略

#### Proxy Authentication Failed

* 检查用户名和密码
* 检查代理 URL 格式

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

#### LLM 返回 401 或 429

* API Key 是否有效
* API 配额是否充足
* 是否符合模型访问限制

***

### 最佳实践

* 使用环境变量统一管理代理信息
* 长时间运行的 Agent 建议使用 Sticky Session
* 多节点或多任务应用可分配不同 Session
* 数据采集和浏览器节点建议使用住宅代理
* Docker、Kubernetes 环境统一管理代理，避免节点互相影响

***

### 相关阅读

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

* [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-langgraph-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.
