gateway: a vendor saying the conversation is too long is said the client's way, so the agent compacts and retries — Volcengine's "Input exceeds the context limit (1048568 tokens)" reached Claude Code and Codex as a plain error they stop on; a 4xx that says the input is past the model's context (context_length_exceeded, maximum context length, prompt is too long, exceeds the context limit, 超过上下文…) now answers 400 with Anthropic's "prompt is too long: …" on /v1/messages and code context_length_exceeded on chat and Responses; one about max_tokens is left alone

This commit is contained in:
yetone
2026-09-26 01:38:37 +08:00
parent c330e20231
commit 40812f1d2b
2 changed files with 86 additions and 1 deletions
+31 -1
View File
@@ -14,6 +14,7 @@ import (
"net"
"net/http"
"os"
"regexp"
"slices"
"sort"
"strings"
@@ -1112,6 +1113,15 @@ func writeError(w http.ResponseWriter, proto provider.Protocol, status int, msg
case status == 529:
typ = "overloaded_error"
}
var code any
if tooLong(status, msg) {
// said the way the client's own API says it, so the agent
// compacts the conversation and tries again rather than stopping
status, typ, code = 400, "invalid_request_error", "context_length_exceeded"
if proto == provider.Anthropic && !strings.Contains(strings.ToLower(msg), "prompt is too long") {
msg = "prompt is too long: " + msg
}
}
var v any
switch proto {
case provider.Anthropic:
@@ -1124,8 +1134,28 @@ func writeError(w http.ResponseWriter, proto provider.Protocol, status int, msg
}
v = map[string]any{"error": map[string]any{"code": status, "message": msg, "status": st}}
default:
v = map[string]any{"error": map[string]any{"message": msg, "type": typ, "code": nil, "param": nil}}
v = map[string]any{"error": map[string]any{"message": msg, "type": typ, "code": code, "param": nil}}
}
writeJSON(w, status, v)
return status
}
// tooLongRe matches how vendors say a request is more than the model's
// context holds: OpenAI's context_length_exceeded, Anthropic's "prompt is
// too long", Volcengine's "Input exceeds the context limit", "maximum
// context length", "context window"…
var tooLongRe = regexp.MustCompile(`(?i)context_length_exceeded|prompt is too long|input is too long|(exceeds?|exceeded|over|beyond)( the)?( model'?s?)?( maximum)? context|context (length|limit|window) (exceeded|is exceeded)|maximum context length|too many (input |prompt )?tokens|上下文(长度)?(超|过长)|超(过|出)(了)?(模型)?(的)?(最大)?上下文`)
// tooLong is whether a vendor's error says the conversation no longer
// fits. One about max_tokens is left alone: the reply's allowance, not
// the conversation, is what is too big there, and compacting won't help.
func tooLong(status int, msg string) bool {
if status < 400 || status >= 500 {
return false
}
m := strings.ToLower(msg)
if strings.Contains(m, "max_tokens") || strings.Contains(m, "max_output_tokens") || strings.Contains(m, "max_completion_tokens") {
return false
}
return tooLongRe.MatchString(msg)
}
+55
View File
@@ -0,0 +1,55 @@
package gateway
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A vendor's "too long" reaches the agent in its own API's words, so it
// compacts and retries: Claude Code on "prompt is too long", Codex and
// chat clients on context_length_exceeded.
func TestContextOverflowSaidTheClientsWay(t *testing.T) {
volc := "Volcengine: Input exceeds the context limit (1048568 tokens)"
for _, tc := range []struct {
proto provider.Protocol
status int
msg string
wantCode string
wantMsg string
}{
{provider.Anthropic, 400, volc, "", "prompt is too long: " + volc},
{provider.Chat, 400, volc, "context_length_exceeded", volc},
{provider.Responses, 413, volc, "context_length_exceeded", volc},
{provider.Responses, 400, "relay: This model's maximum context length is 131072 tokens. However, you requested 140000 tokens", "context_length_exceeded", ""},
{provider.Anthropic, 400, "a: prompt is too long: 210000 tokens > 200000 maximum", "", "a: prompt is too long: 210000 tokens > 200000 maximum"},
{provider.Chat, 400, "zhipu: 输入内容超过模型最大上下文长度", "context_length_exceeded", ""},
// not the conversation: left as the vendor said it
{provider.Chat, 400, "x: max_tokens is too large: 100000 exceeds the model's context", "", ""},
{provider.Chat, 400, "x: invalid tool schema", "", ""},
{provider.Chat, 502, "x: upstream exceeds context budget", "", ""},
} {
w := httptest.NewRecorder()
status := writeError(w, tc.proto, tc.status, tc.msg)
var body struct {
Error struct {
Type string `json:"type"`
Message string `json:"message"`
Code any `json:"code"`
} `json:"error"`
}
json.Unmarshal(w.Body.Bytes(), &body)
code, _ := body.Error.Code.(string)
overflow := tc.wantCode != "" || strings.HasPrefix(tc.wantMsg, "prompt is too long") || strings.Contains(tc.wantMsg, ": prompt is too long")
switch {
case tc.proto == provider.Anthropic && tc.wantMsg != "" && body.Error.Message != tc.wantMsg,
tc.proto != provider.Anthropic && code != tc.wantCode,
overflow && (status != 400 || body.Error.Type != "invalid_request_error"),
!overflow && status != tc.status:
t.Errorf("%s %d %q: status %d body %s", tc.proto, tc.status, tc.msg, status, w.Body.String())
}
}
}