claude-code-cache-fix 1.6.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/preload.mjs +16 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-code-cache-fix",
3
- "version": "1.6.0",
3
+ "version": "1.6.1",
4
4
  "description": "Fixes prompt cache regression in Claude Code that causes up to 20x cost increase on resumed sessions",
5
5
  "type": "module",
6
6
  "exports": "./preload.mjs",
package/preload.mjs CHANGED
@@ -962,12 +962,18 @@ globalThis.fetch = async function (url, options) {
962
962
  }
963
963
 
964
964
  // Clone response to extract TTL tier and usage telemetry from SSE stream.
965
- // Pass the model from the request so we can log a complete usage record.
965
+ // Pass the model and quota headers so we can log a complete usage record.
966
966
  try {
967
967
  let reqModel = "unknown";
968
968
  try { reqModel = JSON.parse(options?.body)?.model || "unknown"; } catch {}
969
+ const quotaHeaders = {
970
+ q5h: parseFloat(response.headers.get("anthropic-ratelimit-unified-5h-utilization") || "0"),
971
+ q7d: parseFloat(response.headers.get("anthropic-ratelimit-unified-7d-utilization") || "0"),
972
+ status: response.headers.get("anthropic-ratelimit-unified-status") || null,
973
+ overage: response.headers.get("anthropic-ratelimit-unified-overage-status") || null,
974
+ };
969
975
  const clone = response.clone();
970
- drainTTLFromClone(clone, reqModel).catch(() => {});
976
+ drainTTLFromClone(clone, reqModel, quotaHeaders).catch(() => {});
971
977
  } catch {
972
978
  // clone() failure is non-fatal
973
979
  }
@@ -988,7 +994,7 @@ globalThis.fetch = async function (url, options) {
988
994
  * Writes TTL tier to ~/.claude/quota-status.json (merges with existing data)
989
995
  * and logs to debug log.
990
996
  */
991
- async function drainTTLFromClone(clone, model) {
997
+ async function drainTTLFromClone(clone, model, quotaHeaders) {
992
998
  if (!clone.body) return;
993
999
 
994
1000
  const reader = clone.body.getReader();
@@ -1082,8 +1088,11 @@ async function drainTTLFromClone(clone, model) {
1082
1088
  if (startUsage) {
1083
1089
  try {
1084
1090
  const cc = startUsage.cache_creation || {};
1091
+ const now = new Date();
1092
+ const utcHour = now.getUTCHours();
1093
+ const utcDay = now.getUTCDay();
1085
1094
  const record = {
1086
- timestamp: new Date().toISOString(),
1095
+ timestamp: now.toISOString(),
1087
1096
  model: model || "unknown",
1088
1097
  input_tokens: startUsage.input_tokens ?? 0,
1089
1098
  output_tokens: deltaUsage?.output_tokens ?? 0,
@@ -1092,6 +1101,9 @@ async function drainTTLFromClone(clone, model) {
1092
1101
  ephemeral_1h_input_tokens: cc.ephemeral_1h_input_tokens ?? 0,
1093
1102
  ephemeral_5m_input_tokens: cc.ephemeral_5m_input_tokens ?? 0,
1094
1103
  ttl_tier: ttlTier,
1104
+ q5h_pct: quotaHeaders ? Math.round(quotaHeaders.q5h * 100) : null,
1105
+ q7d_pct: quotaHeaders ? Math.round(quotaHeaders.q7d * 100) : null,
1106
+ peak_hour: utcDay >= 1 && utcDay <= 5 && utcHour >= 13 && utcHour < 19,
1095
1107
  };
1096
1108
  appendFileSync(USAGE_JSONL, JSON.stringify(record) + "\n");
1097
1109
  } catch {