CostTracker

org.llm4s.metrics.CostTracker
See theCostTracker companion object
final class CostTracker extends MetricsCollector

In-process cost and usage tracker that implements MetricsCollector.

Accumulates per-model request counts, token usage, and cost data in memory with lock-free thread safety via AtomicReference + CAS. Provides query methods for retrieving aggregated statistics without external infrastructure.

Cost is stored as BigDecimal to prevent floating-point accumulation drift, matching the pattern used by UsageSummary and ModelUsage.

Example usage:

val tracker = CostTracker.create()
val client = LLMConnect.getClient(config, tracker)
// ... make LLM calls ...
println(s"Total cost: $$${tracker.totalCost}")
println(tracker.summary)

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

override def addTokens(provider: String, model: String, inputTokens: Long, outputTokens: Long): Unit

Record token usage.

Record token usage.

Implementations typically record input and output tokens separately so dashboards can show prompt and completion usage independently.

Value parameters

inputTokens

Number of input/prompt tokens

model

Model name

outputTokens

Number of output/completion tokens

provider

Provider name

Attributes

Definition Classes
def byModel: Map[String, ModelUsage]

Per-model usage breakdown.

Per-model usage breakdown.

Note: thinkingTokens is always 0 because MetricsCollector.addTokens does not pass thinking token counts.

Attributes

override def observeRequest(provider: String, model: String, outcome: Outcome, duration: FiniteDuration): Unit

Record an LLM request with its outcome and duration.

Record an LLM request with its outcome and duration.

The outcome dimension separates successful requests from failures. Error outcomes carry a stable ErrorKind such as ErrorKind.RateLimit, ErrorKind.Timeout, or ErrorKind.ServiceError so metrics backends can aggregate failures without depending on exception class names.

Value parameters

duration

Request duration

model

Model name (e.g., "gpt-4o", "claude-3-5-sonnet-latest")

outcome

Success or Error with stable error kind

provider

Provider name (e.g., "openai", "anthropic", "ollama")

Attributes

Definition Classes
override def recordCost(provider: String, model: String, costUsd: Double): Unit

Record estimated cost in USD.

Record estimated cost in USD.

Use this after pricing metadata is available for a request or image generation operation. Implementations should treat the value as an additive counter.

Value parameters

costUsd

Estimated cost in USD

model

Model name

provider

Provider name

Attributes

Definition Classes
def reset(): Unit

Reset all accumulated data.

Reset all accumulated data.

Attributes

Returns a UsageSummary for interop with agent-level tracking.

Returns a UsageSummary for interop with agent-level tracking.

Attributes

def summary: String

Human-readable summary string.

Human-readable summary string.

Attributes

def totalCost: BigDecimal

Total estimated cost in USD across all models.

Total estimated cost in USD across all models.

Attributes

def totalInputTokens: Long

Total input/prompt tokens across all models.

Total input/prompt tokens across all models.

Attributes

def totalOutputTokens: Long

Total output/completion tokens across all models.

Total output/completion tokens across all models.

Attributes

def totalRequests: Long

Total number of requests across all models.

Total number of requests across all models.

Attributes

def totalTokens: Long

Total tokens (input + output) across all models.

Total tokens (input + output) across all models.

Attributes

Inherited methods

def observeImageGeneration(provider: String, model: String, operation: String, outcome: Outcome, duration: FiniteDuration, imageCount: Int): Unit

Record an image generation operation.

Record an image generation operation.

The outcome dimension has the same meaning as in observeRequest. imageCount records the number of generated images only for successful operations.

Value parameters

duration

Request duration

imageCount

Number of images generated

model

Model name (e.g., "gpt-image-1", "dall-e-3")

operation

Operation type: "generate" or "edit"

outcome

Success or Error with stable error kind

provider

Provider name (e.g., "openai", "stability-ai")

Attributes

Inherited from:
MetricsCollector
def recordCircuitBreakerTransition(provider: String, newState: String): Unit

Record circuit breaker state transition.

Record circuit breaker state transition.

Expected states are stable labels such as "open", "closed", and "half-open".

Value parameters

newState

New circuit breaker state ("open", "closed", "half-open")

provider

Provider name

Attributes

Inherited from:
MetricsCollector
def recordError(errorKind: ErrorKind, provider: String): Unit

Record a generic error for metrics (when full request tracking not applicable).

Record a generic error for metrics (when full request tracking not applicable).

Use this for failures that occur outside a complete request timing scope, such as configuration, validation, or setup failures.

Value parameters

errorKind

Type of error

provider

Provider name

Attributes

Inherited from:
MetricsCollector
def recordImageGenerationCost(provider: String, model: String, costUsd: Double, imageCount: Int): Unit

Record estimated image generation cost in USD.

Record estimated image generation cost in USD.

Value parameters

costUsd

Estimated cost in USD

imageCount

Number of images

model

Model name

provider

Provider name

Attributes

Inherited from:
MetricsCollector
def recordRetryAttempt(provider: String, attemptNumber: Int): Unit

Record a retry attempt for reliability tracking.

Record a retry attempt for reliability tracking.

The first retry after the original request fails should use attemptNumber = 1.

Value parameters

attemptNumber

Which retry attempt (1 = first retry, 2 = second, etc.)

provider

Provider name

Attributes

Inherited from:
MetricsCollector