Skip to content

fix(sail): honor completion windows and preserve served-tier billing - #46527

Open
kanand77 wants to merge 10 commits into
BerriAI:mainfrom
kanand77:fix/sail-completion-window-rules
Open

kanand77 wants to merge 10 commits into
BerriAI:mainfrom
kanand77:fix/sail-completion-window-rules

Conversation

@kanand77

@kanand77 kanand77 commented Oct 14, 2026 •

Copy link
Copy Markdown

TLDR

Problem this solves:

  • Caller-selected windows disappear on Chat and Messages
  • Unsupported Flex requests can run at another price
  • Streamed Messages lose the tier used for billing

How it solves it:

  • Forward and validate the caller's completion window
  • Preserve the returned tier through streaming and cost calculation

Intentional product change: Chat and Messages reject Flex even with drop_params, because dropping that choice can change the requested price. Flex requires native background Responses

User Flow

Before, a developer's Flex request on Messages succeeds and the gateway reports the ASAP price

  1. They send POST http://localhost:4012/v1/messages with metadata.completion_window: "flex"
  2. They receive HTTP 200 instead of an unsupported-window error
  3. The x-litellm-response-cost header reports the ASAP rate

After, the same unsupported window gets an explicit rejection

  1. They send POST http://localhost:4011/v1/messages with metadata.completion_window: "flex"
  2. They receive HTTP 400 explaining that Flex requires background Responses
  3. The cost header is zero, and they can choose Balanced or native background Responses

Behavior and scope

Chat and Messages accept metadata.completion_window. Messages also accepts the same LiteLLM service_tier mapping as Chat and Responses. Conflicting tier and metadata choices return an explicit error. Only caller string metadata is forwarded, excluding the proxy's internal metadata

Responses rejects foreground Flex and background ASAP unless parameter dropping was explicitly enabled. Dropping an incompatible Responses window also removes its requested billing tier. default and priority select ASAP, and auto lets Sail choose

Billing prefers Sail's reported tier. An explicit caller window provides the fallback for older responses. Streamed Messages retain usage.service_tier when their usage is collected

Explicit additional_drop_params removals are applied before billing parameters are captured, so a removed tier or metadata window cannot survive as a discounted billing fallback

This consolidates and supersedes #43477. That stale draft's synchronous Flex success cases contradict the current API and must not be merged separately

Merge with main

Current head 713bc1c7 merges main at 2e5cc9e0 to clear conflicts. The behavior described above is unchanged. Three conflicts were resolved:

  • The Anthropic Messages handler moved from llms/anthropic/pass_through to llms/anthropic/messages/native. Main's new MCP gateway call now passes the Anthropic-validated metadata, like the other Anthropic calls. Sail's request metadata is combined with main's tool cache-control marking
  • Main now applies additional_drop_params before the billing params are logged, into request_params. This branch's separate drop step is removed, and the logged billing params now come from request_params, so a dropped tier still cannot be billed
  • In main.py, the Sail caller-metadata lines move into a helper. completion() is at basedpyright's complexity limit, and the inline branches pushed it over ("Code is too complex to analyze")

Validation

The PR is ready for review. CI has not run on 713bc1c7: its 11 workflow runs are waiting for a maintainer to approve runs from this fork. The previous head 8c81a0a8 passed all 88 checks, with 100% Codecov diff coverage (unit run)

At 713bc1c7, 3,092 tests pass locally across the Sail, Anthropic Messages, HTTP handler, cost calculation and main suites. Six tests and one test module fail locally only because optional packages (tenacity, anthropic, google-cloud-speech) are not installed. The anthropic and google-cloud-speech failures were confirmed on unmodified main; the tenacity ones stop at the missing import. Strict Ruff, type discipline, test-quality and full basedpyright delta gates pass against 2e5cc9e0, and changed-file formatting is clean

Twelve regression cases cover dropped tiers, whole and nested metadata removals, and the remaining window on collected and streamed Messages

Veria's stale-tier finding has a pushed fix, but its thread is still open and its only review was submitted before that fix. Greptile automatic reviews are disabled

The live proxy proof below was captured at 8c81a0a8, before the merge. A rerun on 2026-10-29 could not reach a model: Sail staging returned nothing within 240 seconds for synchronous inference, including direct calls without LiteLLM. Its model list and background request creation responded normally. So the Chat and dropped-tier Messages cases are pending a staging or production rerun. The two cases LiteLLM rejects before calling Sail were rerun at 713bc1c7 and are shown under "Rerun after the merge"

