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

112 lines
5.2 KiB
Go

package billingexpr
import (
"crypto/sha256"
"fmt"
"github.com/QuantumNous/new-api/common"
)
type RequestInput struct {
Headers map[string]string
Body []byte
Usage map[string]any
// ImageCount is a validated billing quantity, separate from the frozen
// request's n. Settlement can replace it with the actual returned count.
ImageCount *int
}
// TokenParams holds all token dimensions passed into an Expr evaluation.
// Fields beyond P and C are optional — when absent they default to 0,
// which means cache-unaware expressions keep working unchanged.
type TokenParams struct {
P float64 // prompt tokens (text) — auto-excludes sub-categories priced separately
C float64 // completion tokens (text) — auto-excludes sub-categories priced separately
Len float64 // total input context length for tier conditions (non-Claude: raw prompt_tokens; Claude: text + cache read + cache creation)
CR float64 // cache read (hit) tokens
CC float64 // cache creation tokens (5-min TTL for Claude, 30-min TTL for OpenAI, generic for others)
CC1h float64 // cache creation tokens — 1-hour TTL (Claude only)
CC12h float64 // cache creation tokens — 12-hour TTL (OpenAI only)
Img float64 // image input tokens
ImgCR float64 // image cache read tokens, separated only when explicitly priced
ImgO float64 // image output tokens
AI float64 // audio input tokens
AO float64 // audio output tokens
}
// RequestRuleTrace describes one request-dependent multiplier detected at compile time.
type RequestRuleTrace struct {
Cond string `json:"cond"`
Multiplier float64 `json:"multiplier"`
Matched bool `json:"matched"`
}
type BillingUnit string
const (
BillingUnitToken BillingUnit = "token"
BillingUnitRequest BillingUnit = "request"
)
// TraceResult holds side-channel info captured while an expression runs.
type TraceResult struct {
ImageCount *int `json:"image_count,omitempty"`
BillingUnit BillingUnit `json:"billing_unit"`
FixedPrice *float64 `json:"fixed_price,omitempty"`
MatchedTier string `json:"matched_tier"`
RequestRules []RequestRuleTrace `json:"request_rules,omitempty"`
Cost float64 `json:"cost"`
}
// BillingSnapshot captures billing state at pre-consume time. Expression and
// request fields stay frozen; group-dependent fields are refreshed before an
// auto-group retry and settlement. It is fully serializable and contains no
// compiled program pointers.
type BillingSnapshot struct {
EstimatedImageCount *int `json:"estimated_image_count,omitempty"`
BillingMode string `json:"billing_mode"`
ModelName string `json:"model_name"`
ExprString string `json:"expr_string"`
ExprHash string `json:"expr_hash"`
GroupRatio float64 `json:"group_ratio"`
EstimatedPromptTokens int `json:"estimated_prompt_tokens"`
EstimatedCompletionTokens int `json:"estimated_completion_tokens"`
PreConsumeMultiplier float64 `json:"pre_consume_multiplier,omitempty"` // Reservation only; zero in older snapshots means 1.
EstimatedQuotaBeforeGroup float64 `json:"estimated_quota_before_group"`
EstimatedQuotaAfterGroup int `json:"estimated_quota_after_group"`
EstimatedTier string `json:"estimated_tier"`
EstimatedBillingUnit BillingUnit `json:"estimated_billing_unit,omitempty"`
EstimatedFixedPrice *float64 `json:"estimated_fixed_price,omitempty"`
QuotaPerUnit float64 `json:"quota_per_unit"`
ExprVersion int `json:"expr_version"`
TaskUsageBilling bool `json:"task_usage_billing,omitempty"`
UsageFacts map[string]any `json:"usage_facts,omitempty"`
}
// TieredResult holds everything needed after running tiered settlement.
type TieredResult struct {
// BillingTokens records the actual normalized inputs for successful token
// billing that explicitly references img_cr. Logs serialize these through
// the shared injection path, not as additional snapshot state.
BillingTokens *TokenParams `json:"-"`
ImageCount *int `json:"image_count,omitempty"`
BillingUnit BillingUnit `json:"billing_unit"`
FixedPrice *float64 `json:"fixed_price,omitempty"`
ActualQuotaBeforeGroup float64 `json:"actual_quota_before_group"`
ActualQuotaAfterGroup int `json:"actual_quota_after_group"`
MatchedTier string `json:"matched_tier"`
RequestRules []RequestRuleTrace `json:"request_rules,omitempty"`
CrossedTier bool `json:"crossed_tier"`
// Clamp records a single-request saturation event during quota conversion so the
// caller can surface it on the consume log for admin auditing. Nil when no
// clamping occurred. Not serialized: the marker is attached separately via
// the shared quota-saturation audit path.
Clamp *common.QuotaClamp `json:"-"`
}
// ExprHashString returns the SHA-256 hex digest of an expression string.
func ExprHashString(expr string) string {
h := sha256.Sum256([]byte(expr))
return fmt.Sprintf("%x", h)
}