Repository navigation
Conversation
… and /v1/messages Problem ------- The upstream DeepAI gateway silently truncates the *beginning* of oversized request bodies (issue 2951461586#7). On /v1/responses we already handle this via the previous_response_id replay path with explicit truncation:"auto". But the two stateless endpoints — /v1/chat/completions and /v1/messages — forwarded requests unchanged, so a long conversation silently "forgot" its early turns. What this does -------------- - New respstate.EnforceLimits: a standalone function that applies the same token + byte dual-gate logic Replay uses, but takes a flat item list. It returns the original items, the truncated items, or TooLong + reason. - /v1/chat/completions and /v1/messages now run EnforceLimits on their (already chat-shaped) messages array before forwarding. The truncation semantics match /v1/responses: keep system prefix + first user msg + newest tail, insert a 「中間的對話歷史因超過上下文長度已由網關自動省略」 marker at the cut, and drop orphan tool outputs via the same helper. Defaults -------- Auto-truncation is ON by default — silent upstream truncation is strictly worse than a marked mid-cut. Clients that want strict behavior can send "truncation": "disabled" which surfaces a 400 with the exact overflow numbers (mirroring the existing truncation:"auto" opt-in on /v1/responses). Compatibility ------------- - /v1/responses flow unchanged; Replay still owns its own logic. - The marker text is identical to the one Replay uses. - No new env vars; honors the existing DEEPAI2API_CONTEXT_BODY_BYTES and DEEPAI2API_EFFECTIVE_CONTEXT_WINDOW / DEEPAI2API_CONTEXT_WINDOWS overrides via EffectiveContextWindow + ContextBodyBytes. Tests ----- - 5 new unit tests in respstate/enforce_test.go cover: under-budget passthrough, strict TooLong, auto-truncate path, byte-cap-only mode, and the output-reserve edge case. - go test ./... — all packages pass. - End-to-end: 250 KB body against claude-haiku-5-5 returns HTTP 200 with the codeword still recalled; "disabled" returns 400 as expected. Co-Authored-By: Claude Code <noreply@anthropic.com>
ihsieh31
force-pushed
the
feat/auto-truncate-chat-and-messages
branch
from
November 30, 2026 14:06
1897122 to
84464c5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Upstream DeepAI silently truncates the beginning of oversized request bodies (issue #7: ~196 KB body limit,
usage.prompt_tokenspegs near 40k). The/v1/responsesendpoint already handles this correctly viaprevious_response_idreplay +truncation: "auto". But the two stateless endpoints —/v1/chat/completionsand/v1/messages— forwarded payloads unchanged, so long conversations quietly "forgot" their earliest turns.What this does
New
respstate.EnforceLimits— pulls the dual-gate (token + byte) truncation logic out ofReplayinto a standalone, reusable function:(items, OK)when within budget(items, TooLong, msg)when over budget and auto-truncation is off(truncated items, OK)when over budget and auto-truncation is on(items, TooLong, msg)when even after truncation it still overflowsWire it into both stateless endpoints:
chat.goandanthropic.gocallEnforceLimitson the (already chat-shaped)messagesarray before forwarding. The semantics are identical to/v1/responses:(中間的對話歷史因超過上下文長度已由網關自動省略)marker at the cutdropOrphanToolOutputscleans up splitfunction_call/function_call_outputpairsDefaults
Auto-truncation is ON by default — silent upstream truncation is strictly worse than a marked mid-cut, and existing clients (Claude Code, Continue, etc.) get memory-loss protection with no changes needed.
Clients that prefer strict behavior can opt out per-request:
{ "model": "...", "messages": [...], "truncation": "disabled" }which surfaces a 400 with the exact overflow numbers (mirroring the existing
truncation: "auto"opt-in on/v1/responses).Compatibility
/v1/responsesflow unchanged —Replaystill owns its own logic;EnforceLimitsreuses the same helpers (truncateHistoryByCosts,historyTruncMarker,EffectiveContextWindow,ContextBodyBytes)DEEPAI2API_CONTEXT_BODY_BYTES,DEEPAI2API_EFFECTIVE_CONTEXT_WINDOW,DEEPAI2API_CONTEXT_WINDOWSoverridesReplayusesTests
internal/respstate/enforce_test.go:TooLongwhenallowAuto=falsemax_output_tokensreserve edge casego test ./...— all 23 packages passgo vet ./...— cleanclaude-haiku-5-5returns HTTP 200 with the codeword correctly recalled (vs. silent memory loss before);"truncation": "disabled"returns 400 as expectedFiles
internal/respstate/enforce.go— newinternal/respstate/enforce_test.go— newinternal/server/chat.go— addEnforceLimitscall before marshalinternal/server/anthropic.go— same, afterAnthropicToChatPayload🤖 Generated with Claude Code