|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// ccBuiltinConstants holds the always-on costs Claude Code charges every |
| 9 | +// turn that we structurally cannot read from the transcript: |
| 10 | +// |
| 11 | +// - SystemPromptTokens the bundled default system prompt |
| 12 | +// - SystemToolsTokens full JSON schemas for the default tools |
| 13 | +// (Read, Edit, Bash, …) — not the names alone |
| 14 | +// - SystemToolsDeferredTokens the catalog of deferred tool definitions |
| 15 | +// (Cron*, Task*, Web*, Monitor, …) plus any |
| 16 | +// schemas Claude Code injects on demand |
| 17 | +// |
| 18 | +// These are taken from `/context` for a known CC version. They drift with |
| 19 | +// each binary release, so the table below should grow over time. |
| 20 | +type ccBuiltinConstants struct { |
| 21 | + SystemPromptTokens int |
| 22 | + SystemToolsTokens int |
| 23 | + SystemToolsDeferredTokens int |
| 24 | +} |
| 25 | + |
| 26 | +// ccBuiltinByVersion is a small versioned table — keys are exact CC |
| 27 | +// CLI versions (the `version` field stamped on every transcript entry). |
| 28 | +// When the binary changes its bundled prompt or tool catalog, /context's |
| 29 | +// numbers shift; add an entry here with the new ones. |
| 30 | +// |
| 31 | +// Source: `claude -p "/context"` in a project with NO MCPs connected, on |
| 32 | +// the listed CC version + Opus model. The two figures together cover |
| 33 | +// >65% of /context's accounted-for tokens. |
| 34 | +var ccBuiltinByVersion = map[string]ccBuiltinConstants{ |
| 35 | + "2.1.150": { |
| 36 | + SystemPromptTokens: 8000, |
| 37 | + SystemToolsTokens: 17600, |
| 38 | + SystemToolsDeferredTokens: 19200, |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +// ccBuiltinFor returns the constants for the given CC version. Falls |
| 43 | +// through to the highest known version (sorted lexically — fine while |
| 44 | +// the table is small) when an exact match isn't found; returns the zero |
| 45 | +// value when the table is empty. Callers should check `version != ""` on |
| 46 | +// the returned snapshot to distinguish "estimated" from "unknown". |
| 47 | +func ccBuiltinFor(version string) (ccBuiltinConstants, string) { |
| 48 | + if c, ok := ccBuiltinByVersion[version]; ok { |
| 49 | + return c, version |
| 50 | + } |
| 51 | + // Patch-version fallback: 2.1.151 → use the closest known 2.1.* row. |
| 52 | + // Major.Minor match is a reasonable proxy because the system prompt |
| 53 | + // and tool catalog rarely change inside a minor. |
| 54 | + if best, key := closestKnownVersion(version); key != "" { |
| 55 | + return best, key |
| 56 | + } |
| 57 | + return ccBuiltinConstants{}, "" |
| 58 | +} |
| 59 | + |
| 60 | +// closestKnownVersion returns the highest known version that shares the |
| 61 | +// same major.minor as want. Returns ("", "") if no such version exists. |
| 62 | +func closestKnownVersion(want string) (ccBuiltinConstants, string) { |
| 63 | + if want == "" { |
| 64 | + return ccBuiltinConstants{}, "" |
| 65 | + } |
| 66 | + wantMaj, wantMin, _ := splitSemver(want) |
| 67 | + if wantMaj < 0 { |
| 68 | + return ccBuiltinConstants{}, "" |
| 69 | + } |
| 70 | + bestKey := "" |
| 71 | + var bestVal ccBuiltinConstants |
| 72 | + bestPatch := -1 |
| 73 | + for k, v := range ccBuiltinByVersion { |
| 74 | + maj, min, patch := splitSemver(k) |
| 75 | + if maj != wantMaj || min != wantMin { |
| 76 | + continue |
| 77 | + } |
| 78 | + if patch > bestPatch { |
| 79 | + bestPatch = patch |
| 80 | + bestKey = k |
| 81 | + bestVal = v |
| 82 | + } |
| 83 | + } |
| 84 | + return bestVal, bestKey |
| 85 | +} |
| 86 | + |
| 87 | +func splitSemver(v string) (maj, min, patch int) { |
| 88 | + maj, min, patch = -1, -1, -1 |
| 89 | + parts := strings.SplitN(v, ".", 3) |
| 90 | + if len(parts) < 2 { |
| 91 | + return |
| 92 | + } |
| 93 | + var err error |
| 94 | + if maj, err = strconv.Atoi(parts[0]); err != nil { |
| 95 | + maj = -1 |
| 96 | + return |
| 97 | + } |
| 98 | + if min, err = strconv.Atoi(parts[1]); err != nil { |
| 99 | + min = -1 |
| 100 | + return |
| 101 | + } |
| 102 | + if len(parts) == 3 { |
| 103 | + patch, _ = strconv.Atoi(parts[2]) |
| 104 | + } |
| 105 | + return |
| 106 | +} |
| 107 | + |
| 108 | +// findCCVersion returns the first non-empty `version` field stamped on |
| 109 | +// any transcript entry. Claude Code puts the CLI version on every user |
| 110 | +// and assistant entry, so any pass-through tells us the binary that wrote |
| 111 | +// the session. |
| 112 | +func findCCVersion(entries []TranscriptEntry) string { |
| 113 | + for _, e := range entries { |
| 114 | + if e.Version != "" { |
| 115 | + return e.Version |
| 116 | + } |
| 117 | + } |
| 118 | + return "" |
| 119 | +} |
| 120 | + |
| 121 | +// extractCCBuiltinSnapshot returns the `cc.cc_builtin` block — |
| 122 | +// approximated, version-keyed costs for the bundled system prompt and |
| 123 | +// tool catalog. Marked `estimated: true` so dashboards can distinguish |
| 124 | +// these from transcript-derived numbers. Returns nil when the version |
| 125 | +// is unknown (better to under-report than ship made-up numbers). |
| 126 | +// |
| 127 | +// total_tokens splits the same way /context does: always_on is what |
| 128 | +// ships in the request envelope every turn (and bills against |
| 129 | +// input_tokens / cache_*); deferred_tools is what WOULD cost if loaded |
| 130 | +// via ToolSearch, but normally doesn't ship at all. Summing total + |
| 131 | +// deferred would over-count by the deferred bucket — same trap we |
| 132 | +// addressed in buildContextSnapshot. |
| 133 | +func extractCCBuiltinSnapshot(entries []TranscriptEntry) map[string]interface{} { |
| 134 | + version := findCCVersion(entries) |
| 135 | + consts, matched := ccBuiltinFor(version) |
| 136 | + if matched == "" { |
| 137 | + return nil |
| 138 | + } |
| 139 | + alwaysOn := consts.SystemPromptTokens + consts.SystemToolsTokens |
| 140 | + return map[string]interface{}{ |
| 141 | + "summary": map[string]interface{}{ |
| 142 | + "total_tokens": alwaysOn, // ← matches /context and API billing |
| 143 | + "deferred_tokens": consts.SystemToolsDeferredTokens, |
| 144 | + "estimated": true, |
| 145 | + "cc_version": version, |
| 146 | + "matched_table": matched, |
| 147 | + "system_prompt": consts.SystemPromptTokens, |
| 148 | + "system_tools": consts.SystemToolsTokens, |
| 149 | + "deferred_tools": consts.SystemToolsDeferredTokens, |
| 150 | + }, |
| 151 | + } |
| 152 | +} |
0 commit comments