improvement(usage): update usage limit in realtime, standardize token output object across providers (#2553)

* improvement(usage-limit): update usage in real time, fix token output object

* updated tokenBreakdown to tokens, standardized input/output/total token object type across providers

* update remaining references

* ack PR comment

* remove singleton query client instance from hooks, leave only in zustand
This commit is contained in:
Waleed
2025-12-23 13:04:47 -08:00
committed by GitHub
parent 641ac58017
commit 4e09c389e8
51 changed files with 368 additions and 388 deletions

View File

@@ -59,8 +59,12 @@ export class ExecutionLogger implements IExecutionLoggerService {
output: (merged[model].output || 0) + (costs.output || 0),
total: (merged[model].total || 0) + (costs.total || 0),
tokens: {
prompt: (merged[model].tokens?.prompt || 0) + (costs.tokens?.prompt || 0),
completion: (merged[model].tokens?.completion || 0) + (costs.tokens?.completion || 0),
input:
(merged[model].tokens?.input || merged[model].tokens?.prompt || 0) +
(costs.tokens?.input || costs.tokens?.prompt || 0),
output:
(merged[model].tokens?.output || merged[model].tokens?.completion || 0) +
(costs.tokens?.output || costs.tokens?.completion || 0),
total: (merged[model].tokens?.total || 0) + (costs.tokens?.total || 0),
},
}
@@ -195,7 +199,7 @@ export class ExecutionLogger implements IExecutionLoggerService {
input: number
output: number
total: number
tokens: { prompt: number; completion: number; total: number }
tokens: { input: number; output: number; total: number }
}
>
}
@@ -269,8 +273,12 @@ export class ExecutionLogger implements IExecutionLoggerService {
input: (existingCost.input || 0) + costSummary.totalInputCost,
output: (existingCost.output || 0) + costSummary.totalOutputCost,
tokens: {
prompt: (existingCost.tokens?.prompt || 0) + costSummary.totalPromptTokens,
completion: (existingCost.tokens?.completion || 0) + costSummary.totalCompletionTokens,
input:
(existingCost.tokens?.input || existingCost.tokens?.prompt || 0) +
costSummary.totalPromptTokens,
output:
(existingCost.tokens?.output || existingCost.tokens?.completion || 0) +
costSummary.totalCompletionTokens,
total: (existingCost.tokens?.total || 0) + costSummary.totalTokens,
},
models: this.mergeCostModels(existingCost.models || {}, costSummary.models),
@@ -280,8 +288,8 @@ export class ExecutionLogger implements IExecutionLoggerService {
input: costSummary.totalInputCost,
output: costSummary.totalOutputCost,
tokens: {
prompt: costSummary.totalPromptTokens,
completion: costSummary.totalCompletionTokens,
input: costSummary.totalPromptTokens,
output: costSummary.totalCompletionTokens,
total: costSummary.totalTokens,
},
models: costSummary.models,
@@ -307,9 +315,9 @@ export class ExecutionLogger implements IExecutionLoggerService {
executionData: {
traceSpans: redactedTraceSpans,
finalOutput: redactedFinalOutput,
tokenBreakdown: {
prompt: mergedCost.tokens.prompt,
completion: mergedCost.tokens.completion,
tokens: {
input: mergedCost.tokens.input,
output: mergedCost.tokens.output,
total: mergedCost.tokens.total,
},
models: mergedCost.models,
@@ -508,7 +516,7 @@ export class ExecutionLogger implements IExecutionLoggerService {
input: number
output: number
total: number
tokens: { prompt: number; completion: number; total: number }
tokens: { input: number; output: number; total: number }
}
>
},

View File

@@ -83,7 +83,7 @@ export function calculateCostSummary(traceSpans: any[]): {
input: number
output: number
total: number
tokens: { prompt: number; completion: number; total: number }
tokens: { input: number; output: number; total: number }
}
>
} {
@@ -131,7 +131,7 @@ export function calculateCostSummary(traceSpans: any[]): {
input: number
output: number
total: number
tokens: { prompt: number; completion: number; total: number }
tokens: { input: number; output: number; total: number }
}
> = {}
@@ -150,14 +150,14 @@ export function calculateCostSummary(traceSpans: any[]): {
input: 0,
output: 0,
total: 0,
tokens: { prompt: 0, completion: 0, total: 0 },
tokens: { input: 0, output: 0, total: 0 },
}
}
models[model].input += span.cost.input || 0
models[model].output += span.cost.output || 0
models[model].total += span.cost.total || 0
models[model].tokens.prompt += span.tokens?.input ?? span.tokens?.prompt ?? 0
models[model].tokens.completion += span.tokens?.output ?? span.tokens?.completion ?? 0
models[model].tokens.input += span.tokens?.input ?? span.tokens?.prompt ?? 0
models[model].tokens.output += span.tokens?.output ?? span.tokens?.completion ?? 0
models[model].tokens.total += span.tokens?.total || 0
}
}

View File

@@ -23,7 +23,7 @@ describe('buildTraceSpans', () => {
output: {
content: 'Agent response',
model: 'gpt-4o',
tokens: { prompt: 10, completion: 20, total: 30 },
tokens: { input: 10, output: 20, total: 30 },
providerTiming: {
duration: 8000,
startTime: '2024-01-01T10:00:00.000Z',
@@ -138,7 +138,7 @@ describe('buildTraceSpans', () => {
output: {
content: 'Agent response',
model: 'gpt-4o',
tokens: { prompt: 10, completion: 20, total: 30 },
tokens: { input: 10, output: 20, total: 30 },
providerTiming: {
duration: 4000,
startTime: '2024-01-01T10:00:00.500Z',
@@ -427,8 +427,8 @@ describe('buildTraceSpans', () => {
output: {
content: 'Based on my research using multiple sources...',
model: 'gpt-4o',
tokens: { prompt: 50, completion: 200, total: 250 },
cost: { total: 0.0025, prompt: 0.001, completion: 0.0015 },
tokens: { input: 50, output: 200, total: 250 },
cost: { total: 0.0025, input: 0.001, output: 0.0015 },
providerTiming: {
duration: 15000,
startTime: '2024-01-01T10:00:00.000Z',

View File

@@ -15,8 +15,8 @@ export interface PricingInfo {
}
export interface TokenUsage {
prompt: number
completion: number
input: number
output: number
total: number
}
@@ -102,6 +102,17 @@ export interface WorkflowExecutionLog {
environment?: ExecutionEnvironment
trigger?: ExecutionTrigger
traceSpans?: TraceSpan[]
tokens?: { input?: number; output?: number; total?: number }
models?: Record<
string,
{
input?: number
output?: number
total?: number
tokens?: { input?: number; output?: number; total?: number }
}
>
finalOutput?: any
errorDetails?: {
blockId: string
blockName: string
@@ -114,14 +125,14 @@ export interface WorkflowExecutionLog {
input?: number
output?: number
total?: number
tokens?: { prompt?: number; completion?: number; total?: number }
tokens?: { input?: number; output?: number; total?: number }
models?: Record<
string,
{
input?: number
output?: number
total?: number
tokens?: { prompt?: number; completion?: number; total?: number }
tokens?: { input?: number; output?: number; total?: number }
}
>
}