Files
ai/controllers/message_dryrun.go
zeekayandHanzo Dev 2ff73320e6 one /v1/answer request is one completion and one debit
GetAnswer called object.GetAnswer twice — two full upstream completions per
request, the first one's answer and price discarded by the second — and then
folded the price into chat.Price and stopped. No debit ever fired: the surface
ran a model the client names in a query param, priced it, recorded the price,
and charged nobody. Only RequireSignedIn stood between an account and unlimited
inference on the priciest model it could name.

The completion now runs once, and it runs behind the same pre-flight balance
gate every chat answer runs: the provider's own dry run over the same question
and prompt the real call will carry, refused when the payer cannot cover the
estimate. The answer is then charged through AddTransactionForMessage — the ONE
debit of the message-answer path — for its own price, keyed on the answer
message. Persisted before charged, so a failed debit leaves a row the retry cron
finds.

Two things were braided that this needed apart. gateBalance is the pre-flight
gate itself, and validateTransactionBeforeAIGeneration is now the chat turn's
caller of it, so both surfaces gate through one function instead of one surface
having a gate and the other having none. object.QueryAnswer runs a completion on
an already-resolved provider, so a caller that must price a call before making
it resolves the provider once — to estimate against, then to answer with —
rather than once per concern.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-06 05:21:19 -07:00

117 lines
4.2 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2025 The OpenAgent Authors. 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 (
"fmt"
"github.com/hanzoai/ai/model"
"github.com/hanzoai/ai/object"
)
// dryRunWriter is a dummy writer that implements both io.Writer and http.Flusher
// It discards all writes and is used for dry run queries that need a Flusher
type dryRunWriter struct{}
func (w *dryRunWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (w *dryRunWriter) Flush() {}
// shouldPerformDryRun determines if a dry run estimation should be performed
// before generating the actual AI answer. Dry run is skipped for:
// - Dummy providers (no real AI calls)
// - Reason models (they have different execution paths)
// - Queries with agent clients (agent-based workflows)
func shouldPerformDryRun(providerType string, modelSubType string, hasAgentClients bool) bool {
return providerType != "Dummy" && !isReasonModel(modelSubType) && !hasAgentClients
}
// gateBalance refuses an AI generation BEFORE it spends upstream when the payer
// cannot cover what that generation is estimated to cost. It is the ONE pre-flight
// money gate every answer surface runs, so no surface can generate for free.
//
// The estimate is the provider's OWN dry run over the same question, history and
// prompt the real call will carry, so the number gated on is the number billed —
// never a rate-table guess keyed on a provider name. A provider with no dry run
// (Dummy, reason models) has nothing to estimate and is not gated.
//
// owner/user name the payer, and they are the SAME pair AddTransactionForMessage
// debits: a call this gate admits depletes the account this gate read.
func gateBalance(
owner string,
user string,
question string,
history []*model.RawMessage,
prompt string,
modelProvider *object.Provider,
modelProviderObj model.ModelProvider,
acceptLanguage string,
) error {
if !shouldPerformDryRun(modelProvider.Type, modelProvider.SubType, false) {
return nil
}
// The dry run marker makes the provider estimate the call instead of running it.
// dryRunWriter is an io.Writer AND an http.Flusher — some providers require both
// even when they answer nothing.
estimate, err := modelProviderObj.QueryText(model.DryRunPrefix+question, &dryRunWriter{}, history, prompt, nil, nil, acceptLanguage)
if err != nil {
return fmt.Errorf("failed to estimate token count: %s", err.Error())
}
return object.ValidateTransactionForMessage(&object.Message{
Owner: owner,
User: user,
ModelProvider: modelProvider.Name,
Price: estimate.TotalPrice,
Currency: estimate.Currency,
})
}
// validateTransactionBeforeAIGeneration runs gateBalance for a chat turn, whose
// history is the chat's own recent messages and whose prompt is the store's. It
// reports refusal on the message's event stream rather than as a bare error.
func validateTransactionBeforeAIGeneration(
message *object.Message,
chat *object.Chat,
store *object.Store,
question string,
modelProvider *object.Provider,
modelProviderObj model.ModelProvider,
acceptLanguage string,
responseErrorFunc func(*object.Message, string),
) error {
if !shouldPerformDryRun(modelProvider.Type, modelProvider.SubType, false) {
return nil
}
history, err := object.GetRecentRawMessages(chat.Name, message.CreatedTime, store.MemoryLimit)
if err != nil {
responseErrorFunc(message, err.Error())
return err
}
err = gateBalance(message.Owner, message.User, question, history, store.Prompt, modelProvider, modelProviderObj, acceptLanguage)
if err != nil {
responseErrorFunc(message, err.Error())
return err
}
return nil
}