Skip to main content

Class: CostTracker

Defined in: packages/orchestration/src/cost/cost-tracker.ts:40

Tracks cost per run, per agent and per ticket.

Runs are priced step by step (CostTracker.priceStep) from each step_finished event's token classes, at core's MODEL_RATES plus customPricing. A model with no rates is reported once and priced as undefined: nothing is booked for it, because a made-up rate would be spend that never happened. When no step reported usage, the done event's token counts are priced instead, at defaultModel's rates (the blended CostTracker.estimateCost rate when they are only a total).

Constructors​

Constructor​

new CostTracker(config?): CostTracker

Defined in: packages/orchestration/src/cost/cost-tracker.ts:49

Parameters​

config?​

CostTrackerConfig

Returns​

CostTracker

Methods​

estimateCost()​

estimateCost(tokensUsed, model?): number | undefined

Defined in: packages/orchestration/src/cost/cost-tracker.ts:68

Estimate cost from a token total at the model's blended rate (the average of its input and output rates), or undefined for a model with no rates.

Parameters​

tokensUsed​

number

model?​

string

Returns​

number | undefined


getAgentSpend()​

getAgentSpend(agentName): number

Defined in: packages/orchestration/src/cost/cost-tracker.ts:131

Parameters​

agentName​

string

Returns​

number


getRunCost()​

getRunCost(runId): number

Defined in: packages/orchestration/src/cost/cost-tracker.ts:120

Parameters​

runId​

string

Returns​

number


getTicketSpend()​

getTicketSpend(ticketId): number

Defined in: packages/orchestration/src/cost/cost-tracker.ts:135

Parameters​

ticketId​

string

Returns​

number


getTotalSpend()​

getTotalSpend(): number

Defined in: packages/orchestration/src/cost/cost-tracker.ts:125

What every run this tracker recorded cost, in this process.

Returns​

number


load()​

load(agentName, ticketId?, need?): Promise<void>

Defined in: packages/orchestration/src/cost/cost-tracker.ts:108

Refresh an agent's and a ticket's spend from the spend store (a no-op without one), so budget checks see what every instance spent. need says which totals a budget actually reads — an agent with no budgetTotal has no use for its lifetime spend, so the store is not asked for it. Both are read by default.

Parameters​

agentName​

string

ticketId?​

string

need?​
agent?​

boolean

ticket?​

boolean

Returns​

Promise<void>


priceStep()​

priceStep(step): number | undefined

Defined in: packages/orchestration/src/cost/cost-tracker.ts:148

USD for one model step (a step_finished event): the provider-reported cost when there is one, else the model's own rates over four token classes, no token counted twice — fresh input, cache writes, cache reads, output (inputTokens includes both cache counts, as AI SDK reports them; core's tokenPricing) — else the blended estimate when the step reported only a total. undefined when the model has no rates. Model ids may carry a provider/ or provider: prefix.

Parameters​

step​
cost?​

number

modelId?​

string

usage​

{ cacheReadTokens?: number; cacheWriteTokens?: number; inputTokens?: number; outputTokens?: number; totalTokens?: number; }

usage.cacheReadTokens?​

number

usage.cacheWriteTokens?​

number

usage.inputTokens?​

number

usage.outputTokens?​

number

usage.totalTokens?​

number

Returns​

number | undefined


recordRun()​

recordRun(runId, agentName, ticketId, costUsd): Promise<void>

Defined in: packages/orchestration/src/cost/cost-tracker.ts:96

Record a finished run's cost: in memory, and in the spend store when there is one (awaited — a lost write would let the next run start over budget).

Parameters​

runId​

string

agentName​

string

ticketId​

string

costUsd​

number

Returns​

Promise<void>


recordRunCost()​

recordRunCost(runId, agentName, ticketId, costUsd): void

Defined in: packages/orchestration/src/cost/cost-tracker.ts:86

Add to a run's cost in this process's memory only (see recordRun).

Parameters​

runId​

string

agentName​

string

ticketId​

string

costUsd​

number

Returns​

void