feat(usage): report model usage per org with identity and cost (#1855)

Model usage was recorded per call and never read, so an org could not see
what it spent. Add the read side:

- model_usage carries identity_id. Task calls take it from the task row,
  chat and curator calls from the calling principal carried in the usage
  record, so an outbox replay keeps its attribution. Existing task rows are
  backfilled from tasks.
- StatsService.UsageTotals sums the caller org's rows by UTC day, source,
  provider, model and identity. The org comes from the principal, with no
  org parameter, so cross-org reads do not exist.
- GET /api/usage prices tokens at the catalog's list rate, netting cache
  hits, and reports calls to unpriced models as tokens only. The published
  catalog view now carries each model's cost.
- A new org Usage page behind a read_usage grant: admin and owner only,
  with a 7, 30 or 90 day range, totals, the daily series, and model, source
  and identity breakdowns. The org shell keeps it on the caller's own org.
This commit is contained in:
sam committed 2026-11-11 02:15:29 +11:00
1 parent 255e0c7ee0
commit 4728c8c253
38 files changed
+1329 -52

No files matched your search

+1
View File
@@ -16,6 +16,7 @@ release's per-component sections are labelled beneath its heading.
- The task bar reads "Describe what Archie should do" and labels its workflow picker. Activity opens on Tasks.
- A reopened conversation shows why a chat turn failed instead of leaving the question unanswered.
- Revoking an API token on the account page works.
- An organisation's model usage is reported on a new Usage page: calls, tokens and cost for the last 7, 30 or 90 days, broken down by day, model, source, and the user or agent the work was requested as. Local models with no catalogue price report tokens only.
### archie-gateway
+8
View File
@@ -15,13 +15,21 @@ func catalogView(snapshot modelcatalog.Snapshot) []webui.CatalogProviderView {
view := make([]webui.CatalogProviderView, 0, len(snapshot.Providers))
for _, provider := range snapshot.Providers {
models := make([]string, 0, len(provider.Models))
costs := make(map[string]webui.ModelCostView, len(provider.Models))
for _, model := range provider.Models {
models = append(models, model.ID)
if model.Cost != (modelcatalog.Cost{}) {
costs[model.ID] = webui.ModelCostView{
Input: model.Cost.Input, Output: model.Cost.Output,
CacheRead: model.Cost.CacheRead, CacheWrite: model.Cost.CacheWrite,
}
}
}
slices.Sort(models)
view = append(view, webui.CatalogProviderView{
ID: provider.ID, Name: provider.Name, Class: provider.Class,
APIKeyEnv: provider.APIKeyEnv, BaseURL: provider.BaseURL, Models: models,
Costs: costs,
})
}
slices.SortFunc(view, func(a, b webui.CatalogProviderView) int { return strings.Compare(a.ID, b.ID) })
+9 -1
View File
@@ -7,6 +7,7 @@ import (
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/usage"
"github.com/samcharles93/archie-core/internal/gateway"
)
@@ -33,8 +34,15 @@ func (u *usageSink) record(ctx context.Context, source usage.Source, ref string,
alias = u.models.ActiveAlias(ctx)
}
}
// The dashboard forwards the caller's principal, so a chat call is
// attributed to whoever asked for it. A channel front-end with no
// principal leaves it unknown rather than guessing.
identity := ""
if p, ok := access.PrincipalFromContext(ctx); ok {
identity = string(p.IdentityID)
}
record := usage.Record{
Source: source, Alias: alias,
Source: source, Alias: alias, Identity: identity,
InputTokens: int64(used.PromptTokens), OutputTokens: int64(used.CompletionTokens),
CachedTokens: int64(used.CachedTokens), At: time.Now().UTC(),
}.WithRef(ref)
+2
View File
@@ -32,6 +32,7 @@ type deps struct {
Options Options
Log *slog.Logger
Store storecontract.TaskStore
Usage storecontract.UsageStore
Chat messaging.ChatContract
Updates *releaseupdate.Service
Health *health.Registry
@@ -57,6 +58,7 @@ type deps struct {
func compose(ctx context.Context, d deps) *webui.Server {
srv := &webui.Server{
Store: d.Store,
Usage: d.Usage,
Log: d.Log,
Authenticate: d.Authenticate,
AuthenticateBrowser: d.AuthenticateBrowser,
+1
View File
@@ -88,6 +88,7 @@ func Run(ctx context.Context, options Options) error {
Options: opts,
Log: log,
Store: tasks,
Usage: tasks,
Chat: chat,
Updates: controlplanerpc.UpdateService(controlplanerpc.NewRPCClient(tasks.ControlPlane()), filepath.Join(configuration.DefaultWorkDir(), "ui-update-deferrals.json")),
Health: newReadinessRegistry(opts, tasks, chat, chain, provider),
+3
View File
@@ -376,6 +376,9 @@ func (b *server) executionDeps(deps *staterpc.Deps) {
if ur, ok := b.st.(usage.Recorder); ok {
deps.Usage = ur
}
if ut, ok := b.st.(storecontract.UsageStore); ok {
deps.UsageTotals = ut
}
if sr, ok := b.st.(storecontract.StepReader); ok {
deps.StepReader = sr
}
+26 -14
View File
@@ -668,18 +668,22 @@ func (x *ListStepsResponse) GetSteps() []*StepExecution {
type UsageRecord struct {
state protoimpl.MessageState `protogen:"open.v1"`
// source is task, chat or curator.
Source string `protobuf:"bytes,2,opt,name=source,proto3" json:"source,omitempty"`
TaskId int64 `protobuf:"varint,3,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"`
Attempt int64 `protobuf:"varint,4,opt,name=attempt,proto3" json:"attempt,omitempty"`
Workflow string `protobuf:"bytes,5,opt,name=workflow,proto3" json:"workflow,omitempty"`
Step string `protobuf:"bytes,6,opt,name=step,proto3" json:"step,omitempty"`
Alias string `protobuf:"bytes,7,opt,name=alias,proto3" json:"alias,omitempty"`
Provider string `protobuf:"bytes,8,opt,name=provider,proto3" json:"provider,omitempty"`
Model string `protobuf:"bytes,9,opt,name=model,proto3" json:"model,omitempty"`
InputTokens int64 `protobuf:"varint,10,opt,name=input_tokens,json=inputTokens,proto3" json:"input_tokens,omitempty"`
OutputTokens int64 `protobuf:"varint,11,opt,name=output_tokens,json=outputTokens,proto3" json:"output_tokens,omitempty"`
CachedTokens int64 `protobuf:"varint,12,opt,name=cached_tokens,json=cachedTokens,proto3" json:"cached_tokens,omitempty"`
At *timestamppb.Timestamp `protobuf:"bytes,13,opt,name=at,proto3" json:"at,omitempty"`
Source string `protobuf:"bytes,2,opt,name=source,proto3" json:"source,omitempty"`
TaskId int64 `protobuf:"varint,3,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"`
Attempt int64 `protobuf:"varint,4,opt,name=attempt,proto3" json:"attempt,omitempty"`
Workflow string `protobuf:"bytes,5,opt,name=workflow,proto3" json:"workflow,omitempty"`
Step string `protobuf:"bytes,6,opt,name=step,proto3" json:"step,omitempty"`
Alias string `protobuf:"bytes,7,opt,name=alias,proto3" json:"alias,omitempty"`
Provider string `protobuf:"bytes,8,opt,name=provider,proto3" json:"provider,omitempty"`
Model string `protobuf:"bytes,9,opt,name=model,proto3" json:"model,omitempty"`
InputTokens int64 `protobuf:"varint,10,opt,name=input_tokens,json=inputTokens,proto3" json:"input_tokens,omitempty"`
OutputTokens int64 `protobuf:"varint,11,opt,name=output_tokens,json=outputTokens,proto3" json:"output_tokens,omitempty"`
CachedTokens int64 `protobuf:"varint,12,opt,name=cached_tokens,json=cachedTokens,proto3" json:"cached_tokens,omitempty"`
At *timestamppb.Timestamp `protobuf:"bytes,13,opt,name=at,proto3" json:"at,omitempty"`
// identity is who the work was requested as: the task's creating identity
// for task calls, the calling principal for chat and curator calls. It
// travels in the record so a queued retry keeps its attribution.
Identity string `protobuf:"bytes,14,opt,name=identity,proto3" json:"identity,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -798,6 +802,13 @@ func (x *UsageRecord) GetAt() *timestamppb.Timestamp {
return nil
}
func (x *UsageRecord) GetIdentity() string {
if x != nil {
return x.Identity
}
return ""
}
type RecordUsageRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
Record *UsageRecord `protobuf:"bytes,1,opt,name=record,proto3" json:"record,omitempty"`
@@ -932,7 +943,7 @@ const file_state_v1_execution_proto_rawDesc = "" +
"\fexecution_id\x18\x01 \x01(\x03R\vexecutionId\x12\x18\n" +
"\aattempt\x18\x02 \x01(\x03R\aattempt\"B\n" +
"\x11ListStepsResponse\x12-\n" +
"\x05steps\x18\x01 \x03(\v2\x17.state.v1.StepExecutionR\x05steps\"\xf7\x02\n" +
"\x05steps\x18\x01 \x03(\v2\x17.state.v1.StepExecutionR\x05steps\"\x93\x03\n" +
"\vUsageRecord\x12\x16\n" +
"\x06source\x18\x02 \x01(\tR\x06source\x12\x17\n" +
"\atask_id\x18\x03 \x01(\x03R\x06taskId\x12\x18\n" +
@@ -946,7 +957,8 @@ const file_state_v1_execution_proto_rawDesc = "" +
" \x01(\x03R\vinputTokens\x12#\n" +
"\routput_tokens\x18\v \x01(\x03R\foutputTokens\x12#\n" +
"\rcached_tokens\x18\f \x01(\x03R\fcachedTokens\x12*\n" +
"\x02at\x18\r \x01(\v2\x1a.google.protobuf.TimestampR\x02atJ\x04\b\x01\x10\x02R\x06org_id\"C\n" +
"\x02at\x18\r \x01(\v2\x1a.google.protobuf.TimestampR\x02at\x12\x1a\n" +
"\bidentity\x18\x0e \x01(\tR\bidentityJ\x04\b\x01\x10\x02R\x06org_id\"C\n" +
"\x12RecordUsageRequest\x12-\n" +
"\x06record\x18\x01 \x01(\v2\x15.state.v1.UsageRecordR\x06record\"\x15\n" +
"\x13RecordUsageResponse2\x8b\x03\n" +
+242 -19
View File
@@ -606,6 +606,208 @@ func (x *TokensByDayResponse) GetTokens() []*DayTokens {
return nil
}
// UsageTotalRow is one cell of a model-usage report: the day, source,
// provider, model and identity the calls belong to, and the tokens they
// used. Cost is not carried: the reader prices the tokens against the model
// catalog, so a price change is a change of interpretation, not of history.
type UsageTotalRow struct {
state protoimpl.MessageState `protogen:"open.v1"`
Day string `protobuf:"bytes,1,opt,name=day,proto3" json:"day,omitempty"`
Source string `protobuf:"bytes,2,opt,name=source,proto3" json:"source,omitempty"`
Provider string `protobuf:"bytes,3,opt,name=provider,proto3" json:"provider,omitempty"`
Model string `protobuf:"bytes,4,opt,name=model,proto3" json:"model,omitempty"`
// identity is who the work was requested as, empty when unknown.
Identity string `protobuf:"bytes,5,opt,name=identity,proto3" json:"identity,omitempty"`
Calls int64 `protobuf:"varint,6,opt,name=calls,proto3" json:"calls,omitempty"`
InputTokens int64 `protobuf:"varint,7,opt,name=input_tokens,json=inputTokens,proto3" json:"input_tokens,omitempty"`
OutputTokens int64 `protobuf:"varint,8,opt,name=output_tokens,json=outputTokens,proto3" json:"output_tokens,omitempty"`
CachedTokens int64 `protobuf:"varint,9,opt,name=cached_tokens,json=cachedTokens,proto3" json:"cached_tokens,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *UsageTotalRow) Reset() {
*x = UsageTotalRow{}
mi := &file_state_v1_stats_proto_msgTypes[11]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *UsageTotalRow) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UsageTotalRow) ProtoMessage() {}
func (x *UsageTotalRow) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_stats_proto_msgTypes[11]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UsageTotalRow.ProtoReflect.Descriptor instead.
func (*UsageTotalRow) Descriptor() ([]byte, []int) {
return file_state_v1_stats_proto_rawDescGZIP(), []int{11}
}
func (x *UsageTotalRow) GetDay() string {
if x != nil {
return x.Day
}
return ""
}
func (x *UsageTotalRow) GetSource() string {
if x != nil {
return x.Source
}
return ""
}
func (x *UsageTotalRow) GetProvider() string {
if x != nil {
return x.Provider
}
return ""
}
func (x *UsageTotalRow) GetModel() string {
if x != nil {
return x.Model
}
return ""
}
func (x *UsageTotalRow) GetIdentity() string {
if x != nil {
return x.Identity
}
return ""
}
func (x *UsageTotalRow) GetCalls() int64 {
if x != nil {
return x.Calls
}
return 0
}
func (x *UsageTotalRow) GetInputTokens() int64 {
if x != nil {
return x.InputTokens
}
return 0
}
func (x *UsageTotalRow) GetOutputTokens() int64 {
if x != nil {
return x.OutputTokens
}
return 0
}
func (x *UsageTotalRow) GetCachedTokens() int64 {
if x != nil {
return x.CachedTokens
}
return 0
}
// UsageTotalsRequest names the range ending now, in days.
type UsageTotalsRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
Days int64 `protobuf:"varint,1,opt,name=days,proto3" json:"days,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *UsageTotalsRequest) Reset() {
*x = UsageTotalsRequest{}
mi := &file_state_v1_stats_proto_msgTypes[12]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *UsageTotalsRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UsageTotalsRequest) ProtoMessage() {}
func (x *UsageTotalsRequest) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_stats_proto_msgTypes[12]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UsageTotalsRequest.ProtoReflect.Descriptor instead.
func (*UsageTotalsRequest) Descriptor() ([]byte, []int) {
return file_state_v1_stats_proto_rawDescGZIP(), []int{12}
}
func (x *UsageTotalsRequest) GetDays() int64 {
if x != nil {
return x.Days
}
return 0
}
type UsageTotalsResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
Rows []*UsageTotalRow `protobuf:"bytes,1,rep,name=rows,proto3" json:"rows,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *UsageTotalsResponse) Reset() {
*x = UsageTotalsResponse{}
mi := &file_state_v1_stats_proto_msgTypes[13]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *UsageTotalsResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*UsageTotalsResponse) ProtoMessage() {}
func (x *UsageTotalsResponse) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_stats_proto_msgTypes[13]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use UsageTotalsResponse.ProtoReflect.Descriptor instead.
func (*UsageTotalsResponse) Descriptor() ([]byte, []int) {
return file_state_v1_stats_proto_rawDescGZIP(), []int{13}
}
func (x *UsageTotalsResponse) GetRows() []*UsageTotalRow {
if x != nil {
return x.Rows
}
return nil
}
var File_state_v1_stats_proto protoreflect.FileDescriptor
const file_state_v1_stats_proto_rawDesc = "" +
@@ -650,13 +852,28 @@ const file_state_v1_stats_proto_rawDesc = "" +
"\x12TokensByDayRequest\x12\x12\n" +
"\x04days\x18\x01 \x01(\x03R\x04days\"B\n" +
"\x13TokensByDayResponse\x12+\n" +
"\x06tokens\x18\x01 \x03(\v2\x13.state.v1.DayTokensR\x06tokens2\xc4\x02\n" +
"\x06tokens\x18\x01 \x03(\v2\x13.state.v1.DayTokensR\x06tokens\"\x8a\x02\n" +
"\rUsageTotalRow\x12\x10\n" +
"\x03day\x18\x01 \x01(\tR\x03day\x12\x16\n" +
"\x06source\x18\x02 \x01(\tR\x06source\x12\x1a\n" +
"\bprovider\x18\x03 \x01(\tR\bprovider\x12\x14\n" +
"\x05model\x18\x04 \x01(\tR\x05model\x12\x1a\n" +
"\bidentity\x18\x05 \x01(\tR\bidentity\x12\x14\n" +
"\x05calls\x18\x06 \x01(\x03R\x05calls\x12!\n" +
"\finput_tokens\x18\a \x01(\x03R\vinputTokens\x12#\n" +
"\routput_tokens\x18\b \x01(\x03R\foutputTokens\x12#\n" +
"\rcached_tokens\x18\t \x01(\x03R\fcachedTokens\"(\n" +
"\x12UsageTotalsRequest\x12\x12\n" +
"\x04days\x18\x01 \x01(\x03R\x04days\"B\n" +
"\x13UsageTotalsResponse\x12+\n" +
"\x04rows\x18\x01 \x03(\v2\x17.state.v1.UsageTotalRowR\x04rows2\x90\x03\n" +
"\fStatsService\x12M\n" +
"\fStatusCounts\x12\x1d.state.v1.StatusCountsRequest\x1a\x1e.state.v1.StatusCountsResponse\x12P\n" +
"\rWorkflowStats\x12\x1e.state.v1.WorkflowStatsRequest\x1a\x1f.state.v1.WorkflowStatsResponse\x12G\n" +
"\n" +
"StageStats\x12\x1b.state.v1.StageStatsRequest\x1a\x1c.state.v1.StageStatsResponse\x12J\n" +
"\vTokensByDay\x12\x1c.state.v1.TokensByDayRequest\x1a\x1d.state.v1.TokensByDayResponseB\xa4\x01\n" +
"\vTokensByDay\x12\x1c.state.v1.TokensByDayRequest\x1a\x1d.state.v1.TokensByDayResponse\x12J\n" +
"\vUsageTotals\x12\x1c.state.v1.UsageTotalsRequest\x1a\x1d.state.v1.UsageTotalsResponseB\xa4\x01\n" +
"\fcom.state.v1B\n" +
"StatsProtoP\x01ZGgithub.com/samcharles93/archie-core/internal/contracts/state/v1;statev1\xa2\x02\x03SXX\xaa\x02\bState.V1\xca\x02\bState\\V1\xe2\x02\x14State\\V1\\GPBMetadata\xea\x02\tState::V1b\x06proto3"
@@ -672,7 +889,7 @@ func file_state_v1_stats_proto_rawDescGZIP() []byte {
return file_state_v1_stats_proto_rawDescData
}
var file_state_v1_stats_proto_msgTypes = make([]protoimpl.MessageInfo, 12)
var file_state_v1_stats_proto_msgTypes = make([]protoimpl.MessageInfo, 15)
var file_state_v1_stats_proto_goTypes = []any{
(*WorkflowStat)(nil), // 0: state.v1.WorkflowStat
(*StageStat)(nil), // 1: state.v1.StageStat
@@ -685,26 +902,32 @@ var file_state_v1_stats_proto_goTypes = []any{
(*StageStatsResponse)(nil), // 8: state.v1.StageStatsResponse
(*TokensByDayRequest)(nil), // 9: state.v1.TokensByDayRequest
(*TokensByDayResponse)(nil), // 10: state.v1.TokensByDayResponse
nil, // 11: state.v1.StatusCountsResponse.CountsEntry
(*UsageTotalRow)(nil), // 11: state.v1.UsageTotalRow
(*UsageTotalsRequest)(nil), // 12: state.v1.UsageTotalsRequest
(*UsageTotalsResponse)(nil), // 13: state.v1.UsageTotalsResponse
nil, // 14: state.v1.StatusCountsResponse.CountsEntry
}
var file_state_v1_stats_proto_depIdxs = []int32{
11, // 0: state.v1.StatusCountsResponse.counts:type_name -> state.v1.StatusCountsResponse.CountsEntry
14, // 0: state.v1.StatusCountsResponse.counts:type_name -> state.v1.StatusCountsResponse.CountsEntry
0, // 1: state.v1.WorkflowStatsResponse.stats:type_name -> state.v1.WorkflowStat
1, // 2: state.v1.StageStatsResponse.stats:type_name -> state.v1.StageStat
2, // 3: state.v1.TokensByDayResponse.tokens:type_name -> state.v1.DayTokens
3, // 4: state.v1.StatsService.StatusCounts:input_type -> state.v1.StatusCountsRequest
5, // 5: state.v1.StatsService.WorkflowStats:input_type -> state.v1.WorkflowStatsRequest
7, // 6: state.v1.StatsService.StageStats:input_type -> state.v1.StageStatsRequest
9, // 7: state.v1.StatsService.TokensByDay:input_type -> state.v1.TokensByDayRequest
4, // 8: state.v1.StatsService.StatusCounts:output_type -> state.v1.StatusCountsResponse
6, // 9: state.v1.StatsService.WorkflowStats:output_type -> state.v1.WorkflowStatsResponse
8, // 10: state.v1.StatsService.StageStats:output_type -> state.v1.StageStatsResponse
10, // 11: state.v1.StatsService.TokensByDay:output_type -> state.v1.TokensByDayResponse
8, // [8:12] is the sub-list for method output_type
4, // [4:8] is the sub-list for method input_type
4, // [4:4] is the sub-list for extension type_name
4, // [4:4] is the sub-list for extension extendee
0, // [0:4] is the sub-list for field type_name
11, // 4: state.v1.UsageTotalsResponse.rows:type_name -> state.v1.UsageTotalRow
3, // 5: state.v1.StatsService.StatusCounts:input_type -> state.v1.StatusCountsRequest
5, // 6: state.v1.StatsService.WorkflowStats:input_type -> state.v1.WorkflowStatsRequest
7, // 7: state.v1.StatsService.StageStats:input_type -> state.v1.StageStatsRequest
9, // 8: state.v1.StatsService.TokensByDay:input_type -> state.v1.TokensByDayRequest
12, // 9: state.v1.StatsService.UsageTotals:input_type -> state.v1.UsageTotalsRequest
4, // 10: state.v1.StatsService.StatusCounts:output_type -> state.v1.StatusCountsResponse
6, // 11: state.v1.StatsService.WorkflowStats:output_type -> state.v1.WorkflowStatsResponse
8, // 12: state.v1.StatsService.StageStats:output_type -> state.v1.StageStatsResponse
10, // 13: state.v1.StatsService.TokensByDay:output_type -> state.v1.TokensByDayResponse
13, // 14: state.v1.StatsService.UsageTotals:output_type -> state.v1.UsageTotalsResponse
10, // [10:15] is the sub-list for method output_type
5, // [5:10] is the sub-list for method input_type
5, // [5:5] is the sub-list for extension type_name
5, // [5:5] is the sub-list for extension extendee
0, // [0:5] is the sub-list for field type_name
}
func init() { file_state_v1_stats_proto_init() }
@@ -718,7 +941,7 @@ func file_state_v1_stats_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_state_v1_stats_proto_rawDesc), len(file_state_v1_stats_proto_rawDesc)),
NumEnums: 0,
NumMessages: 12,
NumMessages: 15,
NumExtensions: 0,
NumServices: 1,
},
@@ -23,6 +23,7 @@ const (
StatsService_WorkflowStats_FullMethodName = "/state.v1.StatsService/WorkflowStats"
StatsService_StageStats_FullMethodName = "/state.v1.StatsService/StageStats"
StatsService_TokensByDay_FullMethodName = "/state.v1.StatsService/TokensByDay"
StatsService_UsageTotals_FullMethodName = "/state.v1.StatsService/UsageTotals"
)
// StatsServiceClient is the client API for StatsService service.
@@ -33,6 +34,7 @@ type StatsServiceClient interface {
WorkflowStats(ctx context.Context, in *WorkflowStatsRequest, opts ...grpc.CallOption) (*WorkflowStatsResponse, error)
StageStats(ctx context.Context, in *StageStatsRequest, opts ...grpc.CallOption) (*StageStatsResponse, error)
TokensByDay(ctx context.Context, in *TokensByDayRequest, opts ...grpc.CallOption) (*TokensByDayResponse, error)
UsageTotals(ctx context.Context, in *UsageTotalsRequest, opts ...grpc.CallOption) (*UsageTotalsResponse, error)
}
type statsServiceClient struct {
@@ -83,6 +85,16 @@ func (c *statsServiceClient) TokensByDay(ctx context.Context, in *TokensByDayReq
return out, nil
}
func (c *statsServiceClient) UsageTotals(ctx context.Context, in *UsageTotalsRequest, opts ...grpc.CallOption) (*UsageTotalsResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(UsageTotalsResponse)
err := c.cc.Invoke(ctx, StatsService_UsageTotals_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// StatsServiceServer is the server API for StatsService service.
// All implementations must embed UnimplementedStatsServiceServer
// for forward compatibility.
@@ -91,6 +103,7 @@ type StatsServiceServer interface {
WorkflowStats(context.Context, *WorkflowStatsRequest) (*WorkflowStatsResponse, error)
StageStats(context.Context, *StageStatsRequest) (*StageStatsResponse, error)
TokensByDay(context.Context, *TokensByDayRequest) (*TokensByDayResponse, error)
UsageTotals(context.Context, *UsageTotalsRequest) (*UsageTotalsResponse, error)
mustEmbedUnimplementedStatsServiceServer()
}
@@ -113,6 +126,9 @@ func (UnimplementedStatsServiceServer) StageStats(context.Context, *StageStatsRe
func (UnimplementedStatsServiceServer) TokensByDay(context.Context, *TokensByDayRequest) (*TokensByDayResponse, error) {
return nil, status.Error(codes.Unimplemented, "method TokensByDay not implemented")
}
func (UnimplementedStatsServiceServer) UsageTotals(context.Context, *UsageTotalsRequest) (*UsageTotalsResponse, error) {
return nil, status.Error(codes.Unimplemented, "method UsageTotals not implemented")
}
func (UnimplementedStatsServiceServer) mustEmbedUnimplementedStatsServiceServer() {}
func (UnimplementedStatsServiceServer) testEmbeddedByValue() {}
@@ -206,6 +222,24 @@ func _StatsService_TokensByDay_Handler(srv interface{}, ctx context.Context, dec
return interceptor(ctx, in, info, handler)
}
func _StatsService_UsageTotals_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UsageTotalsRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(StatsServiceServer).UsageTotals(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: StatsService_UsageTotals_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(StatsServiceServer).UsageTotals(ctx, req.(*UsageTotalsRequest))
}
return interceptor(ctx, in, info, handler)
}
// StatsService_ServiceDesc is the grpc.ServiceDesc for StatsService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
@@ -229,6 +263,10 @@ var StatsService_ServiceDesc = grpc.ServiceDesc{
MethodName: "TokensByDay",
Handler: _StatsService_TokensByDay_Handler,
},
{
MethodName: "UsageTotals",
Handler: _StatsService_UsageTotals_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "state/v1/stats.proto",
+5 -1
View File
@@ -62,6 +62,10 @@ const (
// ActionRead because the feed spans settings, access, identity and runtime
// trails: a viewer's broad read must not open the whole instance's history.
ActionReadAudit Action = "read_audit"
// ActionReadUsage reads an org's model-usage report. It is separate from
// ActionRead for the same reason as the audit feed: the report names who
// spent what, which a viewer's broad read must not open.
ActionReadUsage Action = "read_usage"
)
// Actions is every action, in vocabulary order. The engine and the shipped
@@ -71,7 +75,7 @@ func Actions() []Action {
ActionRead, ActionCreate, ActionUpdate, ActionDelete, ActionApprove,
ActionRun, ActionDeliver, ActionReadLogs, ActionReadSecret,
ActionManageMembers, ActionManageIdentities, ActionManagePolicies,
ActionReadAudit,
ActionReadAudit, ActionReadUsage,
}
}
+11
View File
@@ -5,6 +5,7 @@ import (
"io"
"time"
"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/logging"
@@ -134,6 +135,16 @@ type TaskEvents interface {
Close() error
}
// UsageStore reads an org's recorded model usage. It is separate from
// TaskEvents so a reader that only reports usage does not acquire the task
// lifecycle surface.
type UsageStore interface {
// UsageTotals sums the caller org's model usage over the last days days,
// one row per day, source, model and identity. The org comes from the
// caller, so no call reads another org's usage.
UsageTotals(ctx context.Context, days int) ([]usage.TotalRow, error)
}
// TaskPage selects one page of the task list. Empty Statuses matches every
// status. A zero After starts at the newest task; otherwise the page begins
// after the task it names, in (updated_at, id) descending order.
+51 -9
View File
@@ -19,15 +19,19 @@ const (
// 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
Org string
Source Source
TaskID int64
Attempt int
Workflow string
Step string
Alias string
Provider string
Model string
// Identity is who the work was requested as: the identity that created
// the task, or the calling principal for chat and curator calls. Empty
// when the caller knew none.
Identity string
InputTokens int64
OutputTokens int64
CachedTokens int64
@@ -39,6 +43,44 @@ type Recorder interface {
RecordUsage(ctx context.Context, r Record) error
}
// MaxReportDays bounds a usage report's range.
const MaxReportDays = 365
// NormalizeDays clamps a requested report range to a usable number of days.
func NormalizeDays(days int) int {
if days < 1 {
return 1
}
if days > MaxReportDays {
return MaxReportDays
}
return days
}
// ReportWindow is the UTC window a report of days days covers, ending at now:
// whole days, today included. Rows are bucketed by UTC day, so the window
// starts at a UTC midnight.
func ReportWindow(days int, now time.Time) (from, to time.Time) {
now = now.UTC()
start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
return start.AddDate(0, 0, -(NormalizeDays(days) - 1)), now
}
// TotalRow is one cell of a usage report: the day, source and model an org's
// calls used and the tokens they spent. Cost is not carried; the reader
// prices the tokens against the model catalog.
type TotalRow struct {
Day string
Source Source
Provider string
Model string
Identity string
Calls int64
InputTokens int64
OutputTokens int64
CachedTokens int64
}
// WithRef fills Provider and Model from a "provider/model" ref.
func (r Record) WithRef(ref string) Record {
r.Provider, r.Model, _ = strings.Cut(ref, "/")
+2 -1
View File
@@ -821,7 +821,8 @@ func (tc *TaskContext) recordUsage(ctx context.Context, step, alias, modelRef st
}
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),
Identity: tc.Task.Identity,
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 {
@@ -48,6 +48,16 @@ type Model struct {
Attachment bool
Structured bool
InputModalities []string
Cost Cost
}
// Cost is a model's list price in USD per million tokens, as the catalog
// publishes it. A zero field means the catalog published no price.
type Cost struct {
Input float64 `json:"input,omitempty"`
Output float64 `json:"output,omitempty"`
CacheRead float64 `json:"cache_read,omitempty"`
CacheWrite float64 `json:"cache_write,omitempty"`
}
type catalogProvider struct {
@@ -223,6 +233,10 @@ func toolModels(source map[string]catalog.Model) []Model {
ContextWindow: model.ContextWindow(), MaxOutputTokens: model.MaxOutputTokens(),
Reasoning: model.Reasoning, Attachment: model.Attachment, Structured: model.Structured,
InputModalities: slices.Clone(model.Modalities.Input),
Cost: Cost{
Input: model.Cost.Input, Output: model.Cost.Output,
CacheRead: model.Cost.CacheRead, CacheWrite: model.Cost.CacheWrite,
},
})
}
slices.SortFunc(models, func(a, b Model) int { return strings.Compare(a.ID, b.ID) })
@@ -0,0 +1,16 @@
-- Attribute every model call to the identity the work was requested as, so
-- usage reports can break down by the people and agents that asked for it.
-- A task's calls belong to the identity that created the task; chat and
-- curator calls carry the calling principal in the record itself, so a queued
-- retry keeps its attribution.
-- +goose Up
ALTER TABLE model_usage ADD COLUMN identity_id text NOT NULL DEFAULT '';
UPDATE model_usage u SET identity_id = t.identity
FROM tasks t
WHERE u.task_id = t.id AND u.task_id <> 0 AND u.identity_id = '';
-- +goose Down
ALTER TABLE model_usage DROP COLUMN identity_id;
@@ -359,6 +359,7 @@ type ModelUsage struct {
OutputTokens int64
CachedTokens int64
At time.Time
IdentityID string
}
type Org struct {
@@ -13,11 +13,13 @@ import (
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
identity_id, 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
$8, $9,
COALESCE((SELECT t.identity FROM tasks t WHERE t.id = $1 AND $1 <> 0), $10::text),
$11, $12, $13, $14
)
`
@@ -31,13 +33,15 @@ type InsertModelUsageParams struct {
Alias string
Provider string
Model string
IdentityID string
InputTokens int64
OutputTokens int64
CachedTokens int64
At time.Time
}
// A task's usage belongs to the task's org, whatever the caller says.
// A task's usage belongs to the task's org and identity, whatever the caller
// says; every other call is billed to the caller's org and identity.
func (q *Queries) InsertModelUsage(ctx context.Context, arg InsertModelUsageParams) error {
_, err := q.db.Exec(ctx, insertModelUsage,
arg.TaskID,
@@ -49,6 +53,7 @@ func (q *Queries) InsertModelUsage(ctx context.Context, arg InsertModelUsagePara
arg.Alias,
arg.Provider,
arg.Model,
arg.IdentityID,
arg.InputTokens,
arg.OutputTokens,
arg.CachedTokens,
@@ -56,3 +61,72 @@ func (q *Queries) InsertModelUsage(ctx context.Context, arg InsertModelUsagePara
)
return err
}
const listModelUsageTotals = `-- name: ListModelUsageTotals :many
SELECT
to_char(at AT TIME ZONE 'UTC', 'YYYY-MM-DD') AS day,
source,
provider,
model,
identity_id,
count(*)::bigint AS calls,
COALESCE(sum(input_tokens), 0)::bigint AS input_tokens,
COALESCE(sum(output_tokens), 0)::bigint AS output_tokens,
COALESCE(sum(cached_tokens), 0)::bigint AS cached_tokens
FROM model_usage
WHERE org_id = $1
AND at >= $2::timestamptz
AND at < $3::timestamptz
GROUP BY 1, 2, 3, 4, 5
ORDER BY 1
`
type ListModelUsageTotalsParams struct {
OrgID string
FromAt time.Time
ToAt time.Time
}
type ListModelUsageTotalsRow struct {
Day string
Source string
Provider string
Model string
IdentityID string
Calls int64
InputTokens int64
OutputTokens int64
CachedTokens int64
}
// One row per day, source, provider, model and identity the org used.
// cached_tokens is a subset of input_tokens; the reader prices them.
func (q *Queries) ListModelUsageTotals(ctx context.Context, arg ListModelUsageTotalsParams) ([]ListModelUsageTotalsRow, error) {
rows, err := q.db.Query(ctx, listModelUsageTotals, arg.OrgID, arg.FromAt, arg.ToAt)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListModelUsageTotalsRow
for rows.Next() {
var i ListModelUsageTotalsRow
if err := rows.Scan(
&i.Day,
&i.Source,
&i.Provider,
&i.Model,
&i.IdentityID,
&i.Calls,
&i.InputTokens,
&i.OutputTokens,
&i.CachedTokens,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
@@ -1,10 +1,33 @@
-- name: InsertModelUsage :exec
-- A task's usage belongs to the task's org, whatever the caller says.
-- A task's usage belongs to the task's org and identity, whatever the caller
-- says; every other call is billed to the caller's org and identity.
INSERT INTO model_usage (
org_id, source, task_id, attempt, workflow, step, alias, provider, model,
input_tokens, output_tokens, cached_tokens, at
identity_id, input_tokens, output_tokens, cached_tokens, at
) VALUES (
COALESCE((SELECT t.org_id FROM tasks t WHERE t.id = sqlc.arg(task_id) AND sqlc.arg(task_id) <> 0), sqlc.arg(org_id)::text),
sqlc.arg(source), sqlc.arg(task_id), sqlc.arg(attempt), sqlc.arg(workflow), sqlc.arg(step), sqlc.arg(alias),
sqlc.arg(provider), sqlc.arg(model), sqlc.arg(input_tokens), sqlc.arg(output_tokens), sqlc.arg(cached_tokens), sqlc.arg(at)
sqlc.arg(provider), sqlc.arg(model),
COALESCE((SELECT t.identity FROM tasks t WHERE t.id = sqlc.arg(task_id) AND sqlc.arg(task_id) <> 0), sqlc.arg(identity_id)::text),
sqlc.arg(input_tokens), sqlc.arg(output_tokens), sqlc.arg(cached_tokens), sqlc.arg(at)
);
-- name: ListModelUsageTotals :many
-- One row per day, source, provider, model and identity the org used.
-- cached_tokens is a subset of input_tokens; the reader prices them.
SELECT
to_char(at AT TIME ZONE 'UTC', 'YYYY-MM-DD') AS day,
source,
provider,
model,
identity_id,
count(*)::bigint AS calls,
COALESCE(sum(input_tokens), 0)::bigint AS input_tokens,
COALESCE(sum(output_tokens), 0)::bigint AS output_tokens,
COALESCE(sum(cached_tokens), 0)::bigint AS cached_tokens
FROM model_usage
WHERE org_id = sqlc.arg(org_id)
AND at >= sqlc.arg(from_at)::timestamptz
AND at < sqlc.arg(to_at)::timestamptz
GROUP BY 1, 2, 3, 4, 5
ORDER BY 1;
+25
View File
@@ -2,7 +2,9 @@ package postgres
import (
"context"
"time"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/usage"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
@@ -12,6 +14,29 @@ func (s *Store) RecordUsage(ctx context.Context, r usage.Record) error {
return postgresdb.New(s.pool).InsertModelUsage(ctx, postgresdb.InsertModelUsageParams{
TaskID: r.TaskID, OrgID: r.Org, Source: string(r.Source), Attempt: int64(r.Attempt),
Workflow: r.Workflow, Step: r.Step, Alias: r.Alias, Provider: r.Provider, Model: r.Model,
IdentityID: r.Identity,
InputTokens: r.InputTokens, OutputTokens: r.OutputTokens, CachedTokens: r.CachedTokens, At: r.At,
})
}
// UsageTotals sums the caller org's model usage over the last days days: one
// row per day, source, provider, model and identity. The org comes from the
// caller, never a parameter, so no call can read another org's usage.
func (s *Store) UsageTotals(ctx context.Context, days int) ([]usage.TotalRow, error) {
from, to := usage.ReportWindow(days, time.Now())
rows, err := postgresdb.New(s.pool).ListModelUsageTotals(ctx, postgresdb.ListModelUsageTotalsParams{
OrgID: string(org.OrgFromContext(ctx)), FromAt: from, ToAt: to,
})
if err != nil {
return nil, err
}
out := make([]usage.TotalRow, 0, len(rows))
for _, row := range rows {
out = append(out, usage.TotalRow{
Day: row.Day, Source: usage.Source(row.Source), Provider: row.Provider,
Model: row.Model, Identity: row.IdentityID, Calls: row.Calls,
InputTokens: row.InputTokens, OutputTokens: row.OutputTokens, CachedTokens: row.CachedTokens,
})
}
return out, nil
}
@@ -107,6 +107,9 @@ type Deps struct {
// Usage appends model usage records. Optional: nil answers RecordUsage
// with codes.Unavailable.
Usage usage.Recorder
// UsageTotals reads an org's recorded usage. Optional: nil answers
// UsageTotals with codes.Unavailable.
UsageTotals storecontract.UsageStore
// OrgModels answers an org's effective model aliases and own providers.
// Optional: nil answers TaskModels with codes.Unavailable.
OrgModels func(ctx context.Context, orgID string) (map[string]string, map[string]config.Provider, error)
+23
View File
@@ -3,8 +3,12 @@ package staterpc
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/usage"
)
type statsServer struct {
@@ -50,6 +54,17 @@ func (s *statsServer) TokensByDay(ctx context.Context, r *pb.TokensByDayRequest)
return &pb.TokensByDayResponse{Tokens: mapValues(tokens, dayTokensProto)}, nil
}
func (s *statsServer) UsageTotals(ctx context.Context, r *pb.UsageTotalsRequest) (*pb.UsageTotalsResponse, error) {
if s.deps.UsageTotals == nil {
return nil, status.Error(codes.Unavailable, "usage totals unavailable")
}
rows, err := s.deps.UsageTotals.UsageTotals(ctx, int(r.GetDays()))
if err != nil {
return nil, s.logErr("UsageTotals", err)
}
return &pb.UsageTotalsResponse{Rows: mapValues(rows, usageTotalProto)}, nil
}
// Capture
func (c *Client) StatusCounts(ctx context.Context) (map[string]int, error) {
@@ -90,4 +105,12 @@ func (c *Client) TokensByDay(ctx context.Context, days int) ([]storecontract.Day
return mapValues(r.Tokens, dayTokensValue), nil
}
func (c *Client) UsageTotals(ctx context.Context, days int) ([]usage.TotalRow, error) {
r, err := c.stats.UsageTotals(ctx, &pb.UsageTotalsRequest{Days: int64(days)})
if err != nil {
return nil, unmapError(err)
}
return mapValues(r.Rows, usageTotalValue), nil
}
// Capture
@@ -11,6 +11,7 @@ func usageProto(r usage.Record) *pb.UsageRecord {
return &pb.UsageRecord{
Source: string(r.Source), TaskId: r.TaskID, Attempt: int64(r.Attempt),
Workflow: r.Workflow, Step: r.Step, Alias: r.Alias, Provider: r.Provider, Model: r.Model,
Identity: r.Identity,
InputTokens: r.InputTokens, OutputTokens: r.OutputTokens, CachedTokens: r.CachedTokens,
At: timestamppb.New(r.At),
}
@@ -20,7 +21,27 @@ func usageValue(r *pb.UsageRecord) usage.Record {
return usage.Record{
Source: usage.Source(r.GetSource()), TaskID: r.GetTaskId(), Attempt: int(r.GetAttempt()),
Workflow: r.GetWorkflow(), Step: r.GetStep(), Alias: r.GetAlias(), Provider: r.GetProvider(), Model: r.GetModel(),
Identity: r.GetIdentity(),
InputTokens: r.GetInputTokens(), OutputTokens: r.GetOutputTokens(), CachedTokens: r.GetCachedTokens(),
At: r.GetAt().AsTime(),
}
}
func usageTotalProto(t usage.TotalRow) *pb.UsageTotalRow {
return &pb.UsageTotalRow{
Day: t.Day, Source: string(t.Source), Provider: t.Provider, Model: t.Model,
Identity: t.Identity, Calls: t.Calls, InputTokens: t.InputTokens,
OutputTokens: t.OutputTokens, CachedTokens: t.CachedTokens,
}
}
func usageTotalValue(r *pb.UsageTotalRow) usage.TotalRow {
if r == nil {
return usage.TotalRow{}
}
return usage.TotalRow{
Day: r.GetDay(), Source: usage.Source(r.GetSource()), Provider: r.GetProvider(), Model: r.GetModel(),
Identity: r.GetIdentity(), Calls: r.GetCalls(), InputTokens: r.GetInputTokens(),
OutputTokens: r.GetOutputTokens(), CachedTokens: r.GetCachedTokens(),
}
}
@@ -72,6 +72,7 @@ func UsageRecorder(store usage.Recorder, t *workflow.Task, log *slog.Logger) fun
}
record := usage.Record{
Source: usage.SourceTask, TaskID: t.ID, Attempt: t.Attempt, Workflow: t.Workflow, Step: "mcp-sampling",
Identity: t.Identity,
InputTokens: int64(used.PromptTokens), OutputTokens: int64(used.CompletionTokens),
CachedTokens: int64(used.CachedTokens), At: time.Now().UTC(),
}.WithRef(model)
+1
View File
@@ -47,6 +47,7 @@ func pageProbes() []pageProbe {
{"org_policies", http.MethodPut, "/api/access/policies"},
{"org_denials", http.MethodGet, "/api/access/denials"},
{"org_audit", http.MethodGet, "/api/audit"},
{"org_usage", http.MethodGet, "/api/usage"},
{"org_config", http.MethodPost, "/api/control-plane/resources/_/commands/replace"},
}
}
+12
View File
@@ -213,6 +213,18 @@ type CatalogProviderView struct {
APIKeyEnv string `json:"api_key_env,omitempty"`
BaseURL string `json:"base_url,omitempty"`
Models []string `json:"models"`
// Costs is each listed model's list price, keyed by model id. A model the
// catalog did not price is absent, so a report shows tokens rather than a
// guessed cost.
Costs map[string]ModelCostView `json:"costs,omitempty"`
}
// ModelCostView is one model's catalog list price in USD per million tokens.
type ModelCostView struct {
Input float64 `json:"input,omitempty"`
Output float64 `json:"output,omitempty"`
CacheRead float64 `json:"cache_read,omitempty"`
CacheWrite float64 `json:"cache_write,omitempty"`
}
// BuildConfigView renders the secret-free configuration view from an explicit
@@ -88,6 +88,7 @@ func TestHTTPPolicyCannotBeBypassed(t *testing.T) {
{"GET", "/api/tasks/7/sandbox"},
{"GET", "/api/tasks/7/transcript"},
{"GET", "/api/tokens"},
{"GET", "/api/usage"},
{"GET", "/api/version"},
{"GET", "/api/workflows"},
{"PATCH", "/api/bindings/7"},
+4
View File
@@ -273,6 +273,10 @@ func accessRequest(r *http.Request) (access.Action, access.ResourceKind, string)
// export route below the same prefix carries the same grant.
return access.ActionReadAudit, access.KindAudit, ""
}
if matchPrefix(path, "/api/usage") {
// Who spent what is its own grant, not a viewer's broad read.
return access.ActionReadUsage, access.KindDashboard, ""
}
if a, kind, id, ok := routeOverride(r, action, path); ok {
return a, kind, id
}
+4
View File
@@ -37,6 +37,9 @@ type Server struct {
accountRateLimit *ratelimit.Limiter
Store storecontract.TaskStore
// Usage reads the caller org's model-usage report. Nil answers
// GET /api/usage with 503.
Usage storecontract.UsageStore
Log *slog.Logger
// Steps lists an execution's recorded steps. Nil reports none.
@@ -274,6 +277,7 @@ func (s *Server) Broadcast(e events.Event) {
func (s *Server) registerCoreRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/summary", s.handleSummary)
mux.HandleFunc("GET /api/usage", s.handleUsage)
mux.HandleFunc("GET /api/setup", s.handleSetup)
newWorkflowsAPI(s).register(mux)
mux.HandleFunc("POST /api/control-plane/resources/repository-policies/check", s.handleRepositoryCheck)
+246
View File
@@ -0,0 +1,246 @@
package webui
import (
"context"
"math"
"net/http"
"sort"
"strconv"
"github.com/samcharles93/archie-core/internal/domain/usage"
)
// usageBucketView is a token and cost total for one slice of a usage report.
// CostUSD is nil when nothing in the slice could be priced: the report shows
// tokens rather than a guessed cost.
type usageBucketView struct {
Calls int64 `json:"calls"`
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
CachedTokens int64 `json:"cached_tokens"`
CostUSD *float64 `json:"cost_usd"`
UnpricedCalls int64 `json:"unpriced_calls"`
}
// usageDayView is one UTC day's totals.
type usageDayView struct {
Day string `json:"day"`
usageBucketView
}
// usageModelView is one provider/model's totals.
type usageModelView struct {
Provider string `json:"provider"`
Model string `json:"model"`
usageBucketView
}
// usageSourceView is one call source's totals.
type usageSourceView struct {
Source string `json:"source"`
usageBucketView
}
// usageIdentityView is one identity's totals. IdentityName is "Unknown" when
// the record carried no identity.
type usageIdentityView struct {
IdentityID string `json:"identity_id"`
IdentityName string `json:"identity_name"`
usageBucketView
}
// usageReportView is GET /api/usage's payload: the range, its totals, and the
// same totals sliced every way the page shows them.
type usageReportView struct {
Days int `json:"days"`
Totals usageBucketView `json:"totals"`
ByDay []usageDayView `json:"by_day"`
ByModel []usageModelView `json:"by_model"`
BySource []usageSourceView `json:"by_source"`
ByIdentity []usageIdentityView `json:"by_identity"`
}
// handleUsage reports the caller org's model usage. The org is never a
// parameter: the State Store scopes the read to the calling principal's org,
// so cross-org reporting does not exist rather than being refused.
func (s *Server) handleUsage(w http.ResponseWriter, r *http.Request) {
if s.Usage == nil {
http.Error(w, "usage unavailable", http.StatusServiceUnavailable)
return
}
days := 30
if raw := r.URL.Query().Get("days"); raw != "" {
if n, err := strconv.Atoi(raw); err == nil {
days = n
}
}
days = usage.NormalizeDays(days)
rows, err := s.Usage.UsageTotals(r.Context(), days)
if err != nil {
s.logf("usage totals unavailable", "err", err)
http.Error(w, "usage unavailable", http.StatusServiceUnavailable)
return
}
writeJSON(w, usageReport(days, rows, s.modelPrices(r.Context()), s.identityNames(r.Context())))
}
// modelPrices prices each model from the catalog view the configuration owner
// publishes. A model it does not price is absent, so its calls are counted as
// unpriced instead of costed at zero.
func (s *Server) modelPrices(ctx context.Context) map[string]ModelCostView {
prices := map[string]ModelCostView{}
view, found, err := s.configSource()(ctx)
if err != nil || !found {
return prices
}
for _, provider := range view.Catalog {
for model, cost := range provider.Costs {
prices[usagePriceKey(provider.ID, model)] = cost
}
}
return prices
}
// identityNames resolves the caller's identities for display. A read that
// fails leaves the report with ids: naming is presentation, not the data.
func (s *Server) identityNames(ctx context.Context) map[string]string {
names := map[string]string{}
if s.Identities == nil {
return names
}
identities, err := s.Identities.List(ctx)
if err != nil {
s.logf("usage identity names unavailable", "err", err)
return names
}
for _, identity := range identities {
names[string(identity.ID)] = identity.DisplayName
}
return names
}
// usagePriceKey identifies one priced model. Provider and model are joined by
// a byte no model id contains, so ("a/b", "c") and ("a", "b/c") stay distinct.
func usagePriceKey(provider, model string) string {
return provider + "\x00" + model
}
// add accumulates one usage row, pricing it when the catalog published a
// price for its model.
func (b *usageBucketView) add(row usage.TotalRow, prices map[string]ModelCostView) {
b.Calls += row.Calls
b.InputTokens += row.InputTokens
b.OutputTokens += row.OutputTokens
b.CachedTokens += row.CachedTokens
cost, priced := prices[usagePriceKey(row.Provider, row.Model)]
if !priced {
b.UnpricedCalls += row.Calls
return
}
total := 0.0
if b.CostUSD != nil {
total = *b.CostUSD
}
costUSD := roundUSD(total + modelCallCost(row, cost))
b.CostUSD = &costUSD
}
// modelCallCost prices one row's tokens at the model's list price, netting
// cache hits: cached tokens are part of the input count and are charged the
// cache-read rate. A catalog that publishes no cache-read price is charged at
// the input rate, because reporting less than the provider bills is worse
// than reporting more.
func modelCallCost(row usage.TotalRow, cost ModelCostView) float64 {
cachedRate := cost.CacheRead
if cachedRate == 0 {
cachedRate = cost.Input
}
uncached := max(row.InputTokens-row.CachedTokens, 0)
return (float64(uncached)*cost.Input +
float64(row.CachedTokens)*cachedRate +
float64(row.OutputTokens)*cost.Output) / 1e6
}
// roundUSD keeps a cent-level figure readable instead of exposing float noise
// to the page.
func roundUSD(amount float64) float64 {
return math.Round(amount*1e6) / 1e6
}
// usageReport slices rows by day, model, source and identity. Every slice is
// also summed into the report's totals.
func usageReport(days int, rows []usage.TotalRow, prices map[string]ModelCostView, names map[string]string) usageReportView {
report := usageReportView{
Days: days,
ByDay: []usageDayView{},
ByModel: []usageModelView{},
BySource: []usageSourceView{},
ByIdentity: []usageIdentityView{},
}
byDay := map[string]*usageBucketView{}
byModel := map[[2]string]*usageBucketView{}
bySource := map[string]*usageBucketView{}
byIdentity := map[string]*usageBucketView{}
for _, row := range rows {
report.Totals.add(row, prices)
usageBucketFor(byDay, row.Day).add(row, prices)
usageBucketFor(byModel, [2]string{row.Provider, row.Model}).add(row, prices)
usageBucketFor(bySource, string(row.Source)).add(row, prices)
usageBucketFor(byIdentity, row.Identity).add(row, prices)
}
for day, bucket := range byDay {
report.ByDay = append(report.ByDay, usageDayView{Day: day, usageBucketView: *bucket})
}
for key, bucket := range byModel {
report.ByModel = append(report.ByModel, usageModelView{Provider: key[0], Model: key[1], usageBucketView: *bucket})
}
for source, bucket := range bySource {
report.BySource = append(report.BySource, usageSourceView{Source: source, usageBucketView: *bucket})
}
for id, bucket := range byIdentity {
name := names[id]
switch {
case id == "":
name = "Unknown"
case name == "":
name = id
}
report.ByIdentity = append(report.ByIdentity, usageIdentityView{IdentityID: id, IdentityName: name, usageBucketView: *bucket})
}
sort.Slice(report.ByDay, func(i, j int) bool { return report.ByDay[i].Day < report.ByDay[j].Day })
sort.Slice(report.ByModel, func(i, j int) bool {
return usageViewLess(report.ByModel[i].tokens(), report.ByModel[j].tokens(), report.ByModel[i].Provider+"/"+report.ByModel[i].Model, report.ByModel[j].Provider+"/"+report.ByModel[j].Model)
})
sort.Slice(report.BySource, func(i, j int) bool {
return usageViewLess(report.BySource[i].tokens(), report.BySource[j].tokens(), report.BySource[i].Source, report.BySource[j].Source)
})
sort.Slice(report.ByIdentity, func(i, j int) bool {
return usageViewLess(report.ByIdentity[i].tokens(), report.ByIdentity[j].tokens(), report.ByIdentity[i].IdentityID, report.ByIdentity[j].IdentityID)
})
return report
}
// usageViewLess orders the largest spenders first, then by a stable key so a
// report is stable between reads of the same data.
func usageViewLess(tokensI, tokensJ int64, keyI, keyJ string) bool {
if tokensI != tokensJ {
return tokensI > tokensJ
}
return keyI < keyJ
}
// tokens is the figure the page and the ordering use: a cached token is part
// of the input count, so adding it again would double-count.
func (b *usageBucketView) tokens() int64 {
return b.InputTokens + b.OutputTokens
}
// usageBucketFor returns the bucket for key, creating it on first use.
func usageBucketFor[K comparable](buckets map[K]*usageBucketView, key K) *usageBucketView {
bucket, ok := buckets[key]
if !ok {
bucket = &usageBucketView{}
buckets[key] = bucket
}
return bucket
}
+69
View File
@@ -0,0 +1,69 @@
package webui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/usage"
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
)
// usageTotalsStore answers the usage read with no rows: these cases test the
// access decision, not the report.
type usageTotalsStore struct{}
func (usageTotalsStore) UsageTotals(context.Context, int) ([]usage.TotalRow, error) {
return nil, nil
}
// TestUsageNeedsAdminOrOwner pins the grant: usage names who spent what, so it
// carries its own action like the audit feed. A viewer's or developer's broad
// read must not open it.
func TestUsageNeedsAdminOrOwner(t *testing.T) {
tests := []struct {
role org.Role
want int
}{
{org.RoleViewer, http.StatusForbidden},
{org.RoleDeveloper, http.StatusForbidden},
{org.RoleAdmin, http.StatusOK},
{org.RoleOwner, http.StatusOK},
}
for _, tt := range tests {
t.Run(string(tt.role), func(t *testing.T) {
principal := access.Principal{
IdentityID: "alice", Org: "acme",
Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tt.role}},
}
srv := &Server{
Usage: usageTotalsStore{},
Accounts: accountBoundaryStore{},
Access: infraaccess.New(access.ShippedOrgPolicies("acme")),
Principals: principalOf(principal),
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example/api/usage", nil)
req.Header.Set("X-Archie-CSRF", "1")
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
rec := httptest.NewRecorder()
srv.Handler().ServeHTTP(rec, req)
if rec.Code != tt.want {
t.Fatalf("status = %d, want %d body=%s", rec.Code, tt.want, rec.Body.String())
}
if tt.want != http.StatusOK {
return
}
var report usageReportView
if err := json.Unmarshal(rec.Body.Bytes(), &report); err != nil {
t.Fatalf("decode report: %v", err)
}
if report.Days != 30 || report.ByDay == nil || report.ByIdentity == nil {
t.Fatalf("report = %+v, want days 30 with empty slices", report)
}
})
}
}
+4
View File
@@ -123,6 +123,10 @@ message UsageRecord {
int64 output_tokens = 11;
int64 cached_tokens = 12;
google.protobuf.Timestamp at = 13;
// identity is who the work was requested as: the task's creating identity
// for task calls, the calling principal for chat and curator calls. It
// travels in the record so a queued retry keeps its attribution.
string identity = 14;
}
message RecordUsageRequest { UsageRecord record = 1; }
message RecordUsageResponse {}
+21
View File
@@ -7,6 +7,7 @@ service StatsService {
rpc WorkflowStats(WorkflowStatsRequest) returns (WorkflowStatsResponse);
rpc StageStats(StageStatsRequest) returns (StageStatsResponse);
rpc TokensByDay(TokensByDayRequest) returns (TokensByDayResponse);
rpc UsageTotals(UsageTotalsRequest) returns (UsageTotalsResponse);
}
// WorkflowStat mirrors storecontract.WorkflowStat.
@@ -46,3 +47,23 @@ message StageStatsRequest {}
message StageStatsResponse { repeated StageStat stats = 1; }
message TokensByDayRequest { int64 days = 1; }
message TokensByDayResponse { repeated DayTokens tokens = 1; }
// UsageTotalRow is one cell of a model-usage report: the day, source,
// provider, model and identity the calls belong to, and the tokens they
// used. Cost is not carried: the reader prices the tokens against the model
// catalog, so a price change is a change of interpretation, not of history.
message UsageTotalRow {
string day = 1;
string source = 2;
string provider = 3;
string model = 4;
// identity is who the work was requested as, empty when unknown.
string identity = 5;
int64 calls = 6;
int64 input_tokens = 7;
int64 output_tokens = 8;
int64 cached_tokens = 9;
}
// UsageTotalsRequest names the range ending now, in days.
message UsageTotalsRequest { int64 days = 1; }
message UsageTotalsResponse { repeated UsageTotalRow rows = 1; }
+3
View File
@@ -255,6 +255,9 @@ export const api = {
identityCommand: <T = unknown>(id: string, command: string, body: { expected_version: number; display_name: string }) =>
request<T>(`/api/identities/${encodeURIComponent(id)}/${encodeURIComponent(command)}`, { method: "POST", body }),
summary: <T = unknown>() => request<T>("/api/summary"),
// usage reads the org's model usage for the last days. The server derives
// the org from the caller and clamps days to 1..365.
usage: <T = unknown>(days = 30) => request<T>("/api/usage" + qs({ days })),
tasks: <T = unknown>() => request<T>("/api/tasks"),
// taskPage reads one page of the board; next is the cursor for the page
// after it, or null when this page was the last.
+13
View File
@@ -34,6 +34,19 @@ export function compact(n: number | null | undefined): string {
}).format(n);
}
/** A cost in US dollars: "$1.23", and "$0.0023" for a sub-cent amount. A null
* cost means work that could not be priced, not free work, so it reads "—"
* rather than "$0". */
export function usd(value: number | null | undefined): string {
if (value == null || !Number.isFinite(value)) return "—";
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
maximumFractionDigits: 4,
}).format(value);
}
/** An exact date and time in zone, or in the browser's zone when zone is empty. */
export function exactTime(value: Date, zone: string): string {
return value.toLocaleString(undefined, zone ? { timeZone: zone, timeZoneName: "short" } : undefined);
+4
View File
@@ -43,6 +43,10 @@ const orgSpecs: NavSpec[] = [
label: "Automation",
paths: ["/org/schedules", "/org/scheduling-policy", "/org/channels", "/org/playbooks", "/org/alerts"],
},
{
label: "Usage",
paths: ["/org/usage"],
},
],
},
];
+1 -1
View File
@@ -24,7 +24,7 @@ const error = ref("");
// Policies and models act on the caller's own org server-side, so an instance
// admin looking at another org is sent back rather than editing the wrong one.
const OWN_ORG_ONLY = ["/org/audit", "/org/policies", "/org/models", "/org/connections", "/org/repositories"];
const OWN_ORG_ONLY = ["/org/audit", "/org/policies", "/org/models", "/org/connections", "/org/repositories", "/org/usage"];
const away = computed(() => !!org.value && org.value.id !== homeOrg.value);
const route = useRoute();
const router = useRouter();
+333
View File
@@ -0,0 +1,333 @@
<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import PageHeader from "@/base/PageHeader.vue";
import { DataTable, type DataTableColumn } from "@/components/ui/data-table";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { MetadataList } from "@/components/ui/metadata-list";
import { ReadError } from "@/components/ui/read-error";
import { Section } from "@/components/ui/section";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Spinner } from "@/components/ui/spinner";
import { api } from "@/lib/api";
import { compact, usd } from "@/lib/format";
/**
* What the org's model calls used and cost over a range: the totals, a row per
* day, and the model, source and identity breakdowns that explain them. The
* server derives the org from the caller, so the page names no org.
*/
interface Bucket {
calls: number;
input_tokens: number;
output_tokens: number;
cached_tokens: number;
cost_usd: number | null;
unpriced_calls: number;
}
type DayRow = { day: string } & Bucket;
type ModelRow = { provider: string; model: string } & Bucket;
type SourceRow = { source: string } & Bucket;
type IdentityRow = { identity_id: string; identity_name: string } & Bucket;
interface UsageReport {
days: number;
totals: Bucket;
by_day: DayRow[];
by_model: ModelRow[];
by_source: SourceRow[];
by_identity: IdentityRow[];
}
const RANGES = [
{ value: "7", label: "Last 7 days" },
{ value: "30", label: "Last 30 days" },
{ value: "90", label: "Last 90 days" },
];
const range = ref("30");
const report = ref<UsageReport | null>(null);
const loading = ref(true);
const error = ref("");
// Identifies the current read, so a reply for a range the operator has left is
// dropped instead of painted over the newer one.
let revision = 0;
async function load(): Promise<void> {
const attempt = ++revision;
loading.value = true;
error.value = "";
// The numbers below the control are the numbers for its range, so the
// previous range's report goes before the new read settles.
report.value = null;
try {
const next = await api.usage<UsageReport>(Number(range.value));
if (attempt !== revision) return;
report.value = next;
} catch (cause) {
if (attempt !== revision) return;
error.value = String((cause as Error).message || cause);
} finally {
if (attempt === revision) loading.value = false;
}
}
onMounted(() => void load());
watch(range, () => void load());
/** Prompt and completion tokens; cached tokens are a subset of the input. */
const tokens = (bucket: Bucket): number =>
bucket.input_tokens + bucket.output_tokens;
/** The exact counts behind a compact cell. */
function tokenTitle(bucket: Bucket): string {
const cached = bucket.cached_tokens
? `, ${bucket.cached_tokens.toLocaleString()} cached`
: "";
return `${tokens(bucket).toLocaleString()} tokens (${bucket.input_tokens.toLocaleString()} in, ${bucket.output_tokens.toLocaleString()} out${cached})`;
}
interface TotalRow {
label: string;
value?: string;
mono?: boolean;
title?: string;
}
const totalRows = computed<TotalRow[]>(() => {
const totals = report.value?.totals;
if (!totals) return [];
const rows: TotalRow[] = [
{ label: "Model calls", value: totals.calls.toLocaleString() },
{
label: "Total tokens",
value: compact(tokens(totals)),
mono: true,
title: `${tokens(totals).toLocaleString()} tokens`,
},
{
label: "Input tokens",
value: compact(totals.input_tokens),
mono: true,
title: `${totals.input_tokens.toLocaleString()} tokens`,
},
{
label: "Output tokens",
value: compact(totals.output_tokens),
mono: true,
title: `${totals.output_tokens.toLocaleString()} tokens`,
},
{
label: "Cached tokens",
value: compact(totals.cached_tokens),
mono: true,
title: `${totals.cached_tokens.toLocaleString()} cached tokens, included in input tokens`,
},
{
label: "Cost",
value: usd(totals.cost_usd),
mono: true,
title:
totals.cost_usd == null
? "No call in this range could be priced"
: undefined,
},
];
if (totals.unpriced_calls > 0) {
rows.push({
label: "Unpriced calls",
value: totals.unpriced_calls.toLocaleString(),
title:
"Calls to models without a catalog price count tokens but have no cost.",
});
}
return rows;
});
const dayColumns: DataTableColumn<DayRow>[] = [
{ id: "day", label: "Day", fixed: true, class: "whitespace-nowrap" },
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
];
const modelColumns: DataTableColumn<ModelRow>[] = [
{ id: "provider", label: "Provider", fixed: true, class: "text-fg-muted whitespace-nowrap" },
{ id: "model", label: "Model", fixed: true, class: "w-full whitespace-normal" },
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
];
const sourceColumns: DataTableColumn<SourceRow>[] = [
{ id: "source", label: "Source", fixed: true },
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
];
const identityColumns: DataTableColumn<IdentityRow>[] = [
{ id: "identity", label: "Identity", fixed: true, class: "w-full whitespace-normal" },
{ id: "calls", label: "Calls", class: "tabular-nums", sort: (row) => row.calls },
{ id: "tokens", label: "Tokens", class: "tabular-nums", sort: tokens },
{ id: "cost", label: "Cost", class: "tabular-nums", sort: (row) => row.cost_usd ?? -1 },
];
</script>
<template>
<div>
<PageHeader title="Usage">
<Select v-model="range">
<SelectTrigger class="w-40" size="sm" aria-label="Date range">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem v-for="option in RANGES" :key="option.value" :value="option.value">
{{ option.label }}
</SelectItem>
</SelectContent>
</Select>
</PageHeader>
<ReadError v-if="error" :message="error" retry-label="Retry" class="mb-4" @retry="load" />
<Section v-else-if="loading">
<p role="status" class="flex items-center gap-2 text-label text-fg-muted">
<Spinner />Loading usage…
</p>
</Section>
<Section v-else-if="!report || report.totals.calls === 0">
<Empty>
<EmptyHeader>
<EmptyTitle>No model calls in this range.</EmptyTitle>
</EmptyHeader>
</Empty>
</Section>
<template v-else>
<Section title="Totals">
<MetadataList :rows="totalRows">
<template #value="{ row }">
<span
class="tabular-nums text-title font-medium"
:class="row.mono && 'font-mono'"
:title="row.title"
>{{ row.value }}</span
>
</template>
</MetadataList>
</Section>
<Section title="Daily">
<DataTable
:columns="dayColumns"
:rows="report?.by_day ?? []"
:row-key="(row) => row.day"
storage-key="usage-daily"
>
<template #empty>
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
</template>
<template #cell-day="{ row }">
<span class="font-mono text-caption">{{ row.day }}</span>
</template>
<template #cell-calls="{ row }">
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
</template>
<template #cell-tokens="{ row }">
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
</template>
<template #cell-cost="{ row }">
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
</template>
</DataTable>
</Section>
<Section title="By model">
<DataTable
:columns="modelColumns"
:rows="report?.by_model ?? []"
:row-key="(row) => `${row.provider}/${row.model}`"
storage-key="usage-by-model"
>
<template #empty>
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
</template>
<template #cell-provider="{ row }">
<span class="font-mono text-caption">{{ row.provider || "–" }}</span>
</template>
<template #cell-model="{ row }">
<span class="font-mono text-caption">{{ row.model || "–" }}</span>
</template>
<template #cell-calls="{ row }">
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
</template>
<template #cell-tokens="{ row }">
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
</template>
<template #cell-cost="{ row }">
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
</template>
</DataTable>
</Section>
<Section title="By source">
<DataTable
:columns="sourceColumns"
:rows="report?.by_source ?? []"
:row-key="(row) => row.source"
storage-key="usage-by-source"
>
<template #empty>
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
</template>
<template #cell-source="{ row }">
<span class="font-mono text-caption">{{ row.source || "–" }}</span>
</template>
<template #cell-calls="{ row }">
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
</template>
<template #cell-tokens="{ row }">
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
</template>
<template #cell-cost="{ row }">
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
</template>
</DataTable>
</Section>
<Section title="By identity">
<DataTable
:columns="identityColumns"
:rows="report?.by_identity ?? []"
:row-key="(row) => row.identity_id || 'unknown'"
storage-key="usage-by-identity"
>
<template #empty>
<Empty><EmptyHeader><EmptyTitle>No model calls in this range.</EmptyTitle></EmptyHeader></Empty>
</template>
<template #cell-identity="{ row }">
<span :title="row.identity_id || undefined">{{ row.identity_name || "Unknown" }}</span>
</template>
<template #cell-calls="{ row }">
<span class="tabular-nums">{{ row.calls.toLocaleString() }}</span>
</template>
<template #cell-tokens="{ row }">
<span class="tabular-nums" :title="tokenTitle(row)">{{ compact(tokens(row)) }}</span>
</template>
<template #cell-cost="{ row }">
<span class="tabular-nums">{{ usd(row.cost_usd) }}</span>
</template>
</DataTable>
</Section>
</template>
</div>
</template>
+7
View File
@@ -14,6 +14,7 @@ const OrgAgentsPage = () => import("@/org/OrgAgentsPage.vue");
const OrgWorkspacesPage = () => import("@/org/OrgWorkspacesPage.vue");
const OrgAccessPage = () => import("@/org/OrgAccessPage.vue");
const OrgModelsPage = () => import("@/org/OrgModelsPage.vue");
const OrgUsagePage = () => import("@/org/OrgUsagePage.vue");
const SchedulingPolicyPage = () => import("@/settings/SchedulingPolicyPage.vue");
const ToolsPage = () => import("@/settings/ToolsPage.vue");
const InstructionsPage = () => import("@/settings/InstructionsPage.vue");
@@ -306,6 +307,12 @@ const routes = [
component: () => import("@/settings/PlaybooksPage.vue"),
meta: { section: "org_config", label: "Playbooks", navPath: "/org" },
},
{
path: "usage",
name: "org-usage",
component: OrgUsagePage,
meta: { section: "org_usage", label: "Usage", navPath: "/org" },
},
],
},
// The old unified Access area and the org-owned settings that used to sit