Skip to content
This repository was archived by the owner on Nov 19, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ python3 check-relay.py --base-url https://中转地址 --key sk-你的密钥 --m
```text
https://中转地址/v1/messages model=claude-sonnet-5-5
[PASS] shape Messages 格式正确,返回模型 claude-sonnet-5-5,输入 143 token
[PASS] cache 首次写入 18473 token,第 2 次读取 18473 token
[PASS] cache 首次写入 18165 token,第 2 次读取 18165 token
[PASS] signature 篡改签名被拒(HTTP 400),原样回传通过
结论:三项都像 Anthropic 官方
```
Expand Down
38 changes: 30 additions & 8 deletions check-relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,42 @@ def check_shape(relay, model):

def long_prefix():
# About 18,000 tokens on Claude 5: above every Claude model's minimum cacheable length.
# The nonce makes this run's prefix new, so the first call has to write it. Each line
# carries its own random value: on 2026-11-19, Sonnet 5.5 returned a 400 for a system
# prompt of ten or more near-identical lines.
nonce = secrets.token_hex(8)
lines = [f"Reference line {i:04d} of run {nonce}: {secrets.token_hex(16)}" for i in range(400)]
return "You are a terse assistant. Answer in one word.\n" + "\n".join(lines)
# The nonce and the random sums make this run's prefix new, so the first call has to
# write it. Sums, not filler: in November 2026, Anthropic refused meaningless system text
# with a 400 on some models (numbered hex lines on Sonnet 5.5 in 3 to 7 calls of 10,
# random words on Haiku 5.5 in 8 of 10), and refused these sums on none of 120 calls across four models.
lines = []
for _ in range(1200):
a, b = secrets.randbelow(9000) + 1000, secrets.randbelow(900) + 100
lines.append(f"{a} plus {b} equals {a + b}.")
return f"You are a terse assistant. Answer in one word. Run {secrets.token_hex(8)}.\n" + "\n".join(lines)


# A refusal of the key or the route will not change on a second try.
FINAL = {401, 403, 404}


def post_retrying(relay, body, tries=3):
"""(status, data, raw, failed calls). Repeats a call the upstream refused for some
other reason: in November 2026, Anthropic sometimes answered a cache call with a 400
that the same request on another account did not get. Retrying cannot fake a cache
write or read, so it costs a few calls and never a verdict."""
for failed in range(tries):
status, data, raw = relay.post(body)
if (status == 200 and isinstance(data, dict)) or status in FINAL or failed == tries - 1:
return status, data, raw, failed
time.sleep(3)


def check_cache(relay, model):
body = {"model": model, "max_tokens": 8,
"system": [{"type": "text", "text": long_prefix(), "cache_control": {"type": "ephemeral"}}],
"messages": [{"role": "user", "content": "Reply with: ok"}]}
usages = []
retried = 0
for attempt in range(3):
status, data, raw = relay.post(body)
status, data, raw, failed = post_retrying(relay, body)
retried += failed
if status != 200 or not isinstance(data, dict):
return ERROR, t(f"请求失败:HTTP {status} {error_text(data, raw)}", f"request failed: HTTP {status} {error_text(data, raw)}")
usages.append(data.get("usage") or {})
Expand All @@ -126,7 +147,8 @@ def check_cache(relay, model):
f"wrote {written} cached tokens but read none in {len(usages) - 1} retries: requests may land on different accounts, so the cache keeps missing")
if written and abs(read - written) > written * 0.05:
return WARN, t(f"写入 {written}、读取 {read} token,两者对不上", f"wrote {written} but read {read} cached tokens; they should match")
return PASS, t(f"首次写入 {written} token,第 {len(usages)} 次读取 {read} token", f"wrote {written} tokens, read {read} on call {len(usages)}")
note = t(f"({retried} 次请求失败后重试成功)", f" (after {retried} failed call(s) were retried)") if retried else ""
return PASS, t(f"首次写入 {written} token,第 {len(usages)} 次读取 {read} token", f"wrote {written} tokens, read {read} on call {len(usages)}") + note


TOOL = {"name": "get_weather", "description": "Current weather for a city.",
Expand Down
43 changes: 36 additions & 7 deletions test_check_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import re
import threading
import unittest
from collections import Counter
from contextlib import redirect_stdout
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
Expand All @@ -27,9 +26,12 @@ class Upstream:
verify_signatures = True
cache = True
same_account = True
refuse_cache_calls = 0
refuse_status = 400

def __init__(self):
self.cached = set()
self.cache_calls = 0

def sign(self, text):
return hmac.new(self.secret, text.encode(), hashlib.sha256).hexdigest() * 2
Expand All @@ -42,8 +44,13 @@ def respond(self, body):
return 400, {"type": "error", "error": {"type": "invalid_request_error", "message": "Invalid signature in thinking block"}}
usage = {"input_tokens": 12, "output_tokens": 3}
system = body.get("system")
if system and repeats_lines(system[0]["text"]):
# Sonnet 5.5 on corecode.live, 2026-11-19: ten or more near-identical system lines got a 400.
if system:
self.cache_calls += 1
if self.cache_calls <= self.refuse_cache_calls:
# Sonnet 5.5, November 2026: a cache call now and then gets a bare 400.
return self.refuse_status, {"type": "error", "error": {"message": "Error"}}
if system and coded_lines(system[0]["text"]):
# Sonnet 5.5, November 2026: system lines of hex codes got a 400 on 3 to 7 calls in 10.
return 400, {"type": "error", "error": {"type": "invalid_request_error", "message": "Error"}}
if system and self.cache:
prefix = system[0]["text"]
Expand All @@ -62,10 +69,9 @@ def respond(self, body):
"content": content, "usage": usage}


def repeats_lines(text):
"""Ten or more lines that differ only in a number."""
counts = Counter(re.sub(r"\d+", "#", line) for line in text.splitlines())
return max(counts.values()) >= 10
def coded_lines(text):
"""Ten or more lines carrying a run of 16 or more hex digits."""
return sum(bool(re.search(r"[0-9a-f]{16,}", line)) for line in text.splitlines()) >= 10


def serve(upstream):
Expand Down Expand Up @@ -129,6 +135,29 @@ def test_cache_on_another_account_warns(self):
self.assertEqual(lines["cache"], "[WARN]")
self.assertEqual(code, 0)

def test_cache_retries_a_refused_call(self):
upstream = Upstream()
upstream.refuse_cache_calls = 2
code, lines = self.run_against(upstream)
self.assertEqual(lines["cache"], "[PASS]")
self.assertEqual(code, 0)

def test_cache_gives_up_after_three_refusals(self):
upstream = Upstream()
upstream.refuse_cache_calls = 99
code, lines = self.run_against(upstream)
self.assertEqual(lines["cache"], "[ERROR]")
self.assertEqual(upstream.cache_calls, 3)
self.assertEqual(code, 2)

def test_cache_does_not_retry_a_refused_key(self):
upstream = Upstream()
upstream.refuse_cache_calls = 99
upstream.refuse_status = 403
code, lines = self.run_against(upstream)
self.assertEqual(lines["cache"], "[ERROR]")
self.assertEqual(upstream.cache_calls, 1)

def test_non_anthropic_shape_fails(self):
upstream = Upstream()
upstream.respond = lambda body: (200, {"id": "chatcmpl-1", "object": "chat.completion", "choices": []})
Expand Down
Loading