diff --git a/CHANGELOG.md b/CHANGELOG.md index d63883f54..20da07c3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/app/archied/model_catalog.go b/internal/app/archied/model_catalog.go index 770d1d699..190fa4955 100644 --- a/internal/app/archied/model_catalog.go +++ b/internal/app/archied/model_catalog.go @@ -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) }) diff --git a/internal/app/archiegateway/usage.go b/internal/app/archiegateway/usage.go index 695261293..d6c28a465 100644 --- a/internal/app/archiegateway/usage.go +++ b/internal/app/archiegateway/usage.go @@ -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) diff --git a/internal/app/archieui/compose.go b/internal/app/archieui/compose.go index 16f01ea63..f51906e5e 100644 --- a/internal/app/archieui/compose.go +++ b/internal/app/archieui/compose.go @@ -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, diff --git a/internal/app/archieui/run.go b/internal/app/archieui/run.go index b86f9f5d4..ecc6deb60 100644 --- a/internal/app/archieui/run.go +++ b/internal/app/archieui/run.go @@ -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), diff --git a/internal/app/statestore/server.go b/internal/app/statestore/server.go index e6729d42c..753b34282 100644 --- a/internal/app/statestore/server.go +++ b/internal/app/statestore/server.go @@ -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 } diff --git a/internal/contracts/state/v1/execution.pb.go b/internal/contracts/state/v1/execution.pb.go index 451f94773..778e55937 100644 --- a/internal/contracts/state/v1/execution.pb.go +++ b/internal/contracts/state/v1/execution.pb.go @@ -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" + diff --git a/internal/contracts/state/v1/stats.pb.go b/internal/contracts/state/v1/stats.pb.go index 6026bd1d7..8d1b954f4 100644 --- a/internal/contracts/state/v1/stats.pb.go +++ b/internal/contracts/state/v1/stats.pb.go @@ -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, }, diff --git a/internal/contracts/state/v1/stats_grpc.pb.go b/internal/contracts/state/v1/stats_grpc.pb.go index 650f73aab..a6303bed2 100644 --- a/internal/contracts/state/v1/stats_grpc.pb.go +++ b/internal/contracts/state/v1/stats_grpc.pb.go @@ -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", diff --git a/internal/domain/access/access.go b/internal/domain/access/access.go index 32c16ade6..2f60045f6 100644 --- a/internal/domain/access/access.go +++ b/internal/domain/access/access.go @@ -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, } } diff --git a/internal/domain/storecontract/task.go b/internal/domain/storecontract/task.go index 9de447068..32eac30df 100644 --- a/internal/domain/storecontract/task.go +++ b/internal/domain/storecontract/task.go @@ -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. diff --git a/internal/domain/usage/usage.go b/internal/domain/usage/usage.go index 34e35e12c..8fe14f09d 100644 --- a/internal/domain/usage/usage.go +++ b/internal/domain/usage/usage.go @@ -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, "/") diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index 387d586a4..38d437aa6 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -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 { diff --git a/internal/infrastructure/modelcatalog/catalog.go b/internal/infrastructure/modelcatalog/catalog.go index 6e39b2594..df481d20a 100644 --- a/internal/infrastructure/modelcatalog/catalog.go +++ b/internal/infrastructure/modelcatalog/catalog.go @@ -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) }) diff --git a/internal/infrastructure/postgres/migrations/0073_model_usage_identity.sql b/internal/infrastructure/postgres/migrations/0073_model_usage_identity.sql new file mode 100644 index 000000000..9f30739d7 --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0073_model_usage_identity.sql @@ -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; diff --git a/internal/infrastructure/postgres/postgresdb/models.go b/internal/infrastructure/postgres/postgresdb/models.go index f960f09c7..d7eb05c57 100644 --- a/internal/infrastructure/postgres/postgresdb/models.go +++ b/internal/infrastructure/postgres/postgresdb/models.go @@ -359,6 +359,7 @@ type ModelUsage struct { OutputTokens int64 CachedTokens int64 At time.Time + IdentityID string } type Org struct { diff --git a/internal/infrastructure/postgres/postgresdb/usage.sql.go b/internal/infrastructure/postgres/postgresdb/usage.sql.go index 080a62e89..39caf5a62 100644 --- a/internal/infrastructure/postgres/postgresdb/usage.sql.go +++ b/internal/infrastructure/postgres/postgresdb/usage.sql.go @@ -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 +} diff --git a/internal/infrastructure/postgres/queries/usage.sql b/internal/infrastructure/postgres/queries/usage.sql index 53f4136d6..d751377a1 100644 --- a/internal/infrastructure/postgres/queries/usage.sql +++ b/internal/infrastructure/postgres/queries/usage.sql @@ -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; diff --git a/internal/infrastructure/postgres/usage.go b/internal/infrastructure/postgres/usage.go index c7659cbd9..26c3d42a6 100644 --- a/internal/infrastructure/postgres/usage.go +++ b/internal/infrastructure/postgres/usage.go @@ -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 +} diff --git a/internal/infrastructure/staterpc/server.go b/internal/infrastructure/staterpc/server.go index b7ca138b8..aa699e830 100644 --- a/internal/infrastructure/staterpc/server.go +++ b/internal/infrastructure/staterpc/server.go @@ -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) diff --git a/internal/infrastructure/staterpc/stats.go b/internal/infrastructure/staterpc/stats.go index cf7f26b39..4a7f8c87d 100644 --- a/internal/infrastructure/staterpc/stats.go +++ b/internal/infrastructure/staterpc/stats.go @@ -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 diff --git a/internal/infrastructure/staterpc/usage_values.go b/internal/infrastructure/staterpc/usage_values.go index 65ea8e611..90c1e0cb1 100644 --- a/internal/infrastructure/staterpc/usage_values.go +++ b/internal/infrastructure/staterpc/usage_values.go @@ -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(), + } +} diff --git a/internal/infrastructure/taskmcp/mcp_sampling.go b/internal/infrastructure/taskmcp/mcp_sampling.go index e085fc203..12f69af4e 100644 --- a/internal/infrastructure/taskmcp/mcp_sampling.go +++ b/internal/infrastructure/taskmcp/mcp_sampling.go @@ -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) diff --git a/internal/webui/api_capabilities.go b/internal/webui/api_capabilities.go index 335355329..f702a9ca8 100644 --- a/internal/webui/api_capabilities.go +++ b/internal/webui/api_capabilities.go @@ -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"}, } } diff --git a/internal/webui/api_config.go b/internal/webui/api_config.go index 3ede9669f..5be26dd78 100644 --- a/internal/webui/api_config.go +++ b/internal/webui/api_config.go @@ -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 diff --git a/internal/webui/authorization_surfaces_test.go b/internal/webui/authorization_surfaces_test.go index 27257725a..5f94a80ea 100644 --- a/internal/webui/authorization_surfaces_test.go +++ b/internal/webui/authorization_surfaces_test.go @@ -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"}, diff --git a/internal/webui/authorize.go b/internal/webui/authorize.go index 70ee566e2..6f9c92cb7 100644 --- a/internal/webui/authorize.go +++ b/internal/webui/authorize.go @@ -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 } diff --git a/internal/webui/server.go b/internal/webui/server.go index 4108ed76f..e51a70750 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -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) diff --git a/internal/webui/usage.go b/internal/webui/usage.go new file mode 100644 index 000000000..53bc5d77a --- /dev/null +++ b/internal/webui/usage.go @@ -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 +} diff --git a/internal/webui/usage_test.go b/internal/webui/usage_test.go new file mode 100644 index 000000000..0ed2fb29f --- /dev/null +++ b/internal/webui/usage_test.go @@ -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) + } + }) + } +} diff --git a/proto/state/v1/execution.proto b/proto/state/v1/execution.proto index dbe5df6aa..87c53d290 100644 --- a/proto/state/v1/execution.proto +++ b/proto/state/v1/execution.proto @@ -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 {} diff --git a/proto/state/v1/stats.proto b/proto/state/v1/stats.proto index 83c7cab78..64e901e6c 100644 --- a/proto/state/v1/stats.proto +++ b/proto/state/v1/stats.proto @@ -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; } diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index fe48b6311..456e35114 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -255,6 +255,9 @@ export const api = { identityCommand: (id: string, command: string, body: { expected_version: number; display_name: string }) => request(`/api/identities/${encodeURIComponent(id)}/${encodeURIComponent(command)}`, { method: "POST", body }), summary: () => request("/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: (days = 30) => request("/api/usage" + qs({ days })), tasks: () => request("/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. diff --git a/ui/src/lib/format.ts b/ui/src/lib/format.ts index 1b29e213c..c594e1928 100644 --- a/ui/src/lib/format.ts +++ b/ui/src/lib/format.ts @@ -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); diff --git a/ui/src/lib/nav.ts b/ui/src/lib/nav.ts index 9b3a2141b..b7d493c5e 100644 --- a/ui/src/lib/nav.ts +++ b/ui/src/lib/nav.ts @@ -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"], + }, ], }, ]; diff --git a/ui/src/org/OrgShell.vue b/ui/src/org/OrgShell.vue index dcc61d6bd..d1358c904 100644 --- a/ui/src/org/OrgShell.vue +++ b/ui/src/org/OrgShell.vue @@ -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(); diff --git a/ui/src/org/OrgUsagePage.vue b/ui/src/org/OrgUsagePage.vue new file mode 100644 index 000000000..67521e48e --- /dev/null +++ b/ui/src/org/OrgUsagePage.vue @@ -0,0 +1,333 @@ + + + diff --git a/ui/src/router/index.ts b/ui/src/router/index.ts index be27faabb..dfe7ba791 100644 --- a/ui/src/router/index.ts +++ b/ui/src/router/index.ts @@ -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