跳轉到主要內容

概覽

LemonData 的 Agent-First API 在錯誤回應中豐富了結構化提示,AI Agent 可以立即解析並採取行動——無需網頁搜尋、無需查閱文件、無需猜測。 每個錯誤回應在標準的 error 物件中包含選填欄位,如 did_you_meansuggestionshintretryableretry_after。這些欄位是向後相容的——不使用它們的用戶端不會看到任何差異。

錯誤提示欄位

所有提示欄位都是 error 物件內的選填擴充:
欄位類型描述
did_you_meanstring最接近匹配的模型名稱
suggestionsarray帶有元數據的推薦模型
alternativesarray目前可用的替代模型
hintstring人類/Agent 可讀的下一步指引
retryableboolean重試相同的請求是否可能成功
retry_afternumber重試前需等待的秒數
balance_usdnumber目前帳戶餘額(美元)
estimated_cost_usdnumber失敗請求的預估成本

錯誤代碼範例

model_not_found (400)

當模型名稱與任何活動模型都不匹配時:
{
  "error": {
    "message": "Model 'gpt5' not found",
    "type": "invalid_request_error",
    "param": "model",
    "code": "model_not_found",
    "did_you_mean": "gpt-4o",
    "suggestions": [
      {"id": "gpt-4o"},
      {"id": "gpt-4o-mini"},
      {"id": "claude-sonnet-4-5"}
    ],
    "hint": "Did you mean 'gpt-4o'? Use GET /v1/models to list all available models."
  }
}
did_you_mean 的解析使用:
  1. 靜態別名映射(來自生產環境錯誤數據)
  2. 標準化字串匹配(去除連字號,不區分大小寫)
  3. 編輯距離匹配(閾值 ≤ 3)

insufficient_balance (402)

當帳戶餘額低於預估成本時:
{
  "error": {
    "message": "Insufficient balance: need ~$0.3500 for claude-sonnet-4-5, but balance is $0.1200.",
    "type": "insufficient_balance",
    "code": "insufficient_balance",
    "balance_usd": 0.12,
    "estimated_cost_usd": 0.35,
    "suggestions": [
      {"id": "gpt-4o-mini"},
      {"id": "deepseek-chat"}
    ],
    "hint": "Insufficient balance: need ~$0.3500 for claude-sonnet-4-5, but balance is $0.1200. Try a cheaper model, or top up at https://lemondata.cc/dashboard/billing."
  }
}
suggestions 包含比預估成本更便宜的模型,Agent 可以切換至這些模型。

all_channels_failed (503)

當模型的所有上游通道都不可用時:
{
  "error": {
    "message": "Model claude-opus-4-6 temporarily unavailable",
    "code": "all_channels_failed",
    "retryable": true,
    "retry_after": 30,
    "alternatives": [
      {"id": "claude-sonnet-4-5", "status": "available", "tags": []},
      {"id": "gpt-4o", "status": "available", "tags": []}
    ],
    "hint": "All channels for 'claude-opus-4-6' are temporarily unavailable. Retry in 30s or try an alternative model."
  }
}
當原因為 no_channels(未為此模型配置通道)時,retryablefalse。僅在發生斷路器觸發或配額耗盡等瞬時故障時,它才為 true

rate_limit_exceeded (429)

{
  "error": {
    "message": "Rate limit: 60 rpm exceeded",
    "type": "rate_limit_error",
    "code": "rate_limit_exceeded",
    "retryable": true,
    "retry_after": 8,
    "hint": "Rate limited. Retry after 8s. Current limit: 60/min for user role."
  }
}
retry_after 的值是根據實際速率限制窗口的重置時間計算的。

context_length_exceeded (400)

當輸入超過模型的上下文窗口時(上游錯誤,並增加了提示資訊):
{
  "error": {
    "message": "This model's maximum context length is 128000 tokens...",
    "type": "invalid_request_error",
    "code": "context_length_exceeded",
    "retryable": false,
    "suggestions": [
      {"id": "gemini-2.5-pro"},
      {"id": "claude-sonnet-4-5"}
    ],
    "hint": "Reduce your input or switch to a model with a larger context window."
  }
}

原生端點標頭

當您使用具有原生端點(Anthropic 或 Gemini)的模型呼叫 /v1/chat/completions 時,成功回應會包含優化標頭:
X-LemonData-Hint: This model supports native Anthropic format. Use POST /v1/messages for better performance (no format conversion).
X-LemonData-Native-Endpoint: /v1/messages
模型提供商建議端點效益
Anthropic (Claude)/v1/messages無格式轉換、擴展思考、提示詞快取
Google (Gemini)/v1beta/gemini無格式轉換、實證 (grounding)、安全設定
OpenAIChat completions 已經是原生格式
這些標頭會同時出現在串流與非串流回應中。

/v1/models 增強功能

每個模型物件的 lemondata 擴充功能中有三個新欄位:
{
  "id": "gpt-4o",
  "lemondata": {
    "category": "chat",
    "pricing_unit": "per_token",
    "cache_pricing": {
      "cache_read_per_1m": "1.25",
      "cache_write_per_1m": "2.50",
      "platform_cache_discount": 0.9
    }
  }
}
欄位描述
categorychat, image, video, audio, tts, stt, 3d, embedding, rerank模型類型
pricing_unitper_token, per_image, per_second, per_request模型的計費方式
cache_pricing物件或 null上游提示詞快取價格 + 平台語義快取折扣

類別篩選

GET /v1/models?category=chat          # 僅限聊天模型
GET /v1/models?category=image         # 圖像生成模型
GET /v1/models?tag=coding&category=chat  # 針對程式碼優化的聊天模型

llms.txt

機器可讀的 API 概覽可在以下位置取得:
GET https://api.lemondata.cc/llms.txt
它包含:
  • 帶有工作範例的首次呼叫模板
  • 常見模型名稱(根據使用數據動態生成)
  • 所有 12 個 API 端點
  • 用於模型發現的篩選參數
  • 錯誤處理指引
在第一次 API 呼叫前閱讀 llms.txt 的 AI Agent 通常可以在第一次嘗試時就成功。

Agent 程式碼中的用法

Python (OpenAI SDK)

from openai import OpenAI, BadRequestError

client = OpenAI(
    api_key="sk-your-key",
    base_url="https://api.lemondata.cc/v1"
)

def smart_chat(messages, model="gpt-4o"):
    try:
        return client.chat.completions.create(
            model=model, messages=messages
        )
    except BadRequestError as e:
        error = e.body.get("error", {}) if isinstance(e.body, dict) else {}
        # 使用 did_you_mean 進行自動修正
        if error.get("code") == "model_not_found" and error.get("did_you_mean"):
            return client.chat.completions.create(
                model=error["did_you_mean"], messages=messages
            )
        raise

JavaScript (OpenAI SDK)

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'sk-your-key',
  baseURL: 'https://api.lemondata.cc/v1'
});

async function smartChat(messages, model = 'gpt-4o') {
  try {
    return await client.chat.completions.create({ model, messages });
  } catch (error) {
    const err = error?.error;
    if (err?.code === 'model_not_found' && err?.did_you_mean) {
      return client.chat.completions.create({
        model: err.did_you_mean, messages
      });
    }
    throw error;
  }
}

設計原則

快速失敗,提供資訊

錯誤會立即返回 Agent 自我修正所需的所有數據。

無自動路由

API 絕不會靜默替換不同的模型。由 Agent 決定。

數據驅動的建議

所有推薦均來自生產數據,而非硬編碼列表。

向後相容

所有提示欄位均為選填。現有用戶端不會看到任何差異。