Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: track page agent thread token usage (#19697)
p0nydad4b296a2df
diff --git a/backend/api/mcp/gen/openapi.yaml b/backend/api/mcp/gen/openapi.yamlindex ecc3a842dd..f3a9e2642d 100644--- a/backend/api/mcp/gen/openapi.yaml+++ b/backend/api/mcp/gen/openapi.yaml@@ -7162,6 +7162,12 @@ components:$ref: '#/components/schemas/bytebase.v1.AIChatToolCall'title: tool_callsdescription: Tool calls the AI wants to make.+ usage:+ oneOf:+ - $ref: '#/components/schemas/bytebase.v1.AIChatUsage'+ - type: "null"+ title: usage+ description: Token usage for this provider call, when available.title: AIChatResponseadditionalProperties: falsedescription: Response message for AIService.Chat.@@ -7209,6 +7215,17 @@ components:title: AIChatToolDefinitionadditionalProperties: falsedescription: A tool definition that the AI can invoke.+ bytebase.v1.AIChatUsage:+ type: object+ properties:+ totalTokens:+ type: integer+ title: total_tokens+ format: int32+ description: Total tokens used by the provider call.+ title: AIChatUsage+ additionalProperties: false+ description: Token usage for a single AI provider call.bytebase.v1.AICompletionRequest:type: objectproperties:diff --git a/backend/api/v1/ai_service.go b/backend/api/v1/ai_service.goindex 0c2f1044eb..62609f1f60 100644--- a/backend/api/v1/ai_service.go+++ b/backend/api/v1/ai_service.go@@ -91,6 +91,10 @@ type chatOpenAIFunctionCallRef struct {Arguments string `json:"arguments"`}+type chatOpenAIUsage struct {+ TotalTokens int32 `json:"total_tokens"`+}+type chatOpenAIResponse struct {Choices []struct {Message struct {@@ -99,6 +103,7 @@ type chatOpenAIResponse struct {ToolCalls []json.RawMessage `json:"tool_calls"`} `json:"message"`} `json:"choices"`+ Usage *chatOpenAIUsage `json:"usage,omitempty"`}// chatOpenAIToolCallParsed extracts the fields we need from a raw tool call JSON.@@ -276,6 +281,9 @@ func (*AIService) chatOpenAI(ctx context.Context, aiSetting *storepb.AISetting,}result := &v1pb.AIChatResponse{}+ if resp.Usage != nil {+ result.Usage = newAIChatUsage(resp.Usage.TotalTokens)+ }if len(resp.Choices) > 0 {msg := resp.Choices[0].Messageresult.Content = msg.Content@@ -333,8 +341,14 @@ type chatClaudeContentBlock struct {Content string `json:"content,omitempty"`}+type chatClaudeUsage struct {+ InputTokens int32 `json:"input_tokens"`+ OutputTokens int32 `json:"output_tokens"`+}+type chatClaudeResponse struct {Content []chatClaudeContentBlock `json:"content"`+ Usage *chatClaudeUsage `json:"usage,omitempty"`}func (*AIService) chatClaude(ctx context.Context, aiSetting *storepb.AISetting, request *v1pb.AIChatRequest) (*connect.Response[v1pb.AIChatResponse], error) {@@ -434,6 +448,9 @@ func (*AIService) chatClaude(ctx context.Context, aiSetting *storepb.AISetting,}result := &v1pb.AIChatResponse{}+ if resp.Usage != nil {+ result.Usage = newAIChatUsage(resp.Usage.InputTokens + resp.Usage.OutputTokens)+ }var textContent stringfor _, block := range resp.Content {switch block.Type {@@ -497,12 +514,17 @@ type chatGeminiFunctionDecl struct {Parameters any `json:"parameters"`}+type chatGeminiUsage struct {+ TotalTokenCount int32 `json:"totalTokenCount"`+}+type chatGeminiResponse struct {Candidates []struct {Content struct {Parts []chatGeminiResponsePart `json:"parts"`} `json:"content"`} `json:"candidates"`+ UsageMetadata *chatGeminiUsage `json:"usageMetadata,omitempty"`}type chatGeminiResponsePart struct {@@ -618,6 +640,9 @@ func (*AIService) chatGemini(ctx context.Context, aiSetting *storepb.AISetting,}result := &v1pb.AIChatResponse{}+ if resp.UsageMetadata != nil {+ result.Usage = newAIChatUsage(resp.UsageMetadata.TotalTokenCount)+ }if len(resp.Candidates) > 0 {var textContent stringfor _, part := range resp.Candidates[0].Content.Parts {@@ -651,6 +676,15 @@ func (*AIService) chatGemini(ctx context.Context, aiSetting *storepb.AISetting,return connect.NewResponse(result), nil}+func newAIChatUsage(totalTokens int32) *v1pb.AIChatUsage {+ if totalTokens <= 0 {+ return nil+ }+ return &v1pb.AIChatUsage{+ TotalTokens: totalTokens,+ }+}+// doHTTPRequest executes an HTTP request and returns the response body.func doHTTPRequest(_ context.Context, httpReq *http.Request, provider string) ([]byte, error) {client := &http.Client{}diff --git a/backend/generated-go/v1/ai_service.pb.go b/backend/generated-go/v1/ai_service.pb.goindex 2d70c757be..3fdc64e964 100644--- a/backend/generated-go/v1/ai_service.pb.go+++ b/backend/generated-go/v1/ai_service.pb.go@@ -349,20 +349,68 @@ func (x *AIChatRequest) GetToolDefinitions() []*AIChatToolDefinition {return nil}+// Token usage for a single AI provider call.+type AIChatUsage struct {+ state protoimpl.MessageState `protogen:"open.v1"`+ // Total tokens used by the provider call.+ TotalTokens int32 `protobuf:"varint,1,opt,name=total_tokens,json=totalTokens,proto3" json:"total_tokens,omitempty"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache+}++func (x *AIChatUsage) Reset() {+ *x = AIChatUsage{}+ mi := &file_v1_ai_service_proto_msgTypes[4]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+}++func (x *AIChatUsage) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*AIChatUsage) ProtoMessage() {}++func (x *AIChatUsage) ProtoReflect() protoreflect.Message {+ mi := &file_v1_ai_service_proto_msgTypes[4]+ 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 AIChatUsage.ProtoReflect.Descriptor instead.+func (*AIChatUsage) Descriptor() ([]byte, []int) {+ return file_v1_ai_service_proto_rawDescGZIP(), []int{4}+}++func (x *AIChatUsage) GetTotalTokens() int32 {+ if x != nil {+ return x.TotalTokens+ }+ return 0+}+// Response message for AIService.Chat.type AIChatResponse struct {state protoimpl.MessageState `protogen:"open.v1"`// The text content of the AI response. Optional when the response only contains tool calls.Content *string `protobuf:"bytes,1,opt,name=content,proto3,oneof" json:"content,omitempty"`// Tool calls the AI wants to make.- ToolCalls []*AIChatToolCall `protobuf:"bytes,2,rep,name=tool_calls,json=toolCalls,proto3" json:"tool_calls,omitempty"`+ ToolCalls []*AIChatToolCall `protobuf:"bytes,2,rep,name=tool_calls,json=toolCalls,proto3" json:"tool_calls,omitempty"`+ // Token usage for this provider call, when available.+ Usage *AIChatUsage `protobuf:"bytes,3,opt,name=usage,proto3,oneof" json:"usage,omitempty"`unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCache}func (x *AIChatResponse) Reset() {*x = AIChatResponse{}- mi := &file_v1_ai_service_proto_msgTypes[4]+ mi := &file_v1_ai_service_proto_msgTypes[5]ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))ms.StoreMessageInfo(mi)}@@ -374,7 +422,7 @@ func (x *AIChatResponse) String() string {func (*AIChatResponse) ProtoMessage() {}func (x *AIChatResponse) ProtoReflect() protoreflect.Message {- mi := &file_v1_ai_service_proto_msgTypes[4]+ mi := &file_v1_ai_service_proto_msgTypes[5]if x != nil {ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))if ms.LoadMessageInfo() == nil {@@ -387,7 +435,7 @@ func (x *AIChatResponse) ProtoReflect() protoreflect.Message {// Deprecated: Use AIChatResponse.ProtoReflect.Descriptor instead.func (*AIChatResponse) Descriptor() ([]byte, []int) {- return file_v1_ai_service_proto_rawDescGZIP(), []int{4}+ return file_v1_ai_service_proto_rawDescGZIP(), []int{5}}func (x *AIChatResponse) GetContent() string {@@ -404,6 +452,13 @@ func (x *AIChatResponse) GetToolCalls() []*AIChatToolCall {return nil}+func (x *AIChatResponse) GetUsage() *AIChatUsage {+ if x != nil {+ return x.Usage+ }+ return nil+}+var File_v1_ai_service_proto protoreflect.FileDescriptorconst file_v1_ai_service_proto_rawDesc = "" +@@ -431,13 +486,17 @@ const file_v1_ai_service_proto_rawDesc = "" +"\t_metadata\"\x95\x01\n" +"\rAIChatRequest\x126\n" +"\bmessages\x18\x01 \x03(\v2\x1a.bytebase.v1.AIChatMessageR\bmessages\x12L\n" +- "\x10tool_definitions\x18\x02 \x03(\v2!.bytebase.v1.AIChatToolDefinitionR\x0ftoolDefinitions\"w\n" ++ "\x10tool_definitions\x18\x02 \x03(\v2!.bytebase.v1.AIChatToolDefinitionR\x0ftoolDefinitions\"0\n" ++ "\vAIChatUsage\x12!\n" ++ "\ftotal_tokens\x18\x01 \x01(\x05R\vtotalTokens\"\xb6\x01\n" +"\x0eAIChatResponse\x12\x1d\n" +"\acontent\x18\x01 \x01(\tH\x00R\acontent\x88\x01\x01\x12:\n" +"\n" +- "tool_calls\x18\x02 \x03(\v2\x1b.bytebase.v1.AIChatToolCallR\ttoolCallsB\n" ++ "tool_calls\x18\x02 \x03(\v2\x1b.bytebase.v1.AIChatToolCallR\ttoolCalls\x123\n" ++ "\x05usage\x18\x03 \x01(\v2\x18.bytebase.v1.AIChatUsageH\x01R\x05usage\x88\x01\x01B\n" +"\n" +- "\b_content*\xbc\x01\n" ++ "\b_contentB\b\n" ++ "\x06_usage*\xbc\x01\n" +"\x11AIChatMessageRole\x12$\n" +" AI_CHAT_MESSAGE_ROLE_UNSPECIFIED\x10\x00\x12\x1f\n" +"\x1bAI_CHAT_MESSAGE_ROLE_SYSTEM\x10\x01\x12\x1d\n" +@@ -461,14 +520,15 @@ func file_v1_ai_service_proto_rawDescGZIP() []byte {}var file_v1_ai_service_proto_enumTypes = make([]protoimpl.EnumInfo, 1)-var file_v1_ai_service_proto_msgTypes = make([]protoimpl.MessageInfo, 5)+var file_v1_ai_service_proto_msgTypes = make([]protoimpl.MessageInfo, 6)var file_v1_ai_service_proto_goTypes = []any{(AIChatMessageRole)(0), // 0: bytebase.v1.AIChatMessageRole(*AIChatMessage)(nil), // 1: bytebase.v1.AIChatMessage(*AIChatToolDefinition)(nil), // 2: bytebase.v1.AIChatToolDefinition(*AIChatToolCall)(nil), // 3: bytebase.v1.AIChatToolCall(*AIChatRequest)(nil), // 4: bytebase.v1.AIChatRequest- (*AIChatResponse)(nil), // 5: bytebase.v1.AIChatResponse+ (*AIChatUsage)(nil), // 5: bytebase.v1.AIChatUsage+ (*AIChatResponse)(nil), // 6: bytebase.v1.AIChatResponse}var file_v1_ai_service_proto_depIdxs = []int32{0, // 0: bytebase.v1.AIChatMessage.role:type_name -> bytebase.v1.AIChatMessageRole@@ -476,13 +536,14 @@ var file_v1_ai_service_proto_depIdxs = []int32{1, // 2: bytebase.v1.AIChatRequest.messages:type_name -> bytebase.v1.AIChatMessage2, // 3: bytebase.v1.AIChatRequest.tool_definitions:type_name -> bytebase.v1.AIChatToolDefinition3, // 4: bytebase.v1.AIChatResponse.tool_calls:type_name -> bytebase.v1.AIChatToolCall- 4, // 5: bytebase.v1.AIService.Chat:input_type -> bytebase.v1.AIChatRequest- 5, // 6: bytebase.v1.AIService.Chat:output_type -> bytebase.v1.AIChatResponse- 6, // [6:7] is the sub-list for method output_type- 5, // [5:6] 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+ 5, // 5: bytebase.v1.AIChatResponse.usage:type_name -> bytebase.v1.AIChatUsage+ 4, // 6: bytebase.v1.AIService.Chat:input_type -> bytebase.v1.AIChatRequest+ 6, // 7: bytebase.v1.AIService.Chat:output_type -> bytebase.v1.AIChatResponse+ 7, // [7:8] is the sub-list for method output_type+ 6, // [6:7] is the sub-list for method input_type+ 6, // [6:6] is the sub-list for extension type_name+ 6, // [6:6] is the sub-list for extension extendee+ 0, // [0:6] is the sub-list for field type_name}func init() { file_v1_ai_service_proto_init() }@@ -493,14 +554,14 @@ func file_v1_ai_service_proto_init() {file_v1_annotation_proto_init()file_v1_ai_service_proto_msgTypes[0].OneofWrappers = []any{}file_v1_ai_service_proto_msgTypes[2].OneofWrappers = []any{}- file_v1_ai_service_proto_msgTypes[4].OneofWrappers = []any{}+ file_v1_ai_service_proto_msgTypes[5].OneofWrappers = []any{}type x struct{}out := protoimpl.TypeBuilder{File: protoimpl.DescBuilder{GoPackagePath: reflect.TypeOf(x{}).PkgPath(),RawDescriptor: unsafe.Slice(unsafe.StringData(file_v1_ai_service_proto_rawDesc), len(file_v1_ai_service_proto_rawDesc)),NumEnums: 1,- NumMessages: 5,+ NumMessages: 6,NumExtensions: 0,NumServices: 1,},diff --git a/backend/generated-go/v1/ai_service_equal.pb.go b/backend/generated-go/v1/ai_service_equal.pb.goindex 6e7a24606d..d341601f49 100644--- a/backend/generated-go/v1/ai_service_equal.pb.go+++ b/backend/generated-go/v1/ai_service_equal.pb.go@@ -97,6 +97,19 @@ func (x *AIChatRequest) Equal(y *AIChatRequest) bool {return true}+func (x *AIChatUsage) Equal(y *AIChatUsage) bool {+ if x == y {+ return true+ }+ if x == nil || y == nil {+ return x == nil && y == nil+ }+ if x.TotalTokens != y.TotalTokens {+ return false+ }+ return true+}+func (x *AIChatResponse) Equal(y *AIChatResponse) bool {if x == y {return true@@ -115,5 +128,8 @@ func (x *AIChatResponse) Equal(y *AIChatResponse) bool {return false}}+ if !x.Usage.Equal(y.Usage) {+ return false+ }return true}diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 7d4be9ff30..436ce4c49a 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -2982,6 +2982,7 @@"thread-status-awaiting-user": "Awaiting input","thread-status-error": "Error","thread-status-interrupted": "Interrupted",+ "thread-total-tokens": "Tokens used: {count}","reply": "Reply","confirm": "Confirm","cancel": "Cancel",diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex a552028640..91ceff5f20 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -2491,6 +2491,7 @@"thread-status-awaiting-user": "Esperando entrada","thread-status-error": "Error","thread-status-interrupted": "Interrumpido",+ "thread-total-tokens": "Tokens usados: {count}","reply": "Responder","confirm": "Confirmar","cancel": "Cancelar",diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex 01f6a16195..01493b1b1a 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -2491,6 +2491,7 @@"thread-status-awaiting-user": "入力待ち","thread-status-error": "エラー","thread-status-interrupted": "中断",+ "thread-total-tokens": "使用済みトークン: {count}","reply": "返信","confirm": "確認","cancel": "キャンセル",diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.jsonindex 6b269e7011..28b29fb8ca 100644--- a/frontend/src/locales/vi-VN.json+++ b/frontend/src/locales/vi-VN.json@@ -2491,6 +2491,7 @@"thread-status-awaiting-user": "Đang chờ nhập","thread-status-error": "Lỗi","thread-status-interrupted": "Bị gián đoạn",+ "thread-total-tokens": "Token đã dùng: {count}","reply": "Trả lời","confirm": "Xác nhận","cancel": "Hủy",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex 630f5e9a0f..35a8292286 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -2982,6 +2982,7 @@"thread-status-awaiting-user": "等待输入","thread-status-error": "错误","thread-status-interrupted": "已中断",+ "thread-total-tokens": "已用令牌:{count}","reply": "回复","confirm": "确认","cancel": "取消",diff --git a/frontend/src/plugins/agent/components/AgentInput.test.ts b/frontend/src/plugins/agent/components/AgentInput.test.tsindex 1733d4eb27..7f150a3159 100644--- a/frontend/src/plugins/agent/components/AgentInput.test.ts+++ b/frontend/src/plugins/agent/components/AgentInput.test.ts@@ -224,6 +224,31 @@ describe("AgentInput", () => {});});+ test("increments the current thread token total from loop usage", async () => {+ const store = useAgentStore();+ const threadId = store.currentThreadId!;++ mockRunAgentLoop.mockResolvedValue({+ kind: "completed",+ text: "Done",+ success: true,+ explicit: true,+ totalTokensUsed: 123,+ });++ const wrapper = mount(AgentInput, {+ global: {+ plugins: [pinia, i18n],+ },+ });++ await wrapper.find("textarea").setValue("inspect this page");+ await findButtonByText(wrapper, "Send")!.trigger("click");+ await flushPromises();++ expect(store.getThread(threadId)?.totalTokensUsed).toBe(123);+ });+test("refreshes the saved thread page snapshot after navigate before a follow-up turn", async () => {const store = useAgentStore();const threadId = store.currentThreadId!;diff --git a/frontend/src/plugins/agent/components/AgentInput.vue b/frontend/src/plugins/agent/components/AgentInput.vueindex 87ae491a55..1b12b9752e 100644--- a/frontend/src/plugins/agent/components/AgentInput.vue+++ b/frontend/src/plugins/agent/components/AgentInput.vue@@ -188,6 +188,11 @@ async function runThread(controller.signal);+ agentStore.incrementThreadTotalTokens(+ threadId,+ outcome.totalTokensUsed ?? 0+ );+if (runToken !== currentRunToken) {return;}diff --git a/frontend/src/plugins/agent/components/AgentWindow.vue b/frontend/src/plugins/agent/components/AgentWindow.vueindex d0d5822ba0..bc0a4ef3d0 100644--- a/frontend/src/plugins/agent/components/AgentWindow.vue+++ b/frontend/src/plugins/agent/components/AgentWindow.vue@@ -110,6 +110,15 @@ const currentThreadStatusClass = computed(() => {return "bg-amber-50 text-amber-600";});+const tokenFormatter = new Intl.NumberFormat();+const currentThreadTokenUsageLabel = computed(() =>+ t("agent.thread-total-tokens", {+ count: tokenFormatter.format(+ agentStore.currentThread?.totalTokensUsed ?? 0+ ),+ })+);+function syncSize(width: number, height: number) {const size = getDisplaySize(width, height);agentStore.size.width = size.width;@@ -336,6 +345,9 @@ onBeforeUnmount(() => {>{{ currentThreadStatusLabel }}</span>+ <span class="truncate text-xs text-gray-500">+ {{ currentThreadTokenUsageLabel }}+ </span></div><div class="flex items-center gap-x-1"><buttondiff --git a/frontend/src/plugins/agent/logic/agentLoop.test.ts b/frontend/src/plugins/agent/logic/agentLoop.test.tsindex ab6f2d5f80..8b12f0b5fc 100644--- a/frontend/src/plugins/agent/logic/agentLoop.test.ts+++ b/frontend/src/plugins/agent/logic/agentLoop.test.ts@@ -54,6 +54,7 @@ describe("runAgentLoop", () => {text: "Finished successfully",success: true,explicit: true,+ totalTokensUsed: 0,});expect(onToolResult).toHaveBeenCalledWith("tool-1",@@ -80,6 +81,50 @@ describe("runAgentLoop", () => {});});+ test("accumulates token usage across repeated provider calls", async () => {+ vi.mocked(aiServiceClientConnect.chat)+ .mockResolvedValueOnce({+ content: "",+ toolCalls: [+ {+ id: "tool-1",+ name: "search_api",+ arguments: JSON.stringify({ service: "SQLService" }),+ metadata: "",+ },+ ],+ usage: {+ totalTokens: 11,+ },+ } as never)+ .mockResolvedValueOnce({+ content: "Final answer",+ toolCalls: [],+ usage: {+ totalTokens: 7,+ },+ } as never);++ const executeTool: ToolExecutor = vi.fn(async () => ({+ kind: "tool_result" as const,+ result: JSON.stringify({ ok: true }),+ }));++ const outcome = await runAgentLoop(+ [{ role: "system", content: "system" }],+ [],+ executeTool+ );++ expect(outcome).toEqual({+ kind: "completed",+ text: "Final answer",+ success: true,+ explicit: false,+ totalTokensUsed: 18,+ });+ });+test("replays completed tool flows with matched tool results", async () => {vi.mocked(aiServiceClientConnect.chat).mockResolvedValueOnce({@@ -195,6 +240,7 @@ describe("runAgentLoop", () => {text: "Here is the answer",success: true,explicit: false,+ totalTokensUsed: 0,});expect(onText).toHaveBeenCalledWith("Here is the answer");expect(executeTool).not.toHaveBeenCalled();@@ -254,6 +300,7 @@ describe("runAgentLoop", () => {{ label: "Staging", value: "staging" },],},+ totalTokensUsed: 0,});expect(onToolResult).not.toHaveBeenCalled();});@@ -312,6 +359,7 @@ describe("runAgentLoop", () => {prompt: "Which environment should I use?",kind: "input",},+ totalTokensUsed: 0,});expect(executeTool).toHaveBeenCalledTimes(1);expect(executeTool).toHaveBeenCalledWith(@@ -384,6 +432,7 @@ describe("runAgentLoop", () => {text: "Finished successfully",success: true,explicit: true,+ totalTokensUsed: 0,});expect(executeTool).toHaveBeenCalledTimes(1);expect(executeTool).toHaveBeenCalledWith(diff --git a/frontend/src/plugins/agent/logic/agentLoop.ts b/frontend/src/plugins/agent/logic/agentLoop.tsindex 53f72df407..d3d15379e9 100644--- a/frontend/src/plugins/agent/logic/agentLoop.ts+++ b/frontend/src/plugins/agent/logic/agentLoop.ts@@ -120,11 +120,12 @@ export async function runAgentLoop(): Promise<AgentLoopOutcome> {const conversation: Message[] = [...messages];const protoTools = tools.map(toolDefToProto);+ let totalTokensUsed = 0;try {for (let i = 0; i < MAX_ITERATIONS; i++) {if (signal?.aborted) {- return { kind: "aborted" };+ return { kind: "aborted", totalTokensUsed };}const protoMessages = conversation.map(messageToProto);@@ -141,6 +142,8 @@ export async function runAgentLoop(signal);+ totalTokensUsed += response.usage?.totalTokens ?? 0;+if (response.toolCalls.length > 0) {const toolCalls: ToolCall[] = response.toolCalls.map((tc) => ({id: tc.id,@@ -206,6 +209,7 @@ export async function runAgentLoop(outcome: {kind: "awaiting_user",ask: executionResult.ask,+ totalTokensUsed,},blockedBy: {toolCallId: tc.id,@@ -231,6 +235,7 @@ export async function runAgentLoop(text: executionResult.text,success: executionResult.success,explicit: true,+ totalTokensUsed,},blockedBy: {toolCallId: tc.id,@@ -256,21 +261,24 @@ export async function runAgentLoop(text,success: true,explicit: false,+ totalTokensUsed,};}return {kind: "error",error: new Error("Agent loop exceeded maximum iterations"),+ totalTokensUsed,};} catch (err) {const error = err instanceof Error ? err : new Error(String(err));if (error.name === "AbortError") {- return { kind: "aborted" };+ return { kind: "aborted", totalTokensUsed };}return {kind: "error",error,+ totalTokensUsed,};}}diff --git a/frontend/src/plugins/agent/logic/types.ts b/frontend/src/plugins/agent/logic/types.tsindex 446af59d94..6e44dc716d 100644--- a/frontend/src/plugins/agent/logic/types.ts+++ b/frontend/src/plugins/agent/logic/types.ts@@ -46,6 +46,7 @@ export interface AgentThread {createdTs: number;updatedTs: number;status: AgentThreadStatus;+ totalTokensUsed: number;page?: AgentThreadSnapshot;lastError?: string | null;interrupted?: boolean;@@ -101,24 +102,28 @@ export type ToolExecutionResult =ask: AgentPendingAsk;};+interface AgentLoopUsage {+ totalTokensUsed?: number;+}+export type AgentLoopOutcome =- | {+ | ({kind: "completed";text: string;success: boolean;explicit: boolean;- }- | {+ } & AgentLoopUsage)+ | ({kind: "awaiting_user";ask: AgentPendingAsk;- }- | {+ } & AgentLoopUsage)+ | ({kind: "aborted";- }- | {+ } & AgentLoopUsage)+ | ({kind: "error";error: Error;- };+ } & AgentLoopUsage);export type ToolExecutor = (name: string,diff --git a/frontend/src/plugins/agent/store/agent.test.ts b/frontend/src/plugins/agent/store/agent.test.tsindex 88cf610724..60ce94e72a 100644--- a/frontend/src/plugins/agent/store/agent.test.ts+++ b/frontend/src/plugins/agent/store/agent.test.ts@@ -54,6 +54,7 @@ describe("useAgentStore", () => {expect(store.threads).toHaveLength(1);expect(store.currentThreadId).toBe(store.threads[0].id);expect(store.messages).toEqual([]);+ expect(store.threads[0].totalTokensUsed).toBe(0);});test("migrates legacy flat messages into a thread-aware state", () => {@@ -179,6 +180,24 @@ describe("useAgentStore", () => {});});+ test("increments and resets thread token totals", async () => {+ const store = createStore();+ const threadId = store.currentThreadId!;++ store.incrementThreadTotalTokens(threadId, 120);+ store.incrementThreadTotalTokens(threadId, 30);++ expect(store.getThread(threadId)?.totalTokensUsed).toBe(150);++ await nextTick();++ const rehydratedStore = createStore();+ expect(rehydratedStore.getThread(threadId)?.totalTokensUsed).toBe(150);++ rehydratedStore.clearConversation(threadId);+ expect(rehydratedStore.getThread(threadId)?.totalTokensUsed).toBe(0);+ });+test("preserves an existing thread page when resuming a run", () => {const store = createStore();const threadId = store.currentThreadId!;diff --git a/frontend/src/plugins/agent/store/agent.ts b/frontend/src/plugins/agent/store/agent.tsindex b8eeaf73da..ec2e2d6086 100644--- a/frontend/src/plugins/agent/store/agent.ts+++ b/frontend/src/plugins/agent/store/agent.ts@@ -45,6 +45,7 @@ const createThreadRecord = (options: CreateThreadOptions = {}): AgentThread => {createdTs: now,updatedTs: now,status: DEFAULT_THREAD_STATUS,+ totalTokensUsed: 0,page: options.page,lastError: null,interrupted: false,@@ -174,6 +175,12 @@ const normalizeThread = (raw: unknown): AgentThread => {createdTs: typeof thread.createdTs === "number" ? thread.createdTs : now,updatedTs: typeof thread.updatedTs === "number" ? thread.updatedTs : now,status,+ totalTokensUsed:+ typeof thread.totalTokensUsed === "number" &&+ Number.isFinite(thread.totalTokensUsed) &&+ thread.totalTokensUsed >= 0+ ? thread.totalTokensUsed+ : 0,page:isRecord(thread.page) &&typeof thread.page.path === "string" &&@@ -512,6 +519,19 @@ export const useAgentStore = defineStore("agent", () => {return message;};+ const incrementThreadTotalTokens = (+ threadId: string,+ totalTokensUsed: number+ ) => {+ const thread = getThread(threadId);+ if (!thread || totalTokensUsed <= 0) {+ return null;+ }+ thread.totalTokensUsed += totalTokensUsed;+ touchThread(threadId);+ return thread;+ };+const awaitUser = (threadId: string, pendingAsk: AgentPendingAsk) => {setThreadStatus(threadId, "awaiting_user");return setPendingAsk(threadId, pendingAsk);@@ -547,6 +567,7 @@ export const useAgentStore = defineStore("agent", () => {thread.title = "";thread.page = undefined;thread.status = DEFAULT_THREAD_STATUS;+ thread.totalTokensUsed = 0;thread.lastError = null;thread.interrupted = false;thread.runId = null;@@ -760,6 +781,7 @@ export const useAgentStore = defineStore("agent", () => {awaitUser,answerPendingAsk,touchThread,+ incrementThreadTotalTokens,addMessage,removeMessagesByRunId,appendToolCall,diff --git a/frontend/src/types/proto-es/v1/ai_service_pb.d.ts b/frontend/src/types/proto-es/v1/ai_service_pb.d.tsindex 42090dca63..7df5b02d76 100644--- a/frontend/src/types/proto-es/v1/ai_service_pb.d.ts+++ b/frontend/src/types/proto-es/v1/ai_service_pb.d.ts@@ -154,6 +154,26 @@ export declare type AIChatRequest = Message<"bytebase.v1.AIChatRequest"> & {*/export declare const AIChatRequestSchema: GenMessage<AIChatRequest>;+/**+ * Token usage for a single AI provider call.+ *+ * @generated from message bytebase.v1.AIChatUsage+ */+export declare type AIChatUsage = Message<"bytebase.v1.AIChatUsage"> & {+ /**+ * Total tokens used by the provider call.+ *+ * @generated from field: int32 total_tokens = 1;+ */+ totalTokens: number;+};++/**+ * Describes the message bytebase.v1.AIChatUsage.+ * Use `create(AIChatUsageSchema)` to create a new message.+ */+export declare const AIChatUsageSchema: GenMessage<AIChatUsage>;+/*** Response message for AIService.Chat.*@@ -173,6 +193,13 @@ export declare type AIChatResponse = Message<"bytebase.v1.AIChatResponse"> & {* @generated from field: repeated bytebase.v1.AIChatToolCall tool_calls = 2;*/toolCalls: AIChatToolCall[];++ /**+ * Token usage for this provider call, when available.+ *+ * @generated from field: optional bytebase.v1.AIChatUsage usage = 3;+ */+ usage?: AIChatUsage;};/**diff --git a/frontend/src/types/proto-es/v1/ai_service_pb.js b/frontend/src/types/proto-es/v1/ai_service_pb.jsindex 7e80684338..c6a33c1d44 100644--- a/frontend/src/types/proto-es/v1/ai_service_pb.js+++ b/frontend/src/types/proto-es/v1/ai_service_pb.js@@ -10,7 +10,7 @@ import { file_v1_annotation } from "./annotation_pb";* Describes the file v1/ai_service.proto.*/export const file_v1_ai_service = /*@__PURE__*/- fileDesc("ChN2MS9haV9zZXJ2aWNlLnByb3RvEgtieXRlYmFzZS52MSK8AQoNQUlDaGF0TWVzc2FnZRIsCgRyb2xlGAEgASgOMh4uYnl0ZWJhc2UudjEuQUlDaGF0TWVzc2FnZVJvbGUSFAoHY29udGVudBgCIAEoCUgAiAEBEi8KCnRvb2xfY2FsbHMYAyADKAsyGy5ieXRlYmFzZS52MS5BSUNoYXRUb29sQ2FsbBIZCgx0b29sX2NhbGxfaWQYBCABKAlIAYgBAUIKCghfY29udGVudEIPCg1fdG9vbF9jYWxsX2lkIlQKFEFJQ2hhdFRvb2xEZWZpbml0aW9uEgwKBG5hbWUYASABKAkSEwoLZGVzY3JpcHRpb24YAiABKAkSGQoRcGFyYW1ldGVyc19zY2hlbWEYAyABKAkiYQoOQUlDaGF0VG9vbENhbGwSCgoCaWQYASABKAkSDAoEbmFtZRgCIAEoCRIRCglhcmd1bWVudHMYAyABKAkSFQoIbWV0YWRhdGEYBCABKAlIAIgBAUILCglfbWV0YWRhdGEiegoNQUlDaGF0UmVx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k9MRV9BU1NJU1RBTlQQAxIdChlBSV9DSEFUX01FU1NBR0VfUk9MRV9UT09MEAQyaAoJQUlTZXJ2aWNlElsKBENoYXQSGi5ieXRlYmFzZS52MS5BSUNoYXRSZXF1ZXN0GhsuYnl0ZWJhc2UudjEuQUlDaGF0UmVzcG9uc2UiGpDqMAKC0+STAhA6ASoiCy92MS9haS9jaGF0QqQBCg9jb20uYnl0ZWJhc2UudjFCDkFpU2VydmljZVByb3RvUAFaNGdpdGh1Yi5jb20vYnl0ZWJhc2UvYnl0ZWJhc2UvYmFja2VuZC9nZW5lcmF0ZWQtZ28vdjGiAgNCWFiqAgtCeXRlYmFzZS5WMcoCC0J5dGViYXNlXFYx4gIXQnl0ZWJhc2VcVjFcR1BCTWV0YWRhdGHqAgxCeXRlYmFzZTo6VjFiBnByb3RvMw", [file_google_api_annotations, file_v1_annotation]);/*** Describes the message bytebase.v1.AIChatMessage.@@ -40,12 +40,19 @@ export const AIChatToolCallSchema = /*@__PURE__*/export const AIChatRequestSchema = /*@__PURE__*/messageDesc(file_v1_ai_service, 3);+/**+ * Describes the message bytebase.v1.AIChatUsage.+ * Use `create(AIChatUsageSchema)` to create a new message.+ */+export const AIChatUsageSchema = /*@__PURE__*/+ messageDesc(file_v1_ai_service, 4);+/*** Describes the message bytebase.v1.AIChatResponse.* Use `create(AIChatResponseSchema)` to create a new message.*/export const AIChatResponseSchema = /*@__PURE__*/- messageDesc(file_v1_ai_service, 4);+ messageDesc(file_v1_ai_service, 5);/*** Describes the enum bytebase.v1.AIChatMessageRole.diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yamlindex f185874a3c..11ce8208ba 100644--- a/proto/gen/grpc-doc/openapi.yaml+++ b/proto/gen/grpc-doc/openapi.yaml@@ -16211,6 +16211,12 @@ components:$ref: '#/components/schemas/bytebase.v1.AIChatToolCall'title: tool_callsdescription: Tool calls the AI wants to make.+ usage:+ oneOf:+ - $ref: '#/components/schemas/bytebase.v1.AIChatUsage'+ - type: "null"+ title: usage+ description: Token usage for this provider call, when available.title: AIChatResponseadditionalProperties: falsedescription: Response message for AIService.Chat.@@ -16258,6 +16264,17 @@ components:title: AIChatToolDefinitionadditionalProperties: falsedescription: A tool definition that the AI can invoke.+ bytebase.v1.AIChatUsage:+ type: object+ properties:+ totalTokens:+ type: integer+ title: total_tokens+ format: int32+ description: Total tokens used by the provider call.+ title: AIChatUsage+ additionalProperties: false+ description: Token usage for a single AI provider call.bytebase.v1.AICompletionRequest:type: objectproperties:diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.mdindex 8dac9b391d..cecbd82a11 100644--- a/proto/gen/grpc-doc/v1/README.md+++ b/proto/gen/grpc-doc/v1/README.md@@ -140,6 +140,7 @@- [AIChatResponse](#bytebase-v1-AIChatResponse)- [AIChatToolCall](#bytebase-v1-AIChatToolCall)- [AIChatToolDefinition](#bytebase-v1-AIChatToolDefinition)+ - [AIChatUsage](#bytebase-v1-AIChatUsage)- [AIChatMessageRole](#bytebase-v1-AIChatMessageRole)@@ -2739,6 +2740,7 @@ Response message for AIService.Chat.| ----- | ---- | ----- | ----------- || content | [string](#string) | optional | The text content of the AI response. Optional when the response only contains tool calls. || tool_calls | [AIChatToolCall](#bytebase-v1-AIChatToolCall) | repeated | Tool calls the AI wants to make. |+| usage | [AIChatUsage](#bytebase-v1-AIChatUsage) | optional | Token usage for this provider call, when available. |@@ -2779,6 +2781,21 @@ A tool definition that the AI can invoke.++<a name="bytebase-v1-AIChatUsage"></a>++### AIChatUsage+Token usage for a single AI provider call.+++| Field | Type | Label | Description |+| ----- | ---- | ----- | ----------- |+| total_tokens | [int32](#int32) | | Total tokens used by the provider call. |+++++diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.htmlindex bf3d6da9f3..3acfe1f241 100644--- a/proto/gen/grpc-doc/v1/index.html+++ b/proto/gen/grpc-doc/v1/index.html@@ -704,6 +704,10 @@<a href="#bytebase.v1.AIChatToolDefinition"><span class="badge">M</span>AIChatToolDefinition</a></li>+ <li>+ <a href="#bytebase.v1.AIChatUsage"><span class="badge">M</span>AIChatUsage</a>+ </li>+<li><a href="#bytebase.v1.AIChatMessageRole"><span class="badge">E</span>AIChatMessageRole</a>@@ -8328,6 +8332,13 @@ Permissions required: None</p></td><td><p>Tool calls the AI wants to make. </p></td></tr>+ <tr>+ <td>usage</td>+ <td><a href="#bytebase.v1.AIChatUsage">AIChatUsage</a></td>+ <td>optional</td>+ <td><p>Token usage for this provider call, when available. </p></td>+ </tr>+</tbody></table>@@ -8419,6 +8430,30 @@ Frontend must echo this back unchanged when sending tool results. </p></td>+ <h3 id="bytebase.v1.AIChatUsage">AIChatUsage</h3>+ <p>Token usage for a single AI provider call.</p>+++ <table class="field-table">+ <thead>+ <tr><td>Field</td><td>Type</td><td>Label</td><td>Description</td></tr>+ </thead>+ <tbody>++ <tr>+ <td>total_tokens</td>+ <td><a href="#int32">int32</a></td>+ <td></td>+ <td><p>Total tokens used by the provider call. </p></td>+ </tr>++ </tbody>+ </table>+++++<h3 id="bytebase.v1.AIChatMessageRole">AIChatMessageRole</h3>diff --git a/proto/v1/v1/ai_service.proto b/proto/v1/v1/ai_service.protoindex c4c0a36e16..332c7bc47d 100644--- a/proto/v1/v1/ai_service.proto+++ b/proto/v1/v1/ai_service.proto@@ -86,6 +86,12 @@ message AIChatRequest {repeated AIChatToolDefinition tool_definitions = 2;}+// Token usage for a single AI provider call.+message AIChatUsage {+ // Total tokens used by the provider call.+ int32 total_tokens = 1;+}+// Response message for AIService.Chat.message AIChatResponse {// The text content of the AI response. Optional when the response only contains tool calls.@@ -93,4 +99,7 @@ message AIChatResponse {// Tool calls the AI wants to make.repeated AIChatToolCall tool_calls = 2;++ // Token usage for this provider call, when available.+ optional AIChatUsage usage = 3;}