Screenshots / Proof of Fix

Both proxies use sail/zai-org/GLM-5.3-Flash against the real staging API, with a local-only master key. Port 4012 runs upstream b9079755, also this branch's merge base. Port 4011 runs 8c81a0a8. Credentials are provided through environment variables and are omitted here

Chat payload, saved as chat.json

{"model":"sail-flash","max_tokens":64,"messages":[{"role":"user","content":"Reply with exactly OK."}],"reasoning_effort":"minimal","metadata":{"completion_window":"balanced"}}

Messages payload, saved as messages.json

{"model":"sail-flash","max_tokens":64,"messages":[{"role":"user","content":"Reply with exactly OK."}],"metadata":{"completion_window":"flex"}}

Dropped-tier Messages payload, saved as messages-drop.json

{"model":"sail-flash","max_tokens":64,"messages":[{"role":"user","content":"Reply with exactly OK."}],"service_tier":"flex","additional_drop_params":["service_tier"]}

Responses payload, saved as responses.json

{"model":"sail-flash","max_output_tokens":32,"input":"Reply OK","service_tier":"flex"}

Before (b907975)

Chat

  1. curl -sS --max-time 240 -i http://127.0.0.1:4012/v1/chat/completions -H 'Authorization: Bearer sk-sail-local-audit' -H 'Content-Type: application/json' --data-binary @chat.json
  2. HTTP 200, service_tier: "asap", x-litellm-response-cost: 6.7699999999999996e-06, 17 uncached input tokens and 14 output tokens. Sail selected ASAP, although the caller requested Balanced

Messages

  1. curl -sS --max-time 240 -i http://127.0.0.1:4012/v1/messages -H 'Authorization: Bearer sk-sail-local-audit' -H 'Content-Type: application/json' --data-binary @messages.json
  2. HTTP 200, usage.service_tier: "asap", x-litellm-response-cost: 1.307e-05, 17 uncached input tokens and 32 output tokens. Sail selected ASAP even though the caller requested Flex

Responses

  1. curl -sS --max-time 240 -i http://127.0.0.1:4012/v1/responses -H 'Authorization: Bearer sk-sail-local-audit' -H 'Content-Type: application/json' --data-binary @responses.json
  2. HTTP 400 after Sail rejects metadata.completion_window, wrapped as SailException

After (8c81a0a)

Chat

  1. Repeat the Chat command using port 4011
  2. HTTP 200, service_tier: "balanced", x-litellm-response-cost: 1.832e-05, 17 input tokens including 16 cached and 64 output tokens. Sail selected Balanced and the cost exactly matches that tier

Messages

  1. Repeat the Messages command using port 4011
  2. HTTP 400 with sail serves metadata.completion_window='flex' only to Responses requests with background=true. Send 'balanced' for a synchronous request. The cost header is zero
  3. curl -sS --max-time 120 -i http://127.0.0.1:4011/v1/messages -H 'Authorization: Bearer sk-sail-local-audit' -H 'Content-Type: application/json' --data-binary @messages-drop.json
  4. HTTP 200, usage.service_tier: "asap", x-litellm-response-cost: 2.283e-05, 1 uncached input token, 16 cached input tokens and 64 output tokens. The cost matches ASAP after the caller explicitly removed Flex

Responses

  1. Repeat the Responses command using port 4011
  2. HTTP 400 with sail serves service_tier='flex' (completion_window 'flex') only to Responses requests with background=true. Send 'balanced' for a synchronous request. LiteLLM rejects this before sending it to Sail

Rerun after the merge (713bc1c)

Same proxy setup and payloads on 2026-10-29: port 4012 runs the merge base 2e5cc9e0, port 4011 runs 713bc1c7.

Messages

  1. Repeat the Messages command using port 4011
  2. HTTP 400, x-litellm-response-cost: 0, litellm.UnsupportedParamsError: sail serves metadata.completion_window='flex' only to Responses requests with background=true. Send 'balanced' for a synchronous request. Nothing reaches Sail

