MCPcopy Index your code
hub / github.com/coder/coder / contextTokensFromUsage

Function contextTokensFromUsage

coderd/x/chatd/chatloop/compaction.go:235–256  ·  view source on GitHub ↗

contextTokensFromUsage returns the total context token count from a step's usage report. It sums input, cache-read, and cache-creation tokens when available, falling back to TotalTokens if none of the granular fields are set.

(usage fantasy.Usage)

Source from the content-addressed store, hash-verified

233// cache-creation tokens when available, falling back to TotalTokens
234// if none of the granular fields are set.
235func contextTokensFromUsage(usage fantasy.Usage) int64 {
236 total := int64(0)
237 hasContextTokens := false
238
239 if usage.InputTokens > 0 {
240 total += usage.InputTokens
241 hasContextTokens = true
242 }
243 if usage.CacheReadTokens > 0 {
244 total += usage.CacheReadTokens
245 hasContextTokens = true
246 }
247 if usage.CacheCreationTokens > 0 {
248 total += usage.CacheCreationTokens
249 hasContextTokens = true
250 }
251 if !hasContextTokens && usage.TotalTokens > 0 {
252 total = usage.TotalTokens
253 }
254
255 return total
256}
257
258// resolveContextLimit picks the first positive value from metadata,
259// configured limit, and fallback — in that priority order. Returns

Callers 1

tryCompactFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected