Skip to content

Move the quota reader to tw-wire, and stop rebuilding header maps - #127

Merged
fylorn merged 1 commit into
mainfrom
refactor/quota-to-wire
Sep 23, 2026
Merged

fylorn merged 1 commit into
mainfrom
refactor/quota-to-wire

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Follows #126.

What moves

quota.rs (455 lines) joins usage and bodies in tw-wire.

It reads the subscription window straight out of response headers — Anthropic puts it on every response, Codex has the same shape in another set. No extra request, no extra scope, no spending the user's own quota. Zero tw-* dependencies, and nothing in it is specific to this gateway.

The enterprise gateway has no equivalent and will need one the moment it fronts a subscription account.

What gets fixed on the way

from_headers_reqwest is deleted. It rebuilt the whole header map entry by entry:

pub fn from_headers_reqwest(h: &reqwest::header::HeaderMap, now_ms: u64) -> Quota {
    let mut axum_map = HeaderMap::new();
    for (k, v) in h.iter() { /* … */ }
    from_headers(&axum_map, now_ms)
}

Its doc comment explained the premise: "reqwest 和 axum 各有各的 HeaderMap". That stopped being true at some point — reqwest 0.13.4 and axum 0.8.9 both re-export http 1.5.0, so the two arguments were already the same type, and every single response paid for a copy that changed nothing. Calling from_headers directly compiles, which is the proof.

(The one http 0.2.12 left in the tree arrives under aws-sigv4 — the Bedrock provider.)

Dependency note

The crate now depends on http directly instead of reaching through axum::http. A shared layer should not dictate which HTTP framework its consumers run; both of the maps it was juggling were this crate's type all along.

cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings, and the full test suite (28 suites) are green.

🤖 Generated with Claude Code

…r maps

`quota` reads the subscription window straight out of response headers —
Anthropic puts it on every response, Codex has the same shape in another
set. It costs nothing: no extra request, no extra scope, no spending the
user's own quota. It has no `tw-*` dependency and nothing about it is
specific to this gateway, so it belongs next to the other sideband
readers. The enterprise gateway has no equivalent and needs one as soon
as it fronts a subscription account.

`from_headers_reqwest` goes away. It rebuilt the entire header map entry
by entry on the premise that "reqwest and axum each have their own
HeaderMap" — which has not been true for some time. reqwest 0.13 and
axum 0.8 both re-export http 1.5.0, so the two arguments were already
the same type and every response paid for a copy that changed nothing.
(The one http 0.2 in the tree comes in under aws-sigv4, i.e. the Bedrock
provider.) Calling `from_headers` directly compiles, which is the proof.

The crate now depends on `http` rather than reaching through axum: a
shared layer should not dictate which HTTP framework its consumers run.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 2c57458 into main Sep 23, 2026
1 check passed
@fylorn
fylorn deleted the refactor/quota-to-wire branch September 23, 2026 07:44
@fylorn fylorn mentioned this pull request Sep 23, 2026
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