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:
1 parent
12b9a57404
commit
e4ea2eaabc
31 files changed
+1068
-322
No files matched your search
@@ -0,0 +1,46 @@
|
||||
// Package usage records model calls for attribution and chargeback.
|
||||
package usage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Source is what made a model call.
|
||||
type Source string
|
||||
|
||||
const (
|
||||
SourceTask Source = "task"
|
||||
SourceChat Source = "chat"
|
||||
SourceCurator Source = "curator"
|
||||
)
|
||||
|
||||
// Record is one model call. A task's record belongs to the task's org,
|
||||
// whatever Org says.
|
||||
type Record struct {
|
||||
Org string
|
||||
Source Source
|
||||
TaskID int64
|
||||
Attempt int
|
||||
Workflow string
|
||||
Step string
|
||||
Alias string
|
||||
Provider string
|
||||
Model string
|
||||
InputTokens int64
|
||||
OutputTokens int64
|
||||
CachedTokens int64
|
||||
At time.Time
|
||||
}
|
||||
|
||||
// Recorder appends usage records.
|
||||
type Recorder interface {
|
||||
RecordUsage(ctx context.Context, r Record) error
|
||||
}
|
||||
|
||||
// WithRef fills Provider and Model from a "provider/model" ref.
|
||||
func (r Record) WithRef(ref string) Record {
|
||||
r.Provider, r.Model, _ = strings.Cut(ref, "/")
|
||||
return r
|
||||
}
|
||||
@@ -68,7 +68,7 @@ func (a AgentStage) Stage() Stage {
|
||||
}
|
||||
// The agent call is a child of the stage's recorded step: the store
|
||||
// says what the stage's runtime did, not just what the stage did.
|
||||
res, runErr := tc.RunAgentChild(ctx, a.Name, func() (agentrun.Result, error) {
|
||||
res, runErr := tc.RunAgentChild(ctx, a.Name, a.Model, modelRef, func() (agentrun.Result, error) {
|
||||
return tc.Agent.Run(ctx, tc.Dir, req, tc.toolCallReporter(a.Name))
|
||||
})
|
||||
return a.handleResult(ctx, tc, req, res, runErr, modelRef)
|
||||
|
||||
@@ -95,7 +95,7 @@ func runBaselineFix(ctx context.Context, tc *TaskContext, argv []string, out []b
|
||||
Protection: agentrun.Protection{Suffixes: append([]string(nil), tc.Repo.Protect...)},
|
||||
}
|
||||
|
||||
res, agentErr := tc.RunAgentChild(ctx, "baseline-fix", func() (agentrun.Result, error) {
|
||||
res, agentErr := tc.RunAgentChild(ctx, "baseline-fix", "", modelRef, func() (agentrun.Result, error) {
|
||||
return tc.Agent.Run(ctx, tc.Dir, req, tc.toolCallReporter("baseline-fix"))
|
||||
})
|
||||
if agentErr != nil && res.Version == 0 {
|
||||
|
||||
@@ -757,7 +757,7 @@ func runPRReviewAgentRecorded(
|
||||
Budget: agentrun.Budget{MaxSteps: maxSteps, WallClock: tc.Cfg.Budgets.WallClock.Std()},
|
||||
CaptureTools: captureTools,
|
||||
}
|
||||
res, err := tc.RunAgentChild(ctx, name, func() (agentrun.Result, error) {
|
||||
res, err := tc.RunAgentChild(ctx, name, "", modelRef, func() (agentrun.Result, error) {
|
||||
return tc.Agent.Run(ctx, workspace, req, tc.toolCallReporter(name))
|
||||
})
|
||||
tc.prReview.tokensSpent.Add(int64(res.TokensUsed))
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/usage"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
@@ -44,6 +45,8 @@ func (f *fakeStepStore) FinishStep(_ context.Context, s task.StepFinish) (events
|
||||
return events.Event{ID: s.StepID}, nil
|
||||
}
|
||||
|
||||
func (f *fakeStepStore) RecordUsage(context.Context, usage.Record) error { return nil }
|
||||
|
||||
// TestRunBranchRecordsChildSteps pins the step contract a branch owes the
|
||||
// canvas: every branch step is a stage row parented to the parallel step and
|
||||
// named for its branch, and its outcome is recorded.
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/usage"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
@@ -185,6 +186,8 @@ type Store interface {
|
||||
// the stage_finish event in the same transaction, and returns that event
|
||||
// for the caller's post-commit publish.
|
||||
FinishStep(ctx context.Context, s StepFinish) (events.Event, error)
|
||||
// RecordUsage appends one model call's usage record.
|
||||
RecordUsage(ctx context.Context, r usage.Record) error
|
||||
}
|
||||
|
||||
// Definition is a workflow's id and stage order.
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
||||
"github.com/samcharles93/archie-core/internal/domain/usage"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
@@ -568,7 +569,7 @@ var goTestOKLine = regexp.MustCompile(`^ok\s+\S+`)
|
||||
|
||||
// RunAgentChild records one agent call as a child step of the current stage.
|
||||
// A failed write parks the execution. Outside a recorded run it just runs.
|
||||
func (tc *TaskContext) RunAgentChild(ctx context.Context, name string, run func() (agentrun.Result, error)) (agentrun.Result, error) {
|
||||
func (tc *TaskContext) RunAgentChild(ctx context.Context, name, alias, modelRef string, run func() (agentrun.Result, error)) (agentrun.Result, error) {
|
||||
if tc.workflowBranch != "" {
|
||||
name = tc.workflowBranch + "/" + name
|
||||
}
|
||||
@@ -577,6 +578,7 @@ func (tc *TaskContext) RunAgentChild(ctx context.Context, name string, run func(
|
||||
return agentrun.Result{}, err
|
||||
}
|
||||
res, runErr := run()
|
||||
tc.recordUsage(ctx, name, alias, modelRef, res.Usage)
|
||||
to, detail := taskstate.StepSucceeded, res.Summary
|
||||
if runErr != nil {
|
||||
to, detail = taskstate.StepFailed, runErr.Error()
|
||||
@@ -621,3 +623,19 @@ func (tc *TaskContext) finishChildStep(ctx context.Context, stepID int64, to tas
|
||||
publishEvent(tc, event)
|
||||
return nil
|
||||
}
|
||||
|
||||
// recordUsage appends the usage record for one agent run. A failed write is
|
||||
// logged, never fails the run: the work already happened.
|
||||
func (tc *TaskContext) recordUsage(ctx context.Context, step, alias, modelRef string, u agentrun.Usage) {
|
||||
if tc.Store == nil {
|
||||
return
|
||||
}
|
||||
record := usage.Record{
|
||||
Source: usage.SourceTask, TaskID: tc.Task.ID, Attempt: tc.Task.Attempt, Workflow: tc.Task.Workflow,
|
||||
Step: step, Alias: alias, InputTokens: int64(u.PromptTokens), OutputTokens: int64(u.CompletionTokens),
|
||||
CachedTokens: int64(u.CachedTokens), At: time.Now().UTC(),
|
||||
}.WithRef(modelRef)
|
||||
if err := tc.Store.RecordUsage(ctx, record); err != nil && tc.Log != nil {
|
||||
tc.Log.Warn("record model usage failed", "step", step, "model", modelRef, "err", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user