Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950package dataplane
import "encoding/json"
// RateLimitEvent reads the usage reading from one line of// `claude -p --output-format stream-json --verbose`. It answers false// for any other line, and for a rate_limit_event without the seven-day// window, which is what an API key's session prints. A plan with no// five-hour limit reports the seven-day window alone, and its reading// has a FiveHour with no reset time.func RateLimitEvent(line []byte) (RateLimitInfo, bool) { var event struct { Type string `json:"type"` RateLimitInfo RateLimitInfo `json:"rate_limit_info"` } if json.Unmarshal(line, &event) != nil || event.Type != "rate_limit_event" { return RateLimitInfo{}, false } windows := event.RateLimitInfo.UnifiedWindows return event.RateLimitInfo, windows.SevenDay.ResetsAt != 0}
// ResultUsage reads the token total and the cost from the result line// that ends `claude -p --output-format stream-json`: the input, output,// and cache tokens of every model, and the sum of each model's cost. A// resumed conversation keeps the usage of its earlier turns, so both// count every turn of it. A subscription pays nothing for a turn, and// the cost is still what the API would charge for it. It answers false// for any other line, and for a result without model usage.func ResultUsage(line []byte) (tokens int64, costUSD float64, ok bool) { var result struct { Type string `json:"type"` ModelUsage map[string]struct { InputTokens int64 `json:"inputTokens"` OutputTokens int64 `json:"outputTokens"` CacheReadInputTokens int64 `json:"cacheReadInputTokens"` CacheCreationInputTokens int64 `json:"cacheCreationInputTokens"` CostUSD float64 `json:"costUSD"` } `json:"modelUsage"` } if json.Unmarshal(line, &result) != nil || result.Type != "result" || len(result.ModelUsage) == 0 { return 0, 0, false } for _, m := range result.ModelUsage { tokens += m.InputTokens + m.OutputTokens + m.CacheReadInputTokens + m.CacheCreationInputTokens costUSD += m.CostUSD } return tokens, costUSD, true}