mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
A call through the gateway bills the price of the arm that answered and the platform's fee on top, and nothing else: 5.5% on Hanzo's accounts, 1% on the org's own key, both the host's settings (object.Fee: hanzo, hanzo_byok). A vendor's fee is part of the cost: a call OpenRouter's credit bought cost its stated price plus 5.5%, and the platform's fee goes on that. - Enso answers bill their cost of record (X-Hanzo-Cogs and the buying vendor's fee) times the platform's fee; a maker's answer the same; a resold OpenRouter SKU its vendor price times both fees. An answer that states no cost bills the SKU's listed ceiling. A call billed below its cost writes loss_nano and a warning. - hanzo.cloud_usage gains reasoning, reasoning_tokens, served, vendor, arms and loss_nano: each Enso row names the arm that answered, its rate at the time and the tokens it was paid for, and each arm that failed first. - rates.go prices every Enso SKU at each depth: from (its cheapest arm), up to (its dearest, a long prompt included), and typical (its measured cost a token over the last day, or its last 50 calls), the arms read from the engine's admin catalog. /v1/models lists the typical rate with from, up_to, fee and window beside it; Prices, Price and Blend serve the rest. The board's ledger counts answers, failovers, OpenRouter-bought answers, fee and losses by model and vendor.
70 lines
2.8 KiB
Go
70 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 (
|
|
"strconv"
|
|
|
|
iam "github.com/hanzoai/ai/internal/iam"
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/hanzoai/decimal"
|
|
)
|
|
|
|
// globalProviderOwner is the sentinel Owner of the built-in (Hanzo-served)
|
|
// providers seeded by object.initLLMProviders. A resolved provider owned by this
|
|
// sentinel is a Hanzo account (bought via Hanzo credits); a provider owned by the
|
|
// caller's own org is a customer-connected BYO account. Confirmed against
|
|
// object/provider_default.go (GetModelProviderByNameForOrg falls back to
|
|
// getProvider("admin", name) for the global built-in).
|
|
const globalProviderOwner = "admin"
|
|
|
|
// platformFeeCents returns the Hanzo platform surcharge, in integer cents, for a
|
|
// completion. BYO (a customer-connected third-party account executed the call) →
|
|
// the platform's BYOK fee (fee.go: the host's `hanzo_byok`, 1%) of the provider
|
|
// list-price cost, rounded UP so a non-zero fee never yields a zero cent. Buying via
|
|
// Hanzo credits (byo=false) → 0 here: that call's price carries its own fee.
|
|
func platformFeeCents(costCents int64, byo bool) int64 {
|
|
if !byo || costCents <= 0 {
|
|
return 0
|
|
}
|
|
pct := platform(true).Sub(decimal.New(1, 0))
|
|
if pct.Sign() <= 0 {
|
|
return 0
|
|
}
|
|
fee := decimal.New(costCents, 0).Mul(pct)
|
|
whole := fee.Quo(decimal.New(1, 0), 0)
|
|
if whole.Cmp(fee) < 0 {
|
|
whole = whole.Add(decimal.New(1, 0))
|
|
}
|
|
n, _ := strconv.ParseInt(whole.String(), 10, 64)
|
|
return n
|
|
}
|
|
|
|
// providerBYO classifies a resolved (provider, billed-user) pair for metering.
|
|
// BYO is true only when the resolved provider row is owned by the caller's OWN
|
|
// org — i.e. the org connected its own third-party account (GetModelProvider
|
|
// ByNameForOrg returned the org's override) rather than being served by the
|
|
// global Hanzo built-in. account is the attribution label persisted on the usage
|
|
// record: "<owner>/<name>" for a BYO account, else "hanzo". Pure/total; a nil
|
|
// provider or nil user is Hanzo-served.
|
|
func providerBYO(provider *object.Provider, user *iam.User) (byo bool, account string) {
|
|
if provider != nil && user != nil &&
|
|
provider.Owner != "" && provider.Owner != globalProviderOwner &&
|
|
provider.Owner == user.Owner {
|
|
return true, provider.Owner + "/" + provider.Name
|
|
}
|
|
return false, "hanzo"
|
|
}
|