fix(codex-bridge): estimate Claude requests more strictly; report an unexplained 400 on a near-full request

A 607 KB Codex request was estimated at 189776 tokens (window 200000), sent untrimmed and rejected by
Claude; the gateway only said "upstream 400", so Codex saw a dropped stream. Claude targets now use
2.6 bytes/token, and that bare 400 on a request filling most of the window becomes context_length_exceeded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yilsemandClaude Opus 5.5 committed 2026-10-11 00:45:58 +03:00
1 parent 959f5f1459
commit d9a7661281
3 files changed
+46 -7

No files matched your search

+19 -5
View File
@@ -141,6 +141,11 @@ def ogren_aracsiz(home, kimlik):
return True
def kapali_hata_mi(metin):
"""Gecit asil nedeni gizleyip yalniz "upstream 400" derse (olcum 11 Ekim 2026)."""
return bool(re.fullmatch(r"\s*upstream 4(?:00|13)\s*", metin or "", re.I))
def tasma_mi(metin):
return bool(_TASMA.search(metin or ""))
@@ -243,8 +248,17 @@ KIRP_ESIK = 6000 # bundan kisa metin parcasi kirpilmaz
KIRP_BAS = KIRP_SON = 1500
def tahmini_token(req):
return int(len(json.dumps(req, ensure_ascii=False).encode("utf-8")) / BAYT_BOLU_TOKEN)
# Claude ayni metni daha cok tokena boler. Olcum (11 Ekim 2026): 607 KB'lik gercek Codex istegi 3,2 oraniyla
# 189776 token tahmin edildi (pencere 200000) ve Claude tarafindan reddedildi; yani gercek oran 3,03'un altinda.
CLAUDE_BAYT_BOLU_TOKEN = 2.6
def oran_sec(hedef):
return CLAUDE_BAYT_BOLU_TOKEN if "claude" in str(hedef or "").lower() else BAYT_BOLU_TOKEN
def tahmini_token(req, oran=None):
return int(len(json.dumps(req, ensure_ascii=False).encode("utf-8")) / (oran or BAYT_BOLU_TOKEN))
def _metin_parcalari(req):
@@ -271,10 +285,10 @@ def _metin_parcalari(req):
return parcalar
def sigdir(req, pencere_token):
def sigdir(req, pencere_token, oran=None):
"""Istegi hedef pencereye sigdirir. Donus: (kirpilan parca sayisi, sigdi mi, tahmini token)."""
sinir = int(pencere_token * DOLULUK)
tahmin = tahmini_token(req)
tahmin = tahmini_token(req, oran)
if not pencere_token or tahmin <= sinir:
return 0, True, tahmin
kirpilan = 0
@@ -286,7 +300,7 @@ def sigdir(req, pencere_token):
sahip[anahtar] = (metin[:KIRP_BAS] + f"\n[... {len(metin) - KIRP_BAS - KIRP_SON} karakter çıkarıldı (köprü): "
f"seçili modelin bağlam penceresine sığmıyordu ...]\n" + metin[-KIRP_SON:])
kirpilan += 1
tahmin = tahmini_token(req)
tahmin = tahmini_token(req, oran)
if tahmin <= sinir:
return kirpilan, True, tahmin
return kirpilan, tahmin <= sinir, tahmin