Report API spend limit errors by scope (#62329)

## Why

Generic quota errors hide whether the project or organization spend limit
blocked a request. Reporting the scope tells users which limit to check.

## What changed

Recognize API key, project, and organization spend limit errors across
HTTP, SSE, and WebSocket responses. Show a message that names the scope and
treat the error as non-retryable, even when the provider sends
`Retry-After`. Apply the existing usage-limit handling to goals and
compaction.

Expose distinct `CodexErrorInfo` values in the app-server protocol and
generated bindings so clients can identify the limit in live errors and
failed turn history.

## Testing

Extend the transport tests to cover all three scopes and verify that they
stop retries. Extend the app-server test to check that the error survives
`thread/read` and that a subsequent turn can succeed in the same thread.

GitOrigin-RevId: 03341f7a78daa7540e7baa519f1a29ec07e34509
This commit is contained in:
Alex Chang
2026-11-18 18:11:54 +00:00
committed by copyberry
parent 0ba514837d
commit cc7a62e19c
47 changed files with 291 additions and 60 deletions
+1
View File
@@ -710,6 +710,7 @@ async fn process_sse_with_treatment(
| ApiError::ContextWindowExceeded
| ApiError::InvalidRequest { .. }
| ApiError::SessionBudgetExceeded
| ApiError::SpendLimitExceeded(_)
| ApiError::InferenceHookDenied { .. }
| ApiError::AccessProgramNotEnabled { .. }
| ApiError::ContentPolicyViolation { .. }
@@ -45,12 +45,7 @@ pub(super) fn parse_failed_response(response: Option<Value>) -> ApiError {
};
match error.code.as_deref() {
Some(
"insufficient_quota"
| "credit_balance_exhausted"
| "organization_spend_limit_exceeded"
| "project_spend_limit_exceeded",
) => ApiError::QuotaExceeded,
Some("insufficient_quota" | "credit_balance_exhausted") => ApiError::QuotaExceeded,
Some("usage_not_included") => ApiError::UsageNotIncluded,
Some("cyber_policy") => ApiError::CyberPolicy {
message: cyber_policy_message(error.message),
@@ -115,6 +115,18 @@ async fn terminal_failure_with_retry_header_ends_stream_immediately() {
for (code, expected) in [
("flex_unavailable", CodexErrorInfo::FlexUnavailable),
("budget_reached", CodexErrorInfo::SessionBudgetExceeded),
(
"api_key_spend_limit_exceeded",
CodexErrorInfo::ApiKeySpendLimitExceeded,
),
(
"project_spend_limit_exceeded",
CodexErrorInfo::ProjectSpendLimitExceeded,
),
(
"organization_spend_limit_exceeded",
CodexErrorInfo::OrganizationSpendLimitExceeded,
),
("inference_hook_denied", CodexErrorInfo::InferenceHookDenied),
(
"context_length_exceeded",