This commit is contained in:
greeneclipse committed 2026-10-16 00:55:28 +02:00
1 parent 1e7c7bbfbb
commit feebf7d3c1
12 files changed
+248 -70

No files matched your search

+11 -2
View File
@@ -130,11 +130,12 @@ const insertCallSQL = `
INSERT INTO ai_call_logs (
company_id, user_id, job_id, raw_product_id, role, provider_mode, model,
system_prompt, user_prompt, response_text, error, finish_reason,
prompt_tokens, completion_tokens, total_tokens, duration_ms)
prompt_tokens, completion_tokens, total_tokens, duration_ms,
cached_prompt_tokens, cost_micros)
VALUES (
$1, $2, $3, $4, $5, $6, $7,
$8, $9, $10, $11, $12,
$13, $14, $15, $16)`
$13, $14, $15, $16, $17, $18)`
// Record persists one call, merging any CallContext on ctx. Capture must never
// break the request it is observing: every failure is swallowed after one log line.
@@ -167,11 +168,19 @@ func (r *Recorder) Record(ctx context.Context, c Call) {
writeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()
// Persist the exact model-rate cost at capture time. Historical call rows stay
// NULL rather than displaying a fabricated per-call amount.
price := r.priceFor(ctx, c.Model)
var recordedCost any
if price.InputPerMTok > 0 || price.CachedInputPerMTok > 0 || price.OutputPerMTok > 0 {
recordedCost = CostMicros(price, c.PromptTokens, c.CachedPromptTokens, c.OutputTokens)
}
_, err := r.pool.Exec(writeCtx, insertCallSQL,
nullUUID(c.CompanyID), nullUUID(c.UserID), nullUUID(c.JobID), nullUUID(c.RawProductID),
c.Role, c.ProviderMode, c.Model,
clamp(c.System), clamp(c.User), clamp(c.Response), clamp(c.Error), c.FinishReason,
c.PromptTokens, c.OutputTokens, c.TotalTokens, int(c.Duration.Milliseconds()),
c.CachedPromptTokens, recordedCost,
)
if err != nil {
r.noteFailure(err)