org.llm4s.metrics.MetricsCollector
See theMetricsCollector companion object
Minimal algebra for collecting metrics about LLM operations.
Implementations should be safe: failures must not propagate to callers. All methods should catch and log errors internally without throwing.
Example usage:
val startNanos = System.nanoTime()
client.complete(conversation) match {
case Right(completion) =>
val duration = FiniteDuration(System.nanoTime() - startNanos, NANOSECONDS)
metrics.observeRequest(provider, model, Outcome.Success, duration)
completion.usage.foreach { u =>
metrics.addTokens(provider, model, u.promptTokens, u.completionTokens)
}
case Left(error) =>
val duration = FiniteDuration(System.nanoTime() - startNanos, NANOSECONDS)
val errorKind = ErrorKind.fromLLMError(error)
metrics.observeRequest(provider, model, Outcome.Error(errorKind), duration)
}
Attributes
-
Companion
-
object
-
Graph
-
-
Supertypes
-
class Object
trait Matchable
class Any
-
Known subtypes
-
Members list
Value parameters
-
inputTokens
-
Number of input/prompt tokens
-
model
-
Model name
-
outputTokens
-
Number of output/completion tokens
-
provider
-
Provider name
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
Record estimated cost in USD.
Record estimated cost in USD.
Value parameters
-
costUsd
-
Estimated cost in USD
-
model
-
Model name
-
provider
-
Provider name
Attributes
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
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
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