|
| 1 | +# LLM Agent 模型重试示例 |
| 2 | + |
| 3 | +本示例演示如何在模型构造时传入 `ModelRetryConfig`,让业务代码不用自己实现重试逻辑;当 LLM 请求遇到限流、超时、网络波动等瞬时错误时,SDK 会自动重试。 |
| 4 | + |
| 5 | +## 关键特性 |
| 6 | + |
| 7 | +- **按需开启重试**:只有显式传入 `ModelRetryConfig` 的模型才会启用 SDK 托管重试。 |
| 8 | +- **一套配置,多种模型可用**:同一个 `ModelRetryConfig` 可以传给 OpenAI、Anthropic、LiteLLM 等模型实现,统一控制重试次数和指数退避。 |
| 9 | +- **Provider 自己判断是否重试**:OpenAI、Anthropic、LiteLLM 会按各自 SDK 的错误语义、响应 header 和 HTTP status 判断是否重试。 |
| 10 | +- **避免重复输出内容**:流式输出已经产生内容后如果再失败,错误会直接透出,不会重试并重复输出内容。 |
| 11 | +- **优先使用服务端等待时间**:默认会读取 `Retry-After` / `retry-after-ms`,有服务端等待时间时优先使用。 |
| 12 | + |
| 13 | +## Agent 层级结构说明 |
| 14 | + |
| 15 | +本例是单 Agent 示例,重试配置绑定在模型上: |
| 16 | + |
| 17 | +```text |
| 18 | +weather_retry_agent (LlmAgent) |
| 19 | +├── model: OpenAIModel(..., model_retry_config=ModelRetryConfig(...)) |
| 20 | +├── tool: get_weather_report(city) |
| 21 | +└── runner: 无自定义重试逻辑 |
| 22 | +``` |
| 23 | + |
| 24 | +关键文件: |
| 25 | + |
| 26 | +- [examples/llmagent_with_model_retry/agent/agent.py](./agent/agent.py):创建模型并注入 `ModelRetryConfig` |
| 27 | +- [examples/llmagent_with_model_retry/agent/config.py](./agent/config.py):读取模型连接与重试环境变量 |
| 28 | +- [examples/llmagent_with_model_retry/agent/tools.py](./agent/tools.py):天气工具实现 |
| 29 | +- [examples/llmagent_with_model_retry/agent/prompts.py](./agent/prompts.py):提示词 |
| 30 | +- [examples/llmagent_with_model_retry/run_agent.py](./run_agent.py):运行入口,展示业务层无需手写重试 |
| 31 | + |
| 32 | +## 关键代码解释 |
| 33 | + |
| 34 | +### 1) 创建重试配置 |
| 35 | + |
| 36 | +`agent/config.py` 从环境变量构造: |
| 37 | + |
| 38 | +```python |
| 39 | +from trpc_agent_sdk.configs import ExponentialBackoffConfig |
| 40 | +from trpc_agent_sdk.configs import ModelRetryConfig |
| 41 | + |
| 42 | +ModelRetryConfig( |
| 43 | + num_retries=2, # 最多重试 2 次(不包含首次请求) |
| 44 | + backoff=ExponentialBackoffConfig( |
| 45 | + initial_backoff=1.0, # 首次重试前等待 1 秒 |
| 46 | + max_backoff=8.0, # 单次重试等待时间最多 8 秒 |
| 47 | + multiplier=2.0, # 每次失败后等待时间按 2 倍递增 |
| 48 | + jitter=True, # 加入随机抖动,避免并发重试同时打到服务 |
| 49 | + ), |
| 50 | +) |
| 51 | +``` |
| 52 | + |
| 53 | +### 2) 注入模型 |
| 54 | + |
| 55 | +`agent/agent.py` 将配置传给模型构造器: |
| 56 | + |
| 57 | +```python |
| 58 | +OpenAIModel( |
| 59 | + model_name=model_name, |
| 60 | + api_key=api_key, |
| 61 | + base_url=base_url, |
| 62 | + model_retry_config=retry_config, # 将重试配置注入模型,调用失败时按配置自动重试 |
| 63 | +) |
| 64 | +``` |
| 65 | + |
| 66 | +### 3) 配置字段说明 |
| 67 | + |
| 68 | +- `num_retries`:初始请求失败后的额外重试次数,不包含第一次请求。 |
| 69 | +- `initial_backoff`:第一次重试前的基础等待时间,单位秒。 |
| 70 | +- `max_backoff`:单次等待时间上限,单位秒。 |
| 71 | +- `multiplier`:指数退避倍数。 |
| 72 | +- `jitter`:是否启用 full jitter,避免并发请求同时重试。 |
| 73 | + |
| 74 | +SDK 会优先尊重 provider 返回的 `Retry-After` / `retry-after-ms` 服务端建议等待时间。 |
| 75 | + |
| 76 | +### 4) Runner 不需要重试逻辑 |
| 77 | + |
| 78 | +`run_agent.py` 仍然只调用: |
| 79 | + |
| 80 | +```python |
| 81 | +async for event in runner.run_async(...): |
| 82 | + ... |
| 83 | +``` |
| 84 | + |
| 85 | +如果模型调用在产出内容前遇到可重试错误,SDK 会按 `ModelRetryConfig` 自动重试。 |
| 86 | + |
| 87 | +## 会重试和不会重试的场景 |
| 88 | + |
| 89 | +不同 provider 会按各自 SDK 的错误语义判断异常类型;对于带响应信息的错误,通用优先级如下: |
| 90 | + |
| 91 | +### 会重试 |
| 92 | + |
| 93 | +- 响应 header `x-should-retry: true`。 |
| 94 | +- HTTP status 为 `408` / `409` / `429` / `>=500`。 |
| 95 | +- OpenAI / Anthropic 相关的超时、连接类瞬时错误。 |
| 96 | + |
| 97 | +### 不会重试 |
| 98 | + |
| 99 | +- 响应 header `x-should-retry: false`。 |
| 100 | +- HTTP status 为其他 `4xx` 错误,例如 `400` / `401` / `403` / `404`。 |
| 101 | +- 重试次数已耗尽。 |
| 102 | +- 流式输出已经产生内容后才发生的错误。 |
| 103 | + |
| 104 | +## 环境与运行 |
| 105 | + |
| 106 | +### 环境要求 |
| 107 | + |
| 108 | +- Python 3.10+ |
| 109 | + |
| 110 | +### 安装步骤 |
| 111 | + |
| 112 | +```bash |
| 113 | +git clone https://github.com/trpc-group/trpc-agent-python.git |
| 114 | +cd trpc-agent-python |
| 115 | +python3 -m venv .venv |
| 116 | +source .venv/bin/activate |
| 117 | +pip3 install -e . |
| 118 | +``` |
| 119 | + |
| 120 | +### 环境变量要求 |
| 121 | + |
| 122 | +在 [examples/llmagent_with_model_retry/.env](./.env) 中配置(或通过 `export` 设置): |
| 123 | + |
| 124 | +```bash |
| 125 | +# 必填:模型连接配置 |
| 126 | +TRPC_AGENT_API_KEY=<your-api-key> |
| 127 | +TRPC_AGENT_BASE_URL=<openai-compatible-base-url> |
| 128 | +TRPC_AGENT_MODEL_NAME=<model-name> |
| 129 | + |
| 130 | +# 可选:模型重试配置;不设置时使用示例默认值 |
| 131 | +TRPC_AGENT_MODEL_RETRY_NUM_RETRIES=2 |
| 132 | +TRPC_AGENT_MODEL_RETRY_INITIAL_BACKOFF=1.0 |
| 133 | +TRPC_AGENT_MODEL_RETRY_MAX_BACKOFF=8.0 |
| 134 | +TRPC_AGENT_MODEL_RETRY_BACKOFF_MULTIPLIER=2.0 |
| 135 | +TRPC_AGENT_MODEL_RETRY_JITTER=true |
| 136 | +``` |
| 137 | + |
| 138 | +### 运行命令 |
| 139 | + |
| 140 | +```bash |
| 141 | +cd examples/llmagent_with_model_retry |
| 142 | +python3 run_agent.py |
| 143 | +``` |
| 144 | + |
| 145 | +## 运行结果示例 |
| 146 | + |
| 147 | +```text |
| 148 | +Model retry enabled: {'num_retries': 2, 'backoff': {'initial_backoff': 1.0, 'max_backoff': 8.0, 'multiplier': 2.0, 'jitter': True}} |
| 149 | +Session ID: fdb9e370... |
| 150 | +User: What's the current weather in Beijing? |
| 151 | +Assistant: |
| 152 | +Invoke Tool: get_weather_report({'city': 'Beijing'}) |
| 153 | +Tool Result: {'temperature': '25°C', 'condition': 'Sunny', 'humidity': '60%'} |
| 154 | +
|
| 155 | +# 当模型服务返回可重试错误时,SDK 会在模型层自动重试: |
| 156 | +[WARNING] Model call failed (exception=RateLimitError); retrying in 0.46s (attempt 1/2). |
| 157 | +``` |
0 commit comments