fix(proxy): preserve Responses context accounting - #11
Draft
LPFchan wants to merge 1 commit into
Draft
Conversation
Report terminal input/cache usage to Anthropic clients and surface Responses context overflow as an invalid request so Claude Code can compact normally.\n\nImplemented by the mutual-agreement subagent and independently reviewed and verified by the parent agent.
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.
Summary / 概要
Preserve OpenAI Responses input/cache usage in the translated Anthropic stream so Claude Code can track context growth and auto-compact before the provider limit. Surface terminal context overflow as one
invalid_request_errorinstead of silently completing the failed stream or allowing the original error to be masked by a non-streaming retry.Related Issues / 关联 Issue
Closes #10.
Related to #5 and #6, which address the separate HTTP request-body limit encountered during large sessions.
Root Cause
The Responses translator emitted
input_tokens: 0inmessage_start, retained only upstream output usage, and sent onlyoutput_tokensin the finalmessage_delta. Claude Code therefore recorded zero input usage throughout translated sessions and could not reliably reach its proactive auto-compaction threshold.The translator also ignored
response.failed, then emitted a normalend_turn. A backendcontext_length_exceededevent was consequently unavailable to Claude Code's reactive compaction path. In error-aware downstream variants, treating the event as a generic API failure caused Claude Code to try a non-streaming request, which ChatGPT's Codex endpoint rejects withStream must be set to true, obscuring the overflow.Changes
message_delta.usage.context_length_exceededtoinvalid_request_error; preserve unrelated failures asapi_error.message_stop.The Anthropic TypeScript SDK explicitly merges late input/cache fields from
message_delta.usage, so this preserves immediate streaming and requires no buffering or token estimator.Type of Change / 变更类型
Test Plan / 测试计划
cargo fmt --checkcargo clippy --all-targets— change introduces no warnings; 12 pre-existing warnings remain on upstreammaincargo test responses_stream— 8 passedcargo test— 346 unit tests and 1 integration test passedgit diff --checkChecklist / 检查清单
cargo fmt --checkpassescargo clippy— zero warnings (12 pre-existing upstream warnings remain)cargo test— all tests pass