Responses

  1. Repeat the Responses command using port 4012 (merge base)
  2. HTTP 400 from Sail, wrapped as SailException: completion_window "flex" is only available for background responses (POST /v1/responses with background=true) or Batch work; for synchronous requests use asap or balanced
  3. Repeat it using port 4011
  4. HTTP 400, x-litellm-response-cost: 0, litellm.UnsupportedParamsError: sail serves service_tier='flex' (completion_window 'flex') only to Responses requests with background=true. Send 'balanced' for a synchronous request. LiteLLM rejects it before calling Sail

Chat and dropped-tier Messages

  1. Repeat both commands using port 4011
  2. No response within 240 seconds. Plain chat calls sent directly to Sail staging, without LiteLLM, also got no response, so these two cases still need a rerun

Dependencies and remaining verification

Sail's served-tier response field merged in sailresearchco/sail#11084. The fresh staging responses now contain it on collected Chat and Messages, with matching costs. The full collected and streamed endpoint matrix still needs joint verification. The current Balanced Messages control still timed out through the proxy after 240 seconds. An earlier direct Sail control timed out after 130 seconds. This is not counted as a successful current-tip live case

Native background billing exists in LiteLLM, but exactly-once managed spend accounting through completion and repeated retrieval remains a separate live check. No database or Redis billing backend was connected in this local proxy run

Optional polling_via_cache is disabled by default. When enabled it rewrites background work into a foreground stream, which cannot serve Sail Flex. Keep Sail deployment names in native_background_mode when using that optional feature

Pre-Submission checklist

  • Meaningful regression tests added
  • Targeted tests and changed-file checks pass
  • Scope is the Sail completion-window contract
  • Current-tip required CI and coverage pass (waiting for workflow approval)
  • Current-tip automated review findings are complete and addressed
  • Joint live verification with Sail's deployed served-tier field

Sail serves the flex window only to Responses requests with
background=true, and the asap window only to requests without it. It
answers every other pairing with a 400. The Sail config still mapped
service_tier "flex" to the flex window on Chat Completions and on
foreground Responses, so those calls reached Sail and failed, even under
drop_params.

Chat Completions now rejects a tier that selects flex before calling
Sail. drop_params does not drop it, because chat billing reads the
caller's service_tier and would bill flex for a request Sail runs at
asap. Responses rejects flex without background=true and asap with it,
from either service_tier or metadata.completion_window. Under drop_params
the window is dropped and the request bills at the base price.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codspeed

codspeed Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 31 untouched benchmarks


Comparing kanand77:fix/sail-completion-window-rules (8c81a0a) with main (123e207)

Open in CodSpeed

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@greptile-apps

greptile-apps Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Automatic reviews are disabled for this repository.

@kanand77 kanand77 changed the title fix(sail): reject completion windows Sail does not serve for the request fix(sail): validate completion windows and preserve served-tier billing Oct 15, 2026
@kanand77
kanand77 marked this pull request as draft October 15, 2026 18:30
@kanand77 kanand77 changed the title fix(sail): validate completion windows and preserve served-tier billing fix(sail): honor completion windows and preserve served-tier billing Oct 15, 2026
@kanand77
kanand77 marked this pull request as ready for review October 16, 2026 00:35
@veria-ai

veria-ai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

@kanand77
kanand77 marked this pull request as draft October 16, 2026 00:54
@kanand77
kanand77 marked this pull request as ready for review October 16, 2026 18:40
…indow-rules

Conflict resolutions:

- anthropic/messages/native/handler.py (renamed from pass_through on
  main): main's MCP gateway call replaces the old block and now gets the
  Anthropic-validated metadata like the other Anthropic calls. The
  provider's request metadata joins main's tool cache-control marking
  when building the optional params.
- custom_httpx/llm_http_handler.py: main now applies
  additional_drop_params before logging, into request_params, so the
  branch's own drop step and its relocated logging call are dropped and
  the billing params are recorded from request_params instead.
- main.py: the Sail caller-metadata lines move into a helper, because
  completion() is at basedpyright's complexity limit and the inline
  branches pushed it over ("Code is too complex to analyze").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kanand77

kanand77 commented Oct 29, 2026 •

Copy link
Copy Markdown
Author

@shrey-berri this is ready for review. It makes the Sail provider follow Sail's current completion-window rules (flex only on background Responses) and bills from the service_tier Sail now returns (sailresearchco/sail#11084). It replaces the draft #43477, which can be closed. I just merged main to clear conflicts. CI on the new head needs a maintainer to approve the workflow runs from this fork; the previous head passed all checks. Happy to make changes.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant