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
Members list
Value members
Concrete methods
Record token usage.
Record token usage.
Value parameters
- inputTokens
-
Number of input/prompt tokens
- model
-
Model name
- outputTokens
-
Number of output/completion tokens
- provider
-
Provider name
Attributes
- Definition Classes
Per-model usage breakdown.
Per-model usage breakdown.
Note: thinkingTokens is always 0 because MetricsCollector.addTokens does not pass thinking token counts.
Attributes
Record an LLM request with its outcome and duration.
Record an LLM request with its outcome and duration.
Value parameters
- duration
-
Request duration
- model
-
Model name (e.g., "gpt-4o", "claude-3-5-sonnet-latest")
- outcome
-
Success or Error with error kind
- provider
-
Provider name (e.g., "openai", "anthropic", "ollama")
Attributes
- Definition Classes
Record estimated cost in USD.
Record estimated cost in USD.
Value parameters
- costUsd
-
Estimated cost in USD
- model
-
Model name
- provider
-
Provider name
Attributes
- Definition Classes
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
Human-readable summary string.
Human-readable summary string.
Attributes
Total estimated cost in USD across all models.
Total estimated cost in USD across all models.
Attributes
Total input/prompt tokens across all models.
Total input/prompt tokens across all models.
Attributes
Total output/completion tokens across all models.
Total output/completion tokens across all models.
Attributes
Total number of requests across all models.
Total number of requests across all models.
Attributes
Total tokens (input + output) across all models.
Total tokens (input + output) across all models.
Attributes
Inherited methods
Record circuit breaker state transition.
Record circuit breaker state transition.
Value parameters
- newState
-
New circuit breaker state ("open", "closed", "half-open")
- provider
-
Provider name
Attributes
- Inherited from:
- MetricsCollector
Record a generic error for metrics (when full request tracking not applicable).
Record a generic error for metrics (when full request tracking not applicable).
Value parameters
- errorKind
-
Type of error
- provider
-
Provider name
Attributes
- Inherited from:
- MetricsCollector
Record a retry attempt for reliability tracking.
Record a retry attempt for reliability tracking.
Value parameters
- attemptNumber
-
Which retry attempt (1 = first retry, 2 = second, etc.)
- provider
-
Provider name
Attributes
- Inherited from:
- MetricsCollector