Files
Calcium-Ion e9d4d6353c feat(billing): bill OpenAI 12-hour prompt cache writes at their own price (#7965)
OpenAI added a 12-hour prompt cache to the Responses API on 2026-10-20
(prompt_cache_options.ttl "12h", GPT-5.6 and later). A 12-hour write
costs 2.5x the input price, twice the 30-minute write. We already
forwarded the option but billed every cache write at the 30-minute price.

- Read cache_write_tokens_details from Responses usage and keep the split
  through streaming, compaction and format conversion.
- Ratio pricing bills 12-hour writes at twice the cache write ratio, the
  same way Claude 1-hour writes use 6/3.75.
- Expressions get a cc12h price. When an expression leaves the long-lived
  price out, 12-hour writes cost twice the cc price and Claude 1-hour
  writes 6/3.75 times it; without a cc price they bill as input. Claude
  cache writes are no longer free when an expression omits cc or cc1h.
- gpt-6-astra prices 12-hour writes at $25 ($50 above 272K tokens).
- GPT models with a cache write ratio convert to expressions with cc12h.
- Chat Completions forwards prompt_cache_options.
- The pricing editor, cost estimator, pricing pages and usage log details
  show the 12-hour price and tokens.
2026-10-22 22:41:17 +08:00

122 lines
3.4 KiB
Go

package types
import (
"fmt"
"math"
"github.com/shopspring/decimal"
)
// Long-lived cache writes cost a fixed multiple of the short write price.
const (
// https://docs.claude.com/en/docs/build-with-claude/prompt-caching#1-hour-cache-duration
ClaudeCacheCreation1hMultiplier = 6 / 3.75
// https://developers.openai.com/api/docs/guides/prompt-caching
OpenAICacheCreation12hMultiplier = 2
)
type GroupRatioInfo struct {
GroupRatio float64
GroupSpecialRatio float64
HasSpecialRatio bool
}
type PriceData struct {
FreeModel bool
ModelPrice float64
ModelRatio float64
CompletionRatio float64
CacheRatio float64
CacheCreationRatio float64
CacheCreation5mRatio float64
CacheCreation1hRatio float64
CacheCreation12hRatio float64
ImageRatio float64
AudioRatio float64
AudioCompletionRatio float64
otherRatios map[string]float64
UsePrice bool
Quota int // 按次计费的最终额度(MJ / Task)
QuotaToPreConsume int // 按量计费的预消耗额度
GroupRatioInfo GroupRatioInfo
}
func (p *PriceData) AddOtherRatio(key string, ratio float64) {
if !isValidOtherRatio(ratio) {
return
}
if p.otherRatios == nil {
p.otherRatios = make(map[string]float64)
}
p.otherRatios[key] = ratio
}
func (p *PriceData) ReplaceOtherRatios(ratios map[string]float64) bool {
p.otherRatios = nil
for key, ratio := range ratios {
p.AddOtherRatio(key, ratio)
}
return len(p.otherRatios) > 0
}
func (p *PriceData) HasOtherRatio(key string) bool {
ratio, ok := p.otherRatios[key]
return ok && isValidOtherRatio(ratio)
}
func (p *PriceData) OtherRatios() map[string]float64 {
if len(p.otherRatios) == 0 {
return nil
}
ratios := make(map[string]float64, len(p.otherRatios))
for key, ratio := range p.otherRatios {
if isValidOtherRatio(ratio) {
ratios[key] = ratio
}
}
if len(ratios) == 0 {
return nil
}
return ratios
}
func (p *PriceData) OtherRatioMultiplier() float64 {
multiplier := 1.0
for _, ratio := range p.otherRatios {
if isValidOtherRatio(ratio) && ratio != 1.0 {
multiplier *= ratio
}
}
return multiplier
}
func (p *PriceData) ApplyOtherRatiosToFloat(value float64) float64 {
return value * p.OtherRatioMultiplier()
}
func (p *PriceData) ApplyOtherRatiosToDecimal(value decimal.Decimal) decimal.Decimal {
for _, ratio := range p.otherRatios {
if isValidOtherRatio(ratio) && ratio != 1.0 {
value = value.Mul(decimal.NewFromFloat(ratio))
}
}
return value
}
func (p *PriceData) RemoveOtherRatiosFromFloat(value float64) float64 {
for _, ratio := range p.otherRatios {
if isValidOtherRatio(ratio) && ratio != 1.0 {
value /= ratio
}
}
return value
}
func isValidOtherRatio(ratio float64) bool {
return ratio > 0 && !math.IsInf(ratio, 1)
}
func (p *PriceData) ToSetting() string {
return fmt.Sprintf("ModelPrice: %f, ModelRatio: %f, CompletionRatio: %f, CacheRatio: %f, GroupRatio: %f, UsePrice: %t, CacheCreationRatio: %f, CacheCreation5mRatio: %f, CacheCreation1hRatio: %f, CacheCreation12hRatio: %f, QuotaToPreConsume: %d, ImageRatio: %f, AudioRatio: %f, AudioCompletionRatio: %f", p.ModelPrice, p.ModelRatio, p.CompletionRatio, p.CacheRatio, p.GroupRatioInfo.GroupRatio, p.UsePrice, p.CacheCreationRatio, p.CacheCreation5mRatio, p.CacheCreation1hRatio, p.CacheCreation12hRatio, p.QuotaToPreConsume, p.ImageRatio, p.AudioRatio, p.AudioCompletionRatio)
}