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

# 速率限制

> 使用指数退避、随机抖动、有限重试与账号级控制正确处理 Yelu API 请求限制。

Yelu 使用请求限制保护平台可靠性。有效限额可能因账号分组、模型和部署配置而不同，以 Dashboard 与账号配置为准。

## 达到限制时

API 返回 `429 Too Many Requests` 和兼容 OpenAI 的错误对象。使用指数退避和随机抖动，只在操作可以安全重复时重试。

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{"error":{"message":"Rate limit reached. Please retry later.","type":"rate_limit_error","param":null,"code":"rate_limit_exceeded"}}
```

<CodeGroup>
  ```bash Curl theme={"theme":{"light":"github-light","dark":"github-dark"}}
  for delay in 1 2 4 8; do
    status=$(curl -sS -o response.json -w "%{http_code}" \
      https://api.yelu.ai/v1/models \
      -H "Authorization: Bearer $YELU_API_KEY")
    [ "$status" != "429" ] && break
    sleep "$delay"
  done
  cat response.json
  ```

  ```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

  async function listModels(attempts = 4) {
    for (let attempt = 0; attempt < attempts; attempt += 1) {
      const response = await fetch('https://api.yelu.ai/v1/models', {
        headers: { Authorization: `Bearer ${process.env.YELU_API_KEY}` },
      });
      if (response.ok) return response.json();
      if (response.status !== 429 || attempt === attempts - 1) throw new Error(await response.text());
      await sleep(500 * 2 ** attempt + Math.random() * 250);
    }
  }
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import os
  import random
  import time
  import requests

  for attempt in range(4):
      response = requests.get(
          "https://api.yelu.ai/v1/models",
          headers={"Authorization": f"Bearer {os.environ['YELU_API_KEY']}"},
          timeout=30,
      )
      if response.ok:
          print(response.json())
          break
      if response.status_code != 429 or attempt == 3:
          response.raise_for_status()
      time.sleep(0.5 * (2 ** attempt) + random.uniform(0, 0.25))
  ```
</CodeGroup>

## 降低限流压力

* 对突发任务排队并控制并发，不要同时释放所有 Worker。
* 对允许缓存的确定性结果进行应用层缓存。
* 为交互流量与批处理任务使用不同 Key。
* 严格限制重试次数，避免故障放大。
* 不要原样重试 `400`、`401`、`403` 与 `404`。

| 状态码                     | 是否重试 | 建议                   |
| ----------------------- | ---- | -------------------- |
| `408`                   | 通常可以 | 操作可安全重复时退避重试         |
| `429`                   | 可以   | 使用抖动退避并降低并发          |
| `500`、`502`、`503`、`504` | 可以   | 使用较小重试预算；回退模型需经过产品审批 |

更多信息参见[错误处理](/zh/errors)。
