Skip to content

Commit 3788337

Browse files
committed
fix: exclude cache tokens from context window calculation
- Modified getApiMetrics.ts to calculate contextTokens as tokensIn + tokensOut only - Updated tests to reflect the new calculation behavior - Cache tokens (cacheWrites and cacheReads) no longer count towards context window - Fixes inflated context token display in UI that was showing ~170k instead of ~44k
1 parent b03d03d commit 3788337

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/shared/__tests__/getApiMetrics.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe("getApiMetrics", () => {
6161
expect(result.totalCacheWrites).toBe(5)
6262
expect(result.totalCacheReads).toBe(10)
6363
expect(result.totalCost).toBe(0.005)
64-
expect(result.contextTokens).toBe(315) // 100 + 200 + 5 + 10
64+
expect(result.contextTokens).toBe(300) // 100 + 200
6565
})
6666

6767
it("should calculate metrics from multiple api_req_started messages", () => {
@@ -83,7 +83,7 @@ describe("getApiMetrics", () => {
8383
expect(result.totalCacheWrites).toBe(8) // 5 + 3
8484
expect(result.totalCacheReads).toBe(17) // 10 + 7
8585
expect(result.totalCost).toBe(0.008) // 0.005 + 0.003
86-
expect(result.contextTokens).toBe(210) // 50 + 150 + 3 + 7 (from the last message)
86+
expect(result.contextTokens).toBe(200) // 50 + 150 (from the last message)
8787
})
8888

8989
it("should calculate metrics from condense_context messages", () => {
@@ -123,7 +123,7 @@ describe("getApiMetrics", () => {
123123
expect(result.totalCacheWrites).toBe(8) // 5 + 3
124124
expect(result.totalCacheReads).toBe(17) // 10 + 7
125125
expect(result.totalCost).toBe(0.01) // 0.005 + 0.002 + 0.003
126-
expect(result.contextTokens).toBe(210) // 50 + 150 + 3 + 7 (from the last api_req_started message)
126+
expect(result.contextTokens).toBe(200) // 50 + 150 (from the last api_req_started message)
127127
})
128128
})
129129

@@ -242,9 +242,9 @@ describe("getApiMetrics", () => {
242242
expect(result.totalCacheReads).toBe(10)
243243
expect(result.totalCost).toBe(0.005)
244244

245-
// The implementation will use the last message with tokens for contextTokens
246-
// In this case, it's the cacheReads message
247-
expect(result.contextTokens).toBe(10)
245+
// The implementation will use the last message with tokensIn or tokensOut for contextTokens
246+
// In this case, it's the tokensOut message (200)
247+
expect(result.contextTokens).toBe(200) // From the message with tokensOut
248248
})
249249

250250
it("should handle non-number values in api_req_started message", () => {
@@ -265,7 +265,7 @@ describe("getApiMetrics", () => {
265265
expect(result.totalCost).toBe(0)
266266

267267
// The implementation concatenates string values for contextTokens
268-
expect(result.contextTokens).toBe("not-a-numbernot-a-numbernot-a-numbernot-a-number")
268+
expect(result.contextTokens).toBe("not-a-numbernot-a-number") // Only tokensIn + tokensOut
269269
})
270270
})
271271

@@ -279,7 +279,7 @@ describe("getApiMetrics", () => {
279279
const result = getApiMetrics(messages)
280280

281281
// Should use the values from the last api_req_started message
282-
expect(result.contextTokens).toBe(210) // 50 + 150 + 3 + 7
282+
expect(result.contextTokens).toBe(200) // 50 + 150
283283
})
284284

285285
it("should calculate contextTokens from the last condense_context message", () => {
@@ -305,7 +305,7 @@ describe("getApiMetrics", () => {
305305
const result = getApiMetrics(messages)
306306

307307
// Should use the values from the last api_req_started message
308-
expect(result.contextTokens).toBe(210) // 50 + 150 + 3 + 7
308+
expect(result.contextTokens).toBe(200) // 50 + 150
309309
})
310310

311311
it("should handle missing values when calculating contextTokens", () => {
@@ -320,7 +320,7 @@ describe("getApiMetrics", () => {
320320
const result = getApiMetrics(messages)
321321

322322
// Should handle missing or invalid values
323-
expect(result.contextTokens).toBe(15) // 0 + 0 + 5 + 10
323+
expect(result.contextTokens).toBe(0) // 0 + 0 (no tokensIn or tokensOut)
324324

325325
// Restore console.error
326326
console.error = originalConsoleError

src/shared/getApiMetrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function getApiMetrics(messages: ClineMessage[]) {
7373
try {
7474
const parsedText: ParsedApiReqStartedTextType = JSON.parse(message.text)
7575
const { tokensIn, tokensOut, cacheWrites, cacheReads } = parsedText
76-
result.contextTokens = (tokensIn || 0) + (tokensOut || 0) + (cacheWrites || 0) + (cacheReads || 0)
76+
result.contextTokens = (tokensIn || 0) + (tokensOut || 0)
7777
} catch (error) {
7878
console.error("Error parsing JSON:", error)
7979
continue

0 commit comments

Comments
 (0)