Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix(cli): count each Claude Code API response once in the session token rollup (#1783)

Steven Richards819bf2f7b3e0
diff --git a/cli/internal/coding/hook/claudecode/handle.go b/cli/internal/coding/hook/claudecode/handle.go
index 1a37ee2d..709ebfe5 100644
--- a/cli/internal/coding/hook/claudecode/handle.go
+++ b/cli/internal/coding/hook/claudecode/handle.go
@@ -948,20 +948,43 @@ func tailTranscript(path string) (model string, cost float64, inTokens, outToken
CacheReadInputTokens int64 `json:"cache_read_input_tokens"`
}
type turn struct {
- Type string `json:"type"`
- Message struct {
+ Type string `json:"type"`
+ RequestID string `json:"requestId"`
+ Message struct {
+ ID string `json:"id"`
Model string `json:"model"`
Usage usage `json:"usage"`
} `json:"message"`
}
- var ti, to, cacheRead, cacheCreation int64
- var lastModel string
+ // Claude Code writes one line per content block (thinking / text /
+ // tool_use) of an API response, each repeating the response's usage.
+ // Count every response once — the last line of a message wins, as
+ // in coalesceAssistants.
+ byMessage := map[string]int{}
+ var turns []turn
for _, line := range lines {
var t turn
if err := json.Unmarshal(line, &t); err != nil {
continue
}
+ key := t.Message.ID
+ if key == "" {
+ key = t.RequestID
+ }
+ if key != "" {
+ if i, seen := byMessage[key]; seen {
+ turns[i] = t
+ continue
+ }
+ byMessage[key] = len(turns)
+ }
+ turns = append(turns, t)
+ }
+
+ var ti, to, cacheRead, cacheCreation int64
+ var lastModel string
+ for _, t := range turns {
// Anthropic transcript reports cache creation + cache read as
// separate counters; "fresh" input is the standard input_tokens.
// The OTel field gen_ai.usage.input_tokens should equal total
diff --git a/cli/internal/coding/hook/claudecode/transcript_usage_test.go b/cli/internal/coding/hook/claudecode/transcript_usage_test.go
new file mode 100644
index 00000000..24bdf644
--- /dev/null
+++ b/cli/internal/coding/hook/claudecode/transcript_usage_test.go
@@ -0,0 +1,63 @@
+package claudecode
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/openlit/openlit/cli/internal/coding/pricing"
+)
+
+// TestTailTranscriptCountsEachMessageOnce covers the SessionEnd token
+// and cost rollup. Claude Code writes one transcript line per content
+// block (thinking / text / tool_use) of a single API response, and
+// every one of those lines repeats the same message.id, requestId and
+// usage object. The per-turn reader (coalesceAssistants) already
+// collapses them; the session rollup must count the response once too.
+func TestTailTranscriptCountsEachMessageOnce(t *testing.T) {
+ usage := map[string]any{
+ "input_tokens": 10,
+ "cache_creation_input_tokens": 200,
+ "cache_read_input_tokens": 1000,
+ "output_tokens": 50,
+ }
+ var lines []string
+ for _, block := range []map[string]any{
+ {"type": "thinking", "thinking": "plan"},
+ {"type": "text", "text": "Running the tests."},
+ {"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": map[string]any{"command": "go test ./..."}},
+ } {
+ raw, err := json.Marshal(map[string]any{
+ "type": "assistant",
+ "requestId": "req_1",
+ "message": map[string]any{
+ "id": "msg_1",
+ "model": "claude-sonnet-4-5",
+ "content": []any{block},
+ "stop_reason": "tool_use",
+ "usage": usage,
+ },
+ })
+ if err != nil {
+ t.Fatalf("marshal: %v", err)
+ }
+ lines = append(lines, string(raw))
+ }
+ path := filepath.Join(t.TempDir(), "transcript.jsonl")
+ if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil {
+ t.Fatalf("write transcript: %v", err)
+ }
+
+ model, cost, in, out, total := tailTranscript(path)
+ if model != "claude-sonnet-4-5" {
+ t.Errorf("model = %q", model)
+ }
+ if in != 1210 || out != 50 || total != 1260 {
+ t.Errorf("tailTranscript tokens = in %d / out %d / total %d, want 1210 / 50 / 1260 (one API response)", in, out, total)
+ }
+ if want := pricing.Lookup(model).Cost(1210, 50, 1000, 200); cost != want {
+ t.Errorf("tailTranscript cost = %.8f, want %.8f", cost, want)
+ }
+}