# Gateway Error Codes & Troubleshooting

When calling LLMs via the API gateway, a failed response consists of an **HTTP status code** + a **JSON error body**. The external HTTP code is **consistent** with `status_code` in Console "Call Logs", making it easy to trace by `log_id`.

---

## 1. Unified Error Response Format

### Platform Local Interception (Auth, Balance, Rate Limits, etc.)

```json
{
  "error": {
    "message": "Insufficient account balance (below 0.5 CNY)",
    "type": "api_error",
    "code": "402"
  }
}
```

| Field | Description |
| :--- | :--- |
| `error.message` | Failure reason description |
| `error.type` | `api_error` for platform interceptions |
| `error.code` | **HTTP status code string** (e.g., `"402"`), not an OpenAI semantic error code |

### Upstream Pass-Through

When the upstream returns valid JSON, the error body may be **passed through verbatim** (including vendor-specific `type` / `code`), while the HTTP status code still aligns with `status_code` in logs.

---

## 2. Status Code Quick Reference

| HTTP | Meaning | Typical `message` |
| :---: | :--- | :--- |
| **400** | Invalid parameters, model type mismatch, **content moderation / copyright block** | Parameter error; `PolicyViolation` / `OutputVideoSensitiveContentDetected` / copyright restriction |
| **401** | Authentication failed | `Missing Authorization Header` / `Invalid Bearer Token Format` / `Invalid API Key` |
| **402** | **Insufficient available account balance** | `Insufficient account balance...` / `Insufficient balance` |
| **403** | Insufficient permissions (token / IP / model whitelist, etc.) | `API key disabled` / `Model xxx not allowed for this token` / `Daily quota exhausted` / `IP x.x.x.x not whitelisted` |
| **404** | Model not found, temporarily unavailable, or external resource inaccessible | `Model not found: xxx`; or message containing `status 404` (e.g., input image download failure) |
| **429** | **Rate limit or in-flight task cap** | `RPS limit exceeded` / `RPM limit exceeded` / `Balance too low, too many active tasks, please recharge` |
| **500** | Gateway internal error | `Internal server error` |
| **502** | Upstream unavailable / connection failed | `Upstream request failed` or raw upstream message |
| **504** | Timeout / gateway interruption | Contains `timeout`, `gateway`, etc. |

> **Commonly Confused**: Insufficient balance -> **402**; content moderation/copyright -> **400**; no token permission -> **403**; **429** only means RPS/RPM or low-balance in-flight task gating.

---

## 3. Troubleshooting by Scenario

### 400 Bad Request (Malformed Request / Type Error / Content Block)

* **Causes**: Invalid JSON, missing required fields, calling an image/video model on the chat endpoint; or upstream content safety/copyright/sensitivity policy block (e.g., `PolicyViolation`).
* **Resolution**: Check `model` and endpoint path; validate JSON. For content moderation blocks, adjust your prompt or reference assets—this is **not** a token permission issue (see 403 for permissions).

### 401 Unauthorized (Authentication Failed)

* **Causes**: Missing `Authorization`, malformed Bearer header, or nonexistent Key.
* **Resolution**: `Authorization: Bearer sk-xxx` (with a single space). Disabled/expired tokens return **403**.

### 402 Payment Required (Insufficient Balance)

* **Causes**: Available balance is insufficient to cover pre-deduction or minimum charge.
* **Resolution**: Top up in the Console. Async task **GET polling / DELETE cancellation** are still permitted when overdrawn, while **POST submissions** are blocked.

### 403 Forbidden (Permissions / Token Quota)

* **Causes**:
  - Token disabled (`API key disabled`), expired (`API key expired`)
  - Model not in token whitelist (`Model xxx not allowed for this token`)
  - IP not in whitelist (`IP x.x.x.x not whitelisted`)
  - Token daily/weekly/monthly/total quota exhausted (e.g., `Daily quota exhausted`)
  - Creation Center dedicated token invoked outside the Playground environment
* **Resolution**: Check token switch, model/IP whitelists, and quotas. Content moderation/copyright blocks return **400**, not 403.

### 404 Not Found (Model or Resource Missing)

* **Causes**: The model ID does not exist or is currently unavailable; or an input resource URL (such as an image or video link) cannot be downloaded (contains `status 404`).
* **Resolution**: Verify the model ID matches the Model Plaza; check that image/video URLs in the request are publicly accessible.

### 429 Too Many Requests (Rate Limit / In-Flight Limit)

* **Causes**: Token `rps_limit` / `rpm_limit` exceeded; too many concurrent in-flight async tasks of the same type when balance is low.
* **Resolution**: Retry with exponential backoff; top up or reduce concurrent async tasks.

### 500 / 502 / 504 (Service or Upstream Errors)

* **502**: Upstream returned a non-2xx HTTP status or connection failed.
* **504**: Connection or generation timeout.
* **500**: Temporary server error.
* **Resolution**: Retry later and check `error_message` in Call Logs.

---

## 4. Special Notes for Async Tasks

For video and other long-running tasks: HTTP may be **200** when **submission and polling** succeed at the transport layer; check `status` in the body for business success/failure:

```json
{
  "id": "video_task_xxx",
  "status": "failed",
  "error": { "message": "Upstream failure reason" }
}
```

Logs record the inferred 4xx/5xx in `status_code`, while the polling endpoint itself may still return 200. Always rely on `status` and the logs.

---

## 5. Log Tracing

1. **Logs**: Every request records `log_id`, `status_code`, `error_message`, latency, and billing; filter by timestamp and `log_id` during troubleshooting. The HTTP code matches `status_code`.
2. **Processing Status**: After a successful async submission, the log may briefly show `status_code=0` (processing) and update to its terminal state once the task completes.

---

## 6. Capability-Specific Notes

Common error scenarios by endpoint type:

| Capability | Common Symptom | Code / Explanation |
| :--- | :--- | :--- |
| **Chat** | Context too long, invalid parameters | May return an OpenAI-style `error` or platform `api_error` + HTTP code |
| **Image** | Model type mismatch with `/v1/images/generations` | **400**; same for invalid multi-image / reference image parameters |
| **Qwen Image** | Malformed `size`, invalid reference image URL | **400**; `1024x1024` is accepted and auto-converted by the gateway to `1024*1024` |
| **Video (Async)** | Submit succeeds but polling returns `status: failed` | Polling HTTP **200**; log `status_code` is 4xx/5xx, see Section 4 |
| **Wan Video** | Mixing start/end frames with reference assets, or audio-only without image/video | **400** |
| **Wan Video** | Mixing `files` / `links` with start/end frame roles | **400** |
| **Video (Sync Block)** | Insufficient balance, rate limit, model unavailable | **402 / 429 / 404** consistent with the table above |
| **Native Routes** | Misspelled DashScope / Volcengine path | **404** or **400**; verify the path and model name |
