mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Three places report what a call cost the customer: the debit, the warehouse row and the o11y span. Two of them recomputed it from the cent-precision rate table while the third used the exact figure the caller supplied, so one call carried two different billed amounts — and emitGenAISpan reported BOTH of them, a billed_cost from the table beside a margin from the exact value. The gap is not rounding. When a model has no configured price the table bills the conservative $1/$4 default, so a turn that really charged $0.00132 was reported at $0.50: nearly four hundred times the invoice, in the number an operator reads to decide whether a customer is profitable. usageMargin was already the one place that honors an exact amount. usageBilledUSD and the span now read it instead of recomputing its table leg, so the debit, the warehouse and the span carry one number. The span's derivation is a value (spanMoney) rather than four lines of arithmetic inside the emit, so it can be asked what it reports. Exactness is also PRESENCE now, not positivity. A turn that billed exactly nothing — a free tier, a zero-price model — knows its amount as precisely as any other, and an int64 read as "unset at zero" sent it back to the table, which invented $0.50 for a call that charged nothing. A pointer can say zero. Co-authored-by: Hanzo Dev <[email protected]>
76 lines
2.8 KiB
Go
76 lines
2.8 KiB
Go
// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// ServedUsage is the trace input for a request that was BILLED ELSEWHERE — a
|
|
// co-resident subsystem that debits commerce itself (zen's Meter in the unified
|
|
// cloud binary) but must still land in the ONE usage warehouse + o11y span
|
|
// plane this package owns. Money is exact nano-USD when the biller knows it
|
|
// (zen computes both retail and upstream COGS per served tier); 0 falls back to
|
|
// the rate-table recompute.
|
|
type ServedUsage struct {
|
|
Owner string // billing org (who paid) — the warehouse partition key
|
|
User string // actor, "owner/name" when known
|
|
Model string // the SKU the caller requested (zen5, …)
|
|
Provider string // serving family label ("zen")
|
|
RequestID string
|
|
Status string // "success" | "error"
|
|
ErrorMsg string
|
|
PromptTokens int
|
|
CompletionTokens int
|
|
BilledNano int64 // exact retail billed, nano-USD; 0 = recompute from rates
|
|
CostNano int64 // exact upstream COGS, nano-USD; 0 = recompute from rates
|
|
StartTime time.Time
|
|
}
|
|
|
|
// TraceServedUsage emits the warehouse row (hanzo.cloud_usage) + gen_ai span for
|
|
// a self-billed request — recordTrace WITHOUT recordUsage, so the commerce debit
|
|
// the caller already made is never doubled. One warehouse writer, two billers,
|
|
// zero double-billing. Safe under a nil/zero input: an empty Owner still writes
|
|
// an (unattributed) row so traffic is never silently invisible; StartTime zero
|
|
// anchors at now.
|
|
func TraceServedUsage(ctx context.Context, in ServedUsage) {
|
|
if in.StartTime.IsZero() {
|
|
in.StartTime = time.Now().UTC()
|
|
}
|
|
if in.RequestID == "" {
|
|
in.RequestID = uuid.NewString()
|
|
}
|
|
rec := &usageRecord{
|
|
Owner: in.Owner,
|
|
Organization: in.Owner,
|
|
User: in.User,
|
|
Model: in.Model,
|
|
Provider: in.Provider,
|
|
PromptTokens: in.PromptTokens,
|
|
CompletionTokens: in.CompletionTokens,
|
|
TotalTokens: in.PromptTokens + in.CompletionTokens,
|
|
Currency: "USD",
|
|
Status: in.Status,
|
|
ErrorMsg: in.ErrorMsg,
|
|
RequestID: in.RequestID,
|
|
BilledNanoExact: &in.BilledNano,
|
|
CostNanoExact: &in.CostNano,
|
|
}
|
|
recordTrace(ctx, rec, in.StartTime)
|
|
}
|