Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(proxy): strip [1m] model suffix before upstream forwarding (#2027)

Abhishek Mittal52a024d28cff
diff --git a/crates/headroom-proxy/src/compression/mod.rs b/crates/headroom-proxy/src/compression/mod.rs
index af441e39a..f564b6062 100644
--- a/crates/headroom-proxy/src/compression/mod.rs
+++ b/crates/headroom-proxy/src/compression/mod.rs
@@ -85,6 +85,67 @@ pub fn classify_compressible_path(path: &str) -> Option<CompressibleEndpoint> {
}
}
+/// Strip the `[1m]` context-window tier suffix that the Headroom
+/// CLI appends to Anthropic model IDs (e.g. `glm-5.2[1m]`,
+/// `claude-3-7-sonnet[1m]`) before forwarding to the upstream
+/// Anthropic API. The upstream does not recognize the suffix and
+/// rejects requests with 400.
+///
+/// This mirrors the Python proxy's `sanitize_anthropic_model_id()`
+/// (Python PR #1840, fixes issue #1812). The Rust proxy
+/// intentionally scopes this to **Anthropic** requests only — the
+/// `[1m]` marker is an Anthropic / Claude Code compatibility
+/// signal emitted by the CLI, and we must not silently mutate
+/// OpenAI-compatible request model IDs. The caller is responsible
+/// for gating on `CompressibleEndpoint::AnthropicMessages` before
+/// invoking this function; doing so at the call site (not inside
+/// this function) keeps the helper cheap and unambiguous.
+///
+/// Returns the original `body` byte-for-byte on every "no
+/// sanitization" path:
+/// - body is not valid JSON
+/// - body has no top-level `model` field
+/// - top-level `model` is not a string
+/// - the string does not end in `[1m]`
+/// - re-serialization fails (extremely unusual; we have already
+/// parsed the body, but `serde_json` could in theory reject a
+/// value shape it accepted on parse)
+pub fn sanitize_anthropic_model_id_in_body(body: bytes::Bytes) -> bytes::Bytes {
+ let Ok(mut parsed) = serde_json::from_slice::<serde_json::Value>(&body) else {
+ return body;
+ };
+
+ let Some(model_value) = parsed.get_mut("model") else {
+ return body;
+ };
+
+ let serde_json::Value::String(model) = model_value else {
+ return body;
+ };
+
+ let sanitized = trim_anthropic_model_id_suffix(model);
+ if sanitized == *model {
+ // Either no `[1m]` suffix to strip, or the suffix is not at
+ // the tail (e.g. `claude-3-7-sonnet[1m]-thinking` — not a
+ // CLI-emitted shape, but a defensive no-op). Byte-equal
+ // passthrough preserves the cache-safety invariant.
+ return body;
+ }
+
+ *model = sanitized;
+ match serde_json::to_vec(&parsed) {
+ Ok(buf) => bytes::Bytes::from(buf),
+ Err(_) => body,
+ }
+}
+
+/// Pure helper: strip a trailing `[1m]` from a model ID string.
+/// Exposed for unit tests; production callers should use
+/// [`sanitize_anthropic_model_id_in_body`].
+fn trim_anthropic_model_id_suffix(model: &str) -> String {
+ model.trim_end_matches("[1m]").to_string()
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -124,4 +185,101 @@ mod tests {
assert!(!is_compressible_path("/"));
assert!(!is_compressible_path(""));
}
+
+ // ─── [1m] Anthropic model-suffix sanitizer (PR #2027) ────────────
+ //
+ // PR #2027 review feedback (JerrettDavis): the sanitizer must
+ // strip the `[1m]` CLI suffix from Anthropic `/v1/messages`
+ // request bodies without mutating OpenAI-shaped bodies. These
+ // unit tests pin the pure helper; integration coverage for
+ // /v1/messages vs /v1/chat/completions vs /v1/responses lives
+ // in `tests/integration_anthropic_model_sanitize.rs`.
+
+ #[test]
+ fn sanitizer_strips_trailing_1m_suffix() {
+ let body = br#"{"model":"glm-5.2[1m]","max_tokens":1024,"messages":[]}"#;
+ let out = sanitize_anthropic_model_id_in_body(bytes::Bytes::copy_from_slice(body));
+ let parsed: serde_json::Value = serde_json::from_slice(&out).unwrap();
+ assert_eq!(parsed["model"], "glm-5.2");
+ // Other fields round-trip unchanged.
+ assert_eq!(parsed["max_tokens"], 1024);
+ assert!(parsed["messages"].is_array());
+ }
+
+ #[test]
+ fn sanitizer_strips_suffix_from_claude_model() {
+ let body = br#"{"model":"claude-3-7-sonnet[1m]","max_tokens":1024}"#;
+ let out = sanitize_anthropic_model_id_in_body(bytes::Bytes::copy_from_slice(body));
+ let parsed: serde_json::Value = serde_json::from_slice(&out).unwrap();
+ assert_eq!(parsed["model"], "claude-3-7-sonnet");
+ }
+
+ #[test]
+ fn sanitizer_passthrough_when_no_suffix() {
+ let body = br#"{"model":"claude-3-7-sonnet","max_tokens":1024}"#;
+ let original = bytes::Bytes::copy_from_slice(body);
+ let out = sanitize_anthropic_model_id_in_body(original.clone());
+ // Byte-equal — required to keep the cache-safety invariant
+ // for the (very common) no-suffix case.
+ assert_eq!(out, original);
+ }
+
+ #[test]
+ fn sanitizer_passthrough_when_model_not_string() {
+ // Anthropic `/v1/messages` requires a string model, but a
+ // malformed body must round-trip unchanged — we never
+ // mutate non-string `model` values.
+ let body = br#"{"model":42,"max_tokens":1024}"#;
+ let original = bytes::Bytes::copy_from_slice(body);
+ let out = sanitize_anthropic_model_id_in_body(original.clone());
+ assert_eq!(out, original);
+ }
+
+ #[test]
+ fn sanitizer_passthrough_when_no_model_field() {
+ let body = br#"{"max_tokens":1024,"messages":[]}"#;
+ let original = bytes::Bytes::copy_from_slice(body);
+ let out = sanitize_anthropic_model_id_in_body(original.clone());
+ assert_eq!(out, original);
+ }
+
+ #[test]
+ fn sanitizer_passthrough_for_non_json_body() {
+ // A non-JSON body (e.g. SSE-rewritten or streaming chunk)
+ // must round-trip unchanged. The compressible-path gate in
+ // proxy.rs already filters by Content-Type=application/json,
+ // so this is a belt-and-braces check.
+ let body = b"not json at all";
+ let original = bytes::Bytes::copy_from_slice(body);
+ let out = sanitize_anthropic_model_id_in_body(original.clone());
+ assert_eq!(out, original);
+ }
+
+ #[test]
+ fn sanitizer_passthrough_for_1m_not_at_tail() {
+ // Defensive: a `[1m]` mid-string is not a CLI suffix and
+ // must not be mutated. Real CLI behavior only ever appends
+ // `[1m]` at the tail, but a future wire format should not
+ // silently corrupt legitimate `model` strings.
+ let body = br#"{"model":"claude-3-7-sonnet[1m]-thinking","max_tokens":1024}"#;
+ let original = bytes::Bytes::copy_from_slice(body);
+ let out = sanitize_anthropic_model_id_in_body(original.clone());
+ assert_eq!(out, original);
+ }
+
+ #[test]
+ fn trim_helper_pure() {
+ // The pure helper is a thin wrapper around
+ // `str::trim_end_matches`; pin its behavior directly so
+ // refactors that swap the implementation can't silently
+ // change the strip semantics.
+ assert_eq!(trim_anthropic_model_id_suffix("glm-5.2[1m]"), "glm-5.2");
+ assert_eq!(trim_anthropic_model_id_suffix("claude[1m]"), "claude");
+ assert_eq!(trim_anthropic_model_id_suffix("claude"), "claude");
+ // `trim_end_matches` is greedy — back-to-back suffixes are
+ // all stripped. The CLI never emits this, but pinning the
+ // behavior makes the helper a pure function with a stable
+ // contract.
+ assert_eq!(trim_anthropic_model_id_suffix("claude[1m][1m]"), "claude");
+ }
}
diff --git a/crates/headroom-proxy/src/proxy.rs b/crates/headroom-proxy/src/proxy.rs
index 89820d6cc..7bccf123b 100644
--- a/crates/headroom-proxy/src/proxy.rs
+++ b/crates/headroom-proxy/src/proxy.rs
@@ -10,9 +10,9 @@ use axum::http::{HeaderMap, HeaderName, Request, Response, StatusCode, Uri};
use axum::response::IntoResponse;
use axum::routing::{any, get, post};
use axum::Router;
+use futures_util::{StreamExt as _, TryStreamExt};
#[cfg(test)]
use bytes::Bytes;
-use futures_util::{StreamExt as _, TryStreamExt};
#[cfg(test)]
use http_body_util::BodyExt;
@@ -632,6 +632,26 @@ pub(crate) async fn forward_http(
let endpoint = compression::classify_compressible_path(uri.path())
.expect("is_compressible_path guarded above");
+ // PR-2027: strip the `[1m]` context-window tier suffix from
+ // the request body for Anthropic messages only. The
+ // Headroom CLI appends `[1m]` to model IDs (e.g.
+ // `glm-5.2[1m]`, `claude-3-7-sonnet[1m]`) to signal 1M
+ // context to Claude Code; the upstream Anthropic API does
+ // not recognize the suffix and rejects the request. The
+ // suffix is an Anthropic/Claude Code compatibility marker,
+ // so we must not silently mutate OpenAI-compatible
+ // request model IDs. The sanitizer is gated on the
+ // already-classified `endpoint`, which is the same source
+ // of truth the dispatcher uses below — keeping the gate
+ // and the dispatch in lockstep.
+ let buffered = match endpoint {
+ compression::CompressibleEndpoint::AnthropicMessages => {
+ compression::sanitize_anthropic_model_id_in_body(buffered)
+ }
+ compression::CompressibleEndpoint::OpenAiChatCompletions
+ | compression::CompressibleEndpoint::OpenAiResponses => buffered,
+ };
+
// PR-E5 + PR-E6: cache-stabilization observability hooks.
// Both run READ-ONLY against the buffered body and emit
// structured logs only — passthrough invariant from Phase A
diff --git a/crates/headroom-proxy/tests/integration_anthropic_model_sanitize.rs b/crates/headroom-proxy/tests/integration_anthropic_model_sanitize.rs
new file mode 100644
index 000000000..7a50df379
--- /dev/null
+++ b/crates/headroom-proxy/tests/integration_anthropic_model_sanitize.rs
@@ -0,0 +1,328 @@
+//! End-to-end integration coverage for the `[1m]` Anthropic
+//! model-suffix sanitizer (PR #2027).
+//!
+//! Review feedback (JerrettDavis):
+//!
+//! > Please scope the sanitizer to `CompressibleEndpoint::AnthropicMessages`
+//! > after classification, and add Rust coverage for both sides: one test
+//! > that `/v1/messages` strips `glm-5.2[1m]` / Claude-style suffixes
+//! > before forwarding, and one test that an OpenAI chat/responses
+//! > request with a literal model ending in `[1m]` is left byte-for-byte
+//! > unchanged unless some other explicit OpenAI mutation applies.
+//!
+//! These tests boot a real Rust proxy in front of a wiremock upstream
+//! and observe the bytes the upstream actually receives. The
+//! OpenAI-side tests assert SHA-256 byte equality (per the project's
+//! cache-safety contract for byte-faithful passthrough) and the
+//! Anthropic-side tests assert the upstream receives the suffix-free
+//! model ID by parsing the captured body as JSON.
+//!
+//! Each test uses a model ID that ends in a literal `[1m]`. On the
+//! Anthropic path the proxy must strip the suffix before forwarding;
+//! on the OpenAI paths the proxy must leave the body byte-equal to
+//! what the client sent.
+//!
+//! Why split into two assertions (JSON-parse + SHA-256) rather than
+//! only SHA-256? Because the Anthropic assertion is "the upstream
+//! received a body whose `model` field is `glm-5.2`" — i.e. the
+//! suffix is actually gone — while the OpenAI assertion is "the
+//! upstream received exactly these bytes" (per the cache-safety
+//! invariant). Conflating them obscures what each side is pinning.
+
+mod common;
+
+use bytes::Bytes;
+use common::start_proxy_with;
+use serde_json::{json, Value};
+use sha2::{Digest, Sha256};
+use std::sync::{Arc, Mutex};
+use wiremock::matchers::{method, path};
+use wiremock::{Mock, MockServer, ResponseTemplate};
+
+/// Mount a `/v1/messages` capture on the wiremock upstream.
+async fn mount_anthropic_capture(upstream: &MockServer) -> Arc<Mutex<Option<Vec<u8>>>> {
+ let captured: Arc<Mutex<Option<Vec<u8>>>> = Arc::new(Mutex::new(None));
+ let captured_clone = captured.clone();
+ Mock::given(method("POST"))
+ .and(path("/v1/messages"))
+ .respond_with(move |req: &wiremock::Request| {
+ *captured_clone.lock().unwrap() = Some(req.body.clone());
+ ResponseTemplate::new(200).set_body_string(r#"{"ok":true}"#)
+ })
+ .mount(upstream)
+ .await;
+ captured
+}
+
+/// Mount a `/v1/chat/completions` capture on the wiremock upstream.
+async fn mount_chat_capture(upstream: &MockServer) -> Arc<Mutex<Option<Vec<u8>>>> {
+ let captured: Arc<Mutex<Option<Vec<u8>>>> = Arc::new(Mutex::new(None));
+ let captured_clone = captured.clone();
+ Mock::given(method("POST"))
+ .and(path("/v1/chat/completions"))
+ .respond_with(move |req: &wiremock::Request| {
+ *captured_clone.lock().unwrap() = Some(req.body.clone());
+ ResponseTemplate::new(200).set_body_string(r#"{"ok":true}"#)
+ })
+ .mount(upstream)
+ .await;
+ captured
+}
+
+/// Mount a `/v1/responses` capture on the wiremock upstream.
+async fn mount_responses_capture(upstream: &MockServer) -> Arc<Mutex<Option<Vec<u8>>>> {
+ let captured: Arc<Mutex<Option<Vec<u8>>>> = Arc::new(Mutex::new(None));
+ let captured_clone = captured.clone();
+ Mock::given(method("POST"))
+ .and(path("/v1/responses"))
+ .respond_with(move |req: &wiremock::Request| {
+ *captured_clone.lock().unwrap() = Some(req.body.clone());
+ ResponseTemplate::new(200).set_body_string(r#"{"ok":true}"#)
+ })
+ .mount(upstream)
+ .await;
+ captured
+}
+
+fn sha256_hex(bytes: &[u8]) -> String {
+ let mut hasher = Sha256::new();
+ hasher.update(bytes);
+ hasher
+ .finalize()
+ .iter()
+ .fold(String::with_capacity(64), |mut acc, b| {
+ use std::fmt::Write as _;
+ let _ = write!(acc, "{b:02x}");
+ acc
+ })
+}
+
+#[track_caller]
+fn assert_byte_equal_sha256(inbound: &[u8], received: &[u8]) {
+ let inbound_hash = sha256_hex(inbound);
+ let received_hash = sha256_hex(received);
+ assert_eq!(
+ inbound.len(),
+ received.len(),
+ "byte length mismatch: inbound={}, upstream-received={}",
+ inbound.len(),
+ received.len(),
+ );
+ assert_eq!(
+ inbound_hash, received_hash,
+ "SHA-256 mismatch: inbound={inbound_hash}, upstream-received={received_hash}",
+ );
+}
+
+// ─── Anthropic: /v1/messages must strip the suffix ───────────────────
+
+#[tokio::test]
+async fn anthropic_messages_strips_1m_suffix_glm() {
+ let upstream = MockServer::start().await;
+ let captured = mount_anthropic_capture(&upstream).await;
+ let proxy = start_proxy_with(&upstream.uri(), |c| {
+ c.compression = true;
+ c.compression_mode = headroom_proxy::config::CompressionMode::LiveZone;
+ })
+ .await;
+
+ let payload = json!({
+ "model": "glm-5.2[1m]",
+ "max_tokens": 1024,
+ "messages": [{"role": "user", "content": "hello"}],
+ });
+ let body = Bytes::from(serde_json::to_vec(&payload).unwrap());
+ let resp = reqwest::Client::new()
+ .post(format!("{}/v1/messages", proxy.url()))
+ .header("content-type", "application/json")
+ .body(body)
+ .send()
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), 200);
+
+ let got = captured.lock().unwrap().clone().expect("upstream got body");
+ let parsed: Value = serde_json::from_slice(&got).expect("upstream body is JSON");
+ // The whole point of PR #2027: the upstream Anthropic API must
+ // see `glm-5.2`, not `glm-5.2[1m]`. If the suffix slips through,
+ // the upstream returns 400.
+ assert_eq!(parsed["model"], "glm-5.2");
+ // Other fields round-trip.
+ assert_eq!(parsed["max_tokens"], 1024);
+ assert!(parsed["messages"].is_array());
+ proxy.shutdown().await;
+}
+
+#[tokio::test]
+async fn anthropic_messages_strips_1m_suffix_claude() {
+ let upstream = MockServer::start().await;
+ let captured = mount_anthropic_capture(&upstream).await;
+ let proxy = start_proxy_with(&upstream.uri(), |c| {
+ c.compression = true;
+ c.compression_mode = headroom_proxy::config::CompressionMode::LiveZone;
+ })
+ .await;
+
+ let payload = json!({
+ "model": "claude-3-7-sonnet[1m]",
+ "max_tokens": 1024,
+ "messages": [{"role": "user", "content": "hi"}],
+ });
+ let body = Bytes::from(serde_json::to_vec(&payload).unwrap());
+ let resp = reqwest::Client::new()
+ .post(format!("{}/v1/messages", proxy.url()))
+ .header("content-type", "application/json")
+ .body(body)
+ .send()
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), 200);
+
+ let got = captured.lock().unwrap().clone().expect("upstream got body");
+ let parsed: Value = serde_json::from_slice(&got).expect("upstream body is JSON");
+ assert_eq!(parsed["model"], "claude-3-7-sonnet");
+ proxy.shutdown().await;
+}
+
+#[tokio::test]
+async fn anthropic_messages_passthrough_when_no_suffix() {
+ // Pin the no-op case end-to-end. The body the upstream receives
+ // must be byte-equal to the body the client sent — the
+ // cache-safety invariant the OpenAI tests below also assert.
+ let upstream = MockServer::start().await;
+ let captured = mount_anthropic_capture(&upstream).await;
+ let proxy = start_proxy_with(&upstream.uri(), |c| {
+ c.compression = true;
+ c.compression_mode = headroom_proxy::config::CompressionMode::LiveZone;
+ })
+ .await;
+
+ let payload = json!({
+ "model": "claude-3-7-sonnet",
+ "max_tokens": 1024,
+ "messages": [{"role": "user", "content": "hi"}],
+ });
+ let body = Bytes::from(serde_json::to_vec(&payload).unwrap());
+ let resp = reqwest::Client::new()
+ .post(format!("{}/v1/messages", proxy.url()))
+ .header("content-type", "application/json")
+ .body(body.clone())
+ .send()
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), 200);
+
+ let got = captured.lock().unwrap().clone().expect("upstream got body");
+ assert_byte_equal_sha256(&body, &got);
+ proxy.shutdown().await;
+}
+
+// ─── OpenAI: /v1/chat/completions must NOT mutate the model ─────────
+
+#[tokio::test]
+async fn openai_chat_completions_passthrough_with_1m_model() {
+ // Per the review feedback: an OpenAI chat request with a
+ // literal model ending in `[1m]` must be left byte-for-byte
+ // unchanged unless some other explicit OpenAI mutation applies.
+ //
+ // Test setup:
+ //
+ // - `CompressionMode::LiveZone` runs the actual Chat Completions
+ // live-zone dispatcher. For this small body the dispatcher
+ // has nothing to compress, so it returns `NoCompression` and
+ // the body round-trips byte-equal.
+ // - The `Authorization: Bearer <jwt>` header classifies the
+ // request as `AuthMode::OAuth` (rule 4: 3 dot-separated
+ // segments), so PR-E4 `prompt_cache_key` injection
+ // short-circuits. Without this header the proxy defaults to
+ // `AuthMode::Payg` and the injector would mutate the body —
+ // the byte-equality assertion would then fail for an
+ // unrelated reason.
+ //
+ // CRITICAL: if this assertion fails after the fix, the
+ // sanitizer is being applied too broadly (the original PR
+ // #2027 bug). The OAuth-Authorization is the control variable
+ // that isolates the sanitizer's scope from the E4 injector.
+ let upstream = MockServer::start().await;
+ let captured = mount_chat_capture(&upstream).await;
+ let proxy = start_proxy_with(&upstream.uri(), |c| {
+ c.compression = true;
+ c.compression_mode = headroom_proxy::config::CompressionMode::LiveZone;
+ })
+ .await;
+
+ let payload = json!({
+ "model": "gpt-4o[1m]",
+ "messages": [{"role": "user", "content": "hello"}],
+ });
+ let body = Bytes::from(serde_json::to_vec(&payload).unwrap());
+ let resp = reqwest::Client::new()
+ .post(format!("{}/v1/chat/completions", proxy.url()))
+ .header("content-type", "application/json")
+ // OAuth bearer token (3 dot-separated segments) → the
+ // proxy's auth-mode classifier picks `AuthMode::OAuth`,
+ // which gates PR-E4 prompt_cache_key injection to a
+ // no-op. This is the same control variable the existing
+ // byte-equality tests in `integration_chat_completions.rs`
+ // use to isolate dispatcher byte-fidelity from E4.
+ .header(
+ "authorization",
+ "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.signature_bytes",
+ )
+ .body(body.clone())
+ .send()
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), 200);
+
+ let got = captured.lock().unwrap().clone().expect("upstream got body");
+ assert_byte_equal_sha256(&body, &got);
+ let parsed: Value = serde_json::from_slice(&got).expect("upstream body is JSON");
+ assert_eq!(parsed["model"], "gpt-4o[1m]");
+ proxy.shutdown().await;
+}
+
+// ─── OpenAI: /v1/responses must NOT mutate the model ─────────────────
+
+#[tokio::test]
+async fn openai_responses_passthrough_with_1m_model() {
+ // Same contract as /v1/chat/completions but for the Responses
+ // endpoint. The Responses live-zone dispatcher walks a typed
+ // `input` array; for this fixture the input is a single short
+ // string, so the dispatcher has nothing to compress and the
+ // body round-trips byte-equal. The OAuth Authorization
+ // short-circuits the PR-E4 `prompt_cache_key` injector, so
+ // the only byte mutation that could fire is the sanitizer
+ // itself — which is exactly what we are pinning.
+ let upstream = MockServer::start().await;
+ let captured = mount_responses_capture(&upstream).await;
+ let proxy = start_proxy_with(&upstream.uri(), |c| {
+ c.compression = true;
+ c.compression_mode = headroom_proxy::config::CompressionMode::LiveZone;
+ })
+ .await;
+
+ let payload = json!({
+ "model": "gpt-4o[1m]",
+ "input": "hello",
+ });
+ let body = Bytes::from(serde_json::to_vec(&payload).unwrap());
+ let resp = reqwest::Client::new()
+ .post(format!("{}/v1/responses", proxy.url()))
+ .header("content-type", "application/json")
+ .header(
+ "authorization",
+ "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.signature_bytes",
+ )
+ .body(body.clone())
+ .send()
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), 200);
+
+ let got = captured.lock().unwrap().clone().expect("upstream got body");
+ assert_byte_equal_sha256(&body, &got);
+ let parsed: Value = serde_json::from_slice(&got).expect("upstream body is JSON");
+ assert_eq!(parsed["model"], "gpt-4o[1m]");
+ proxy.shutdown().await;
+}