跳到正文
DEV Community· Long AI·· 34 分鐘前AI 評分55

LONG AI 如何設計 API 閘道的重試與計費邊界

Who pays for a retry: designing failure and billing boundaries for an API gateway

AI 導讀

LONG AI 為其 API 閘道調整重試與計費規則,按上游故障階段設定重試策略,並只就成功完成的請求向客戶收費。連線拒絕可不限次重試;請求已被上游接受後逾時,最多重試一次並切換至不同上游模型組合;開始生成後出現上游 5xx 則預設不重試。其自有流量中,客戶端看到的失敗率由 15.98% 降至 2.74%;文中指出,變化在於閘道吸收了可內部重試的故障,而非可靠性改變。

正文

A retry looks free from the client's side. Inside a gateway it is the single most expensive design decision you make, because every retry is a request that somebody pays for — and it is not always the person who retried.

This is how we split that bill, and the mistakes that forced each rule.

Mistake one: retrying a request the upstream already billed

The naive design retries any failed upstream call. The problem is that "failed" arrives in three flavours, and only one of them means the upstream did no work:

  • Connection refused / pre-billing rejection. No bill. Retry freely.
  • Timeout after the request was accepted. The upstream may have generated the whole completion and simply failed to return it in time. You are billed. A retry pays twice.
  • Upstream 5xx after generation started in a stream. Tokens were produced. You are billed for whatever was generated before the connection dropped.

Our rule: retries are unlimited for the first class, capped at one for the second, and disabled by default for the third. For the second class, the retry goes to a different upstream-model pair, so a single seller having a bad minute does not double the bill.

Mistake two: charging the customer for our error

The customer's retry and our retry are different events. What the customer sees should be: a request that succeeded, and a bill for exactly one request.

So the gateway bills on successful completions, not on upstream attempts. That includes:

  • downstream retries we initiate, which are invisible in billing;
  • streaming requests where the upstream died mid-stream and we re-ran the whole thing;
  • requests that failed after partial generation, which are billed at zero to the client.

This is the line item that decides whether a marketplace's headline discount is real. A 30 percent cheaper channel that fails 5 percent of requests and bills those failures costs more than a channel at list price that bills only successes.

Mistake three: telling the client nothing

A silent retry that adds two seconds is indistinguishable from a slow upstream, and users conclude the gateway is slow rather than resilient. We surface it: the response carries the number of upstream attempts and the reason for the switch in a response header, and the dashboard shows the same data per request.

The numbers after the split

After the change, the effective failure rate seen by clients dropped from 15.98 percent to 2.74 percent without buying capacity, because most of those were retryable-internally events that we had previously surfaced as errors. What changed was not reliability; it was who absorbs the failure and who gets told about it.

The billing rule is written down where you can check it before you commit money: https://ai.dijinlong.com/faq

The compatibility surface, including what a partial stream looks like from the client's side: https://ai.dijinlong.com/docs

Per-model and per-seller quotes, because the retry policy and the price are both part of the deal: https://ai.dijinlong.com/model-plaza


Disclosure: I run LONG AI, the gateway described. The failure-rate figures are from our own traffic between 9 and 20 November 2026.

來源:DEV Community · dev.to