@inetafrica/open-claudia 2.6.27 → 2.6.28

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/core/runner.js +20 -3
  2. package/package.json +1 -1
package/core/runner.js CHANGED
@@ -130,6 +130,11 @@ function logTurnUsage(state, usage, costUsd, announce, opts = {}) {
130
130
  const backend = state.settings?.backend || "claude";
131
131
  const parts = usageParts(usage, backend);
132
132
  const usageScope = opts.usageScope || "turn_delta";
133
+ // `parts.context` is the per-turn SUM of every API call's prefix, inflated
134
+ // by re-counting the cached prefix once per tool round-trip. When the caller
135
+ // tracked the true peak single-call prefix, report that as contextTokens and
136
+ // keep the inflated sum as billedContextTokens for cost attribution.
137
+ const hasPeak = Number.isFinite(opts.liveContextTokens) && opts.liveContextTokens > 0;
133
138
  const record = {
134
139
  ts: new Date().toISOString(),
135
140
  version: PKG_VERSION,
@@ -142,7 +147,8 @@ function logTurnUsage(state, usage, costUsd, announce, opts = {}) {
142
147
  outputTokens: parts.output,
143
148
  cacheReadTokens: parts.cacheRead,
144
149
  cacheCreationTokens: parts.cacheCreation,
145
- contextTokens: parts.context,
150
+ contextTokens: hasPeak ? opts.liveContextTokens : parts.context,
151
+ billedContextTokens: parts.context,
146
152
  costUsd: typeof costUsd === "number" ? costUsd : 0,
147
153
  };
148
154
  if (opts.rawUsage && opts.rawUsage !== usage) {
@@ -785,6 +791,11 @@ async function runClaude(prompt, cwd, replyToMsgId, opts = {}) {
785
791
  let toolUses = [];
786
792
  let currentTool = null;
787
793
  let currentToolDetail = "";
794
+ // True context-window occupancy is the largest single API call's prefix
795
+ // (input + cache_read + cache_creation), NOT the per-turn sum the result
796
+ // event reports — that sum re-counts the cached prefix once per tool
797
+ // round-trip and balloons to millions. Track the peak per-call prefix.
798
+ let peakContextTokens = 0;
788
799
 
789
800
  // Hermes-style skill announcements: one chat line when a learned skill
790
801
  // is invoked or its SKILL.md is written/patched. Deduped per turn.
@@ -897,6 +908,10 @@ async function runClaude(prompt, cwd, replyToMsgId, opts = {}) {
897
908
  const lastNewline = state.streamBuffer.lastIndexOf("\n");
898
909
  state.streamBuffer = lastNewline >= 0 ? state.streamBuffer.slice(lastNewline + 1) : state.streamBuffer;
899
910
  for (const evt of events) {
911
+ if (evt.type === "assistant" && evt.message?.usage) {
912
+ const callPrefix = usageParts(evt.message.usage, settings.backend || "claude").context;
913
+ if (callPrefix > peakContextTokens) peakContextTokens = callPrefix;
914
+ }
900
915
  if (evt.type === "assistant" && evt.message?.content) {
901
916
  for (const block of evt.message.content) {
902
917
  if (block.type === "text") assistantText += block.text;
@@ -1003,8 +1018,10 @@ async function runClaude(prompt, cwd, replyToMsgId, opts = {}) {
1003
1018
  // Codex already reports usage at turn.completed. A result usage block,
1004
1019
  // if present, is the same cumulative total and must not be counted again.
1005
1020
  if (evt.usage && settings.backend !== "codex") {
1006
- applyUsageToState(state, evt.usage, typeof evt.total_cost_usd === "number" ? evt.total_cost_usd : 0, { usageScope: "turn_delta" });
1007
- logTurnUsage(state, evt.usage, typeof evt.total_cost_usd === "number" ? evt.total_cost_usd : 0, (text) => chatContext.run(store, () => send(text)), { usageScope: "turn_delta" });
1021
+ const peakOpts = { usageScope: "turn_delta" };
1022
+ if (peakContextTokens > 0) peakOpts.liveContextTokens = peakContextTokens;
1023
+ applyUsageToState(state, evt.usage, typeof evt.total_cost_usd === "number" ? evt.total_cost_usd : 0, peakOpts);
1024
+ logTurnUsage(state, evt.usage, typeof evt.total_cost_usd === "number" ? evt.total_cost_usd : 0, (text) => chatContext.run(store, () => send(text)), peakOpts);
1008
1025
  }
1009
1026
  if (typeof evt.total_cost_usd === "number" && settings.backend === "codex") state.sessionUsage.costUsd += evt.total_cost_usd;
1010
1027
  saveState();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inetafrica/open-claudia",
3
- "version": "2.6.27",
3
+ "version": "2.6.28",
4
4
  "description": "Your always-on AI coding assistant — Claude Code, Cursor Agent, and OpenAI Codex via Telegram or Kazee Chat",
5
5
  "main": "bot.js",
6
6
  "bin": {