ConsoleTracing

org.llm4s.trace.ConsoleTracing
@Stable
class ConsoleTracing extends Tracing

Console-based Tracing implementation with colored, formatted output.

Prints trace events to standard output with ANSI color formatting for improved readability during development and debugging. Returns Result[Unit] to support functional composition.

== Features ==

  • Color-coded output by event type (errors in red, success in green, etc.)
  • Visual separators and headers for different trace sections
  • Formatted display of all TraceEvent types
  • Truncation of long JSON content for readability
  • Timestamps on all events

== Usage ==

val tracing: Tracing = new ConsoleTracing()

// Trace events functionally
for {
 _ <- tracing.traceEvent(TraceEvent.AgentInitialized("query", Vector("tool1")))
 _ <- tracing.traceTokenUsage(TokenUsage(100, 50, 150), "gpt-4", "completion")
} yield ()

Attributes

See also

NoOpTracing for silent tracing

LangfuseTracing in llm4s-observability for production observability

AnsiColors for color constants used

Graph
Supertypes
trait Tracing
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def traceCompletion(completion: Completion, model: String): Result[Unit]
def traceError(error: Throwable, context: String): Result[Unit]
def traceEvent(event: TraceEvent): Result[Unit]
def traceTokenUsage(usage: TokenUsage, model: String, operation: String): Result[Unit]
def traceToolCall(toolName: String, input: String, output: String): Result[Unit]

Inherited methods

def shutdown(): Unit

Shutdown the tracing backend. Alias for close() to maintain terminology consistency.

Shutdown the tracing backend. Alias for close() to maintain terminology consistency.

Attributes

Inherited from:
Tracing
final def traceCost(costUsd: Double, model: String, operation: String, tokenCount: Int, costType: String): Result[Unit]

Trace cost in USD for any operation.

Trace cost in USD for any operation.

Value parameters

costType

Category: "embedding", "completion", "total"

costUsd

Cost in US dollars

model

Model name

operation

Type: "embedding", "completion", "evaluation"

tokenCount

Total tokens used

Attributes

Inherited from:
Tracing
final def traceEmbeddingUsage(usage: EmbeddingUsage, model: String, operation: String, inputCount: Int): Result[Unit]

Trace embedding token usage for cost tracking.

Trace embedding token usage for cost tracking.

Value parameters

inputCount

Number of texts embedded

model

Embedding model name

operation

Type: "indexing", "query", "evaluation"

usage

Token usage from embedding operation

Attributes

Inherited from:
Tracing
final def traceEvent(event: String): Result[Unit]

Emits a named CustomEvent with no additional data payload.

Emits a named CustomEvent with no additional data payload.

Wraps the string in a TraceEvent.CustomEvent with an empty JSON object as its data field before delegating to traceEvent(TraceEvent). Use the typed overload for events that carry structured data.

Value parameters

event

Human-readable event name forwarded as CustomEvent.name

Attributes

Inherited from:
Tracing
final def traceRAGOperation(operation: String, duration: FiniteDuration, embeddingTokens: Option[Int], llmPromptTokens: Option[Int], llmCompletionTokens: Option[Int], totalCostUsd: Option[Double]): Result[Unit]

Trace completion of a RAG operation with metrics.

Trace completion of a RAG operation with metrics.

Value parameters

duration

Wall-clock duration

embeddingTokens

Optional embedding token count

llmCompletionTokens

Optional LLM completion tokens

llmPromptTokens

Optional LLM prompt tokens

operation

Type: "index", "search", "answer", "evaluate"

totalCostUsd

Optional total cost in USD

Attributes

Inherited from:
Tracing