feat(statestore): record a usage row for every model call

Task agent runs and MCP sampling record against their task (billed to the
task's org); gateway chat turns, titles, sampling and curators record
against the system org. Rows carry source, workflow, step, alias,
provider/model and tokens for later chargeback.
This commit is contained in:
sam committed 2026-10-10 18:32:43 +11:00
1 parent 12b9a57404
commit e4ea2eaabc
31 files changed
+1068 -322

No files matched your search

@@ -0,0 +1,58 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: usage.sql
package postgresdb
import (
"context"
"time"
)
const insertModelUsage = `-- name: InsertModelUsage :exec
INSERT INTO model_usage (
org_id, source, task_id, attempt, workflow, step, alias, provider, model,
input_tokens, output_tokens, cached_tokens, at
) VALUES (
COALESCE((SELECT t.org_id FROM tasks t WHERE t.id = $1 AND $1 <> 0), $2::text),
$3, $1, $4, $5, $6, $7,
$8, $9, $10, $11, $12, $13
)
`
type InsertModelUsageParams struct {
TaskID int64
OrgID string
Source string
Attempt int64
Workflow string
Step string
Alias string
Provider string
Model string
InputTokens int64
OutputTokens int64
CachedTokens int64
At time.Time
}
// A task's usage belongs to the task's org, whatever the caller says.
func (q *Queries) InsertModelUsage(ctx context.Context, arg InsertModelUsageParams) error {
_, err := q.db.Exec(ctx, insertModelUsage,
arg.TaskID,
arg.OrgID,
arg.Source,
arg.Attempt,
arg.Workflow,
arg.Step,
arg.Alias,
arg.Provider,
arg.Model,
arg.InputTokens,
arg.OutputTokens,
arg.CachedTokens,
arg.At,
)
return err
}