Skip to content

A request-level 400 (context length) is retried through every tier and reaches the client as a 401 #136

Closed

Description

@cheerc

Problem

A request-level rejection from the free tier is retried on every remaining lane
and then on the keyed tiers. The client ends up with whatever the last tier
answers — typically an authentication error — instead of the real cause.

A concrete case: a 941 KB / 715-message request whose last turn is the compact
instruction. The free tier answers 400 context_length_exceeded, which is
deterministic: no other key, lane or tier can make the context shorter. The
gateway still walked the anonymous lanes, then the Zen key tier, and returned
401 Invalid credential to Claude Code, which reads that as an auth problem
rather than as "compact".

The configuration in that case carried the documented placeholder
sk-your-zen-key in zen_keys (the user only ever uses the keyless free
tier). A placeholder can never authenticate, so the keyed fallback converted
the useful 400 into a useless 401 — and the placeholder was retried three
times.

Evidence

Reproduced locally by replaying the recorded request against a gateway built
from this branch's parent, with the same configuration:

attempt 1  anonymous  /zen        400 context_length_exceeded
attempt 2  anonymous  /inference  400 context_length_exceeded
           -> anonymous_fallback, key_tiers ["zen"]
attempt 1  key        /zen        401  (null-key placeholder)
attempt 2  key        /zen        401
attempt 3  key        /zen        401
client                            401  "Upstream request failed: Invalid credential"

Variables ruled out: the same request trimmed to 300/450/600/700 messages
returned 200, and a minimal request returned 200 at the same time, so neither
the egress nor the model was unavailable. Lowering max_tokens did not change
the outcome. The 400 body itself was captured by instrumenting the anonymous
loop in a scratch build:

{"error":{"message":"Your request resulted in more tokens than this model's
maximum context length allows. Please reduce the length of the messages or run
compaction.","type":"invalid_request_error","code":"context_length_exceeded"}}

Expected Outcome

  • A deterministic request rejection (400/422 with invalid_request_error,
    context_length_exceeded, maximum context length, string_above_max_length
    or prompt is too long) ends the request after the first attempt and reaches
    the client with its original status and body.
  • 401/403/429 keep their current retry behaviour: they can be specific to one
    key.
  • The documented placeholder keys never enter a key pool, so a free-tier-only
    configuration does not silently fall back into a keyed tier at all.

Scope

internal/gateway/upstream.go (doUpstreamTiers, doAnonymousUpstream,
doKeyUpstream), internal/gateway/abuse.go (classification), and
internal/config (placeholder key filtering during Normalize).

Related

Activity

  1. jasonxu114514 commented on Oct 20, 2026

    @jasonxu114514
    Owner

    Fixed in 0bb23c7 (merged to main). The anonymous walk now stops on a deterministic request-level 4xx before another lane, exit, or tier is tried, so the original 400/413/422 reaches the client. Placeholder keys are also dropped, so sk-your-zen-key can no longer turn a context-length 400 into a 401. Verified with a local harness: three exits, context-length 400 -> one upstream request and a 400 response.

  2. jauharymanshura9-max commented on Oct 21, 2026

    @jauharymanshura9-max

    v1.4.3 went out at 2026-10-20T10:20Z, about 8h before this landed, so the latest release still has the old behavior. On Oct 20 we hit the 6×400 → 503 walk four times for one session (the combo then fell through to the direct exit, got a 400, and ended). Is a patch release planned, or is pinning to main the expected move until then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions