Skip to content

Commit 90535f1

Browse files
author
Theodore Li
committed
Fix tool usage billing for streamed outputs
1 parent 2584bb8 commit 90535f1

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

apps/sim/app/workspace/[workspaceId]/logs/components/log-details/log-details.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,24 @@ export const LogDetails = memo(function LogDetails({
598598
{formatCost(log.cost?.output || 0)}
599599
</span>
600600
</div>
601+
{(() => {
602+
const models = (log.cost as Record<string, unknown>)?.models as
603+
| Record<string, { toolCost?: number }>
604+
| undefined
605+
const totalToolCost = models
606+
? Object.values(models).reduce((sum, m) => sum + (m?.toolCost || 0), 0)
607+
: 0
608+
return totalToolCost > 0 ? (
609+
<div className='flex items-center justify-between'>
610+
<span className='font-medium text-[var(--text-tertiary)] text-caption'>
611+
Tool Usage:
612+
</span>
613+
<span className='font-medium text-[var(--text-secondary)] text-caption'>
614+
{formatCost(totalToolCost)}
615+
</span>
616+
</div>
617+
) : null
618+
})()}
601619
</div>
602620

603621
<div className='border-[var(--border)] border-t' />
@@ -626,7 +644,7 @@ export const LogDetails = memo(function LogDetails({
626644
<div className='flex items-center justify-center rounded-md bg-[var(--surface-2)] p-2 text-center'>
627645
<p className='font-medium text-[var(--text-subtle)] text-xs'>
628646
Total cost includes a base execution charge of{' '}
629-
{formatCost(BASE_EXECUTION_CHARGE)} plus any model usage costs.
647+
{formatCost(BASE_EXECUTION_CHARGE)} plus any model and tool usage costs.
630648
</p>
631649
</div>
632650
</div>

apps/sim/providers/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ const ZERO_COST = Object.freeze({
6262
})
6363

6464
/**
65-
* Prevents streaming callbacks from writing non-zero cost for BYOK users.
66-
* The property is frozen via defineProperty because providers set cost inside
67-
* streaming callbacks that fire after this function returns.
65+
* Prevents streaming callbacks from writing non-zero model cost for BYOK users
66+
* while preserving tool costs. The property is frozen via defineProperty because
67+
* providers set cost inside streaming callbacks that fire after this function returns.
6868
*/
6969
function zeroCostForBYOK(response: StreamingExecution): void {
7070
const output = response.execution?.output
@@ -73,9 +73,14 @@ function zeroCostForBYOK(response: StreamingExecution): void {
7373
return
7474
}
7575

76+
let toolCost = 0
7677
Object.defineProperty(output, 'cost', {
77-
get: () => ZERO_COST,
78-
set: () => {},
78+
get: () => (toolCost > 0 ? { ...ZERO_COST, toolCost, total: toolCost } : ZERO_COST),
79+
set: (value: Record<string, unknown>) => {
80+
if (value?.toolCost && typeof value.toolCost === 'number') {
81+
toolCost = value.toolCost
82+
}
83+
},
7984
configurable: true,
8085
enumerable: true,
8186
})

0 commit comments

Comments
 (0)