Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
Updates token cost to account for input/output costs
elijahbenizzy8feb94ce4ca4
diff --git a/telemetry/ui/src/components/routes/app/InsightsView.tsx b/telemetry/ui/src/components/routes/app/InsightsView.tsxindex 739fd628..5aa0d23b 100644--- a/telemetry/ui/src/components/routes/app/InsightsView.tsx+++ b/telemetry/ui/src/components/routes/app/InsightsView.tsx@@ -51,9 +51,9 @@ type InsightWithoutIndividualValues = InsightBase & {type Insight = InsightWithIndividualValues | InsightWithoutIndividualValues;// eslint-disable-next-line @typescript-eslint/no-unused-vars-const getCostPerTokenModelCents = (model: string): number => {+const getCostPerTokenModelCents = (model: string) => {// TODO -- unhardcode this!- return 500 / 1_000_000;+ return { promptCost: 500 / 1_000_000, completionCost: 500 / 1_000_000 };};type CostProps = {@@ -93,20 +93,33 @@ const gatherCostAttributes = (allAttributes: AttributeModel[]): AttributeModel[]}, new Map<string, AttributeModel>());const tokensUsedBySpanID = allAttributes.reduce((acc, attribute) => {- if (attribute.key === 'llm.usage.total_tokens') {- acc.set(- attribute.span_id || '',- (acc.get(attribute.span_id || '') || 0) + (attribute.value as number)- );+ if (attribute.key === 'gen_ai.usage.completion_tokens') {+ const currentValue = acc.get(attribute.span_id || '') || { prompt: 0, completion: 0 };+ acc.set(attribute.span_id || '', {+ prompt: currentValue.prompt + (attribute.value as number),+ completion: currentValue.completion+ });+ }+ if (attribute.key === 'gen_ai.usage.prompt_tokens') {+ const currentValue = acc.get(attribute.span_id || '') || { prompt: 0, completion: 0 };+ acc.set(attribute.span_id || '', {+ prompt: currentValue.prompt,+ completion: currentValue.completion + (attribute.value as number)+ });}return acc;- }, new Map<string, number>());+ }, new Map<string, { prompt: number; completion: number }>());const totalCostBySpanID = new Map<string, number>();tokensUsedBySpanID.forEach((tokensUsed, spanID) => {const modelAttribute = modelAttributesBySpanID.get(spanID);if (modelAttribute) {- const costPerToken = getCostPerTokenModelCents(modelAttribute.value as string);- totalCostBySpanID.set(spanID, tokensUsed * costPerToken);+ const { promptCost, completionCost } = getCostPerTokenModelCents(+ modelAttribute.value as string+ );+ totalCostBySpanID.set(+ spanID,+ tokensUsed.prompt * promptCost + tokensUsed.completion * completionCost+ );}});return Array.from(totalCostBySpanID.entries()).map(([spanID, cost]) => {