errors.mdx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ---
  2. title: "Errors"
  3. sidebarTitle: "Errors"
  4. description: "Error codes, error formats, mid-stream errors, and retry strategies for the Cline API."
  5. ---
  6. The Cline API returns errors in a consistent JSON format. Understanding these errors helps you build reliable integrations.
  7. ## Error Format
  8. All errors follow the OpenAI error format:
  9. ```json
  10. {
  11. "error": {
  12. "code": 401,
  13. "message": "Invalid API key",
  14. "metadata": {}
  15. }
  16. }
  17. ```
  18. | Field | Type | Description |
  19. |-------|------|-------------|
  20. | `code` | number/string | HTTP status code or error identifier |
  21. | `message` | string | Human-readable description of the error |
  22. | `metadata` | object | Additional context (provider details, request IDs) |
  23. ## Error Codes
  24. ### HTTP Errors
  25. These are returned as the HTTP response status code and in the error body:
  26. | Code | Name | Cause | What to do |
  27. |------|------|-------|------------|
  28. | `400` | Bad Request | Malformed request body, missing required fields | Check your JSON syntax and required parameters |
  29. | `401` | Unauthorized | Invalid or missing API key | Verify your API key in the `Authorization` header |
  30. | `402` | Payment Required | Insufficient credits | Add credits at [app.cline.bot](https://app.cline.bot) |
  31. | `403` | Forbidden | Key does not have access to this resource | Check key permissions |
  32. | `404` | Not Found | Invalid endpoint or model ID | Verify the URL and model ID format |
  33. | `429` | Too Many Requests | Rate limit exceeded | Wait and retry with exponential backoff |
  34. | `500` | Internal Server Error | Server-side issue | Retry after a short delay |
  35. | `502` | Bad Gateway | Upstream provider error | Retry after a short delay |
  36. | `503` | Service Unavailable | Service temporarily down | Retry after a short delay |
  37. ### Mid-Stream Errors
  38. When streaming, errors can occur after the response has started. These appear as a chunk with `finish_reason: "error"`:
  39. ```json
  40. {
  41. "choices": [
  42. {
  43. "finish_reason": "error",
  44. "error": {
  45. "code": "context_length_exceeded",
  46. "message": "The input exceeds the model's maximum context length."
  47. }
  48. }
  49. ]
  50. }
  51. ```
  52. Common mid-stream error codes:
  53. | Code | Meaning |
  54. |------|---------|
  55. | `context_length_exceeded` | Input tokens exceed the model's context window |
  56. | `content_filter` | Content was blocked by a safety filter |
  57. | `rate_limit` | Rate limit hit during generation |
  58. | `server_error` | Upstream provider failed during generation |
  59. <Warning>
  60. Mid-stream errors do not produce an HTTP error code (the connection was already 200 OK). Always check `finish_reason` in your streaming handler.
  61. </Warning>
  62. ## Retry Strategies
  63. ### Exponential Backoff
  64. For transient errors (429, 500, 502, 503), retry with exponential backoff:
  65. ```python
  66. import time
  67. import requests
  68. def call_api_with_retry(payload, max_retries=3):
  69. for attempt in range(max_retries):
  70. response = requests.post(
  71. "https://api.cline.bot/api/v1/chat/completions",
  72. headers={
  73. "Authorization": "Bearer YOUR_API_KEY",
  74. "Content-Type": "application/json",
  75. },
  76. json=payload,
  77. )
  78. if response.status_code == 200:
  79. return response.json()
  80. if response.status_code in (429, 500, 502, 503):
  81. delay = (2 ** attempt) + 1
  82. print(f"Retrying in {delay}s (attempt {attempt + 1}/{max_retries})")
  83. time.sleep(delay)
  84. continue
  85. # Non-retryable error
  86. response.raise_for_status()
  87. raise Exception("Max retries exceeded")
  88. ```
  89. ### When to Retry
  90. | Error | Retry? | Strategy |
  91. |-------|--------|----------|
  92. | `401 Unauthorized` | No | Fix your API key |
  93. | `402 Payment Required` | No | Add credits |
  94. | `429 Too Many Requests` | Yes | Exponential backoff (start at 1s) |
  95. | `500 Internal Server Error` | Yes | Retry once after 1s |
  96. | `502 Bad Gateway` | Yes | Retry up to 3 times with backoff |
  97. | `503 Service Unavailable` | Yes | Retry up to 3 times with backoff |
  98. | Mid-stream `error` | Depends | Retry the full request for transient errors |
  99. ### Rate Limits
  100. If you hit rate limits frequently:
  101. - Add delays between requests
  102. - Reduce the number of concurrent requests
  103. - Contact support if you need higher limits
  104. ## Debugging
  105. When reporting issues, include:
  106. 1. The **error code and message** from the response
  107. 2. The **model ID** you were using
  108. 3. The **request ID** (from the `x-request-id` response header, if available)
  109. 4. Whether the error was **immediate** (HTTP error) or **mid-stream** (finish_reason error)
  110. ## Related
  111. <CardGroup cols={2}>
  112. <Card title="Chat Completions" icon="message" href="/api/chat-completions">
  113. Endpoint reference with request and response schemas.
  114. </Card>
  115. <Card title="Authentication" icon="key" href="/api/authentication">
  116. Verify your API key is configured correctly.
  117. </Card>
  118. </CardGroup>