Files
coder/aibridge/intercept/openai_errors.go
T
Susana Ferreira dba45cede7 fix: remove 403 from key failover and cooldown on 401 (#27419)
## Problem

When a key returned 401 or 403, the pool marked it permanently
unavailable for the lifetime of that in-memory pool. This is bad UX: a
transient auth failure or a briefly-misconfigured key could take a key
out of rotation until the operator either restarted Coder or
reconfigured the key (even re-saving the same working value).

## Changes

- **403 removed from key failover**: it's a per-request authorization
failure, not a key-level problem, so it's surfaced to the caller as-is
without marking the key or failing over.
- **401 now applies a temporary cooldown** (like 429) so the key
recovers on its own instead of staying blocked.
- When every key is in an auth-failure cooldown, the pool reports a
`502` with no `Retry-After`, but the keys still recover automatically
once the cooldown elapses.

Closes
https://linear.app/codercom/issue/AIGOV-421/ai-gateway-a-quarantined-centralized-key-never-recovers-without-a
Closes
https://linear.app/codercom/issue/AIGOV-533/403s-misclassifying-keys-as-permanently-down-in-ai-gateway

> [!NOTE]
> Initially generated by Claude Opus 4.7, modified and reviewed by
@ssncferreira
2026-07-27 12:06:01 +01:00

117 lines
3.3 KiB
Go

package intercept
import (
"encoding/json"
"errors"
"net/http"
"time"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/shared"
"github.com/coder/coder/v2/aibridge/keypool"
"github.com/coder/coder/v2/aibridge/utils"
)
// OpenAI error type and code constants used by the chatcompletions
// and responses interceptors. The OpenAI Go SDK does not expose
// these as typed constants, so we define our own.
// See https://platform.openai.com/docs/guides/error-codes.
const (
OpenAIErrTypeError = "error"
OpenAIErrTypeAPI = "api_error"
OpenAIErrTypeRateLimit = "rate_limit_error"
OpenAIErrCodeServer = "server_error"
OpenAIErrCodeRateLimit = "rate_limit_exceeded"
)
var _ error = &ResponseError{}
// ResponseError is the OpenAI-shaped error envelope returned to
// clients. StatusCode and RetryAfter map to HTTP headers, not JSON
// fields. The chatcompletions and responses interceptors both
// use the same response error format.
type ResponseError struct {
ErrorObject *shared.ErrorObject `json:"error"`
StatusCode int `json:"-"`
RetryAfter time.Duration `json:"-"`
}
// NewResponseError builds a ResponseError with the OpenAI-shaped
// envelope. errType and code should be one of the OpenAIErrType*
// and OpenAIErrCode* constants defined above.
func NewResponseError(msg, errType, code string, status int, retryAfter time.Duration) *ResponseError {
return &ResponseError{
ErrorObject: &shared.ErrorObject{
Code: code,
Message: msg,
Type: errType,
},
StatusCode: status,
RetryAfter: retryAfter,
}
}
func (e *ResponseError) Error() string {
if e.ErrorObject == nil {
return ""
}
return e.ErrorObject.Message
}
// ToResponse marshals e into an *http.Response shaped for the
// OpenAI API.
func (e *ResponseError) ToResponse() *http.Response {
body, err := json.Marshal(e)
if err != nil {
body = []byte(`{"error":{"type":"error","message":"error marshaling upstream error","code":"server_error"}}`)
}
return utils.NewJSONErrorResponse(e.StatusCode, e.RetryAfter, body)
}
// ResponseErrorFromKeyPool translates a *keypool.Error into
// a developer-facing ResponseError shaped for the OpenAI API.
func ResponseErrorFromKeyPool(keyPoolErr *keypool.Error) *ResponseError {
if keyPoolErr == nil {
return nil
}
switch keyPoolErr.Kind {
case keypool.ErrorKindPermanent, keypool.ErrorKindUnauthorized:
return NewResponseError(
keyPoolErr.Error(),
OpenAIErrTypeAPI,
OpenAIErrCodeServer,
http.StatusBadGateway,
0,
)
case keypool.ErrorKindRateLimited:
return NewResponseError(
keyPoolErr.Error(),
OpenAIErrTypeRateLimit,
OpenAIErrCodeRateLimit,
http.StatusTooManyRequests,
keyPoolErr.RetryAfter,
)
default:
// Fall back to a generic 502.
return NewResponseError(
keyPoolErr.Error(),
OpenAIErrTypeAPI,
OpenAIErrCodeServer,
http.StatusBadGateway,
keyPoolErr.RetryAfter,
)
}
}
// ResponseErrorFromAPIError converts an OpenAI SDK error into a
// ResponseError. Returns nil if err is not an *openai.Error.
func ResponseErrorFromAPIError(err error) *ResponseError {
var apiErr *openai.Error
if !errors.As(err, &apiErr) {
return nil
}
return NewResponseError(apiErr.Message, apiErr.Type, apiErr.Code, apiErr.StatusCode, keypool.ParseRetryAfter(apiErr.Response))
}