Module: LlmLogs::Batch::TraceRecorder

Defined in:
app/models/llm_logs/batch/trace_recorder.rb

Overview

Records a completed trace + llm span for a reconciled batch request, mirroring what the synchronous chat.complete auto-instrumentation captures (model, provider, tokens, cost). Cost applies the 50% Batch API discount.

Constant Summary collapse

BATCH_COST_MULTIPLIER =
0.5

Class Method Summary collapse

Class Method Details

.compute_cost(message) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'app/models/llm_logs/batch/trace_recorder.rb', line 37

def compute_cost(message)
  model_info = RubyLLM.models.find(message.model_id)
  return nil unless model_info&.input_price_per_million && model_info&.output_price_per_million

  raw = (message.input_tokens.to_f * model_info.input_price_per_million +
         message.output_tokens.to_f * model_info.output_price_per_million) / 1_000_000
  (raw * BATCH_COST_MULTIPLIER).round(6)
rescue StandardError
  nil
end

.record(request:, message:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/llm_logs/batch/trace_recorder.rb', line 11

def record(request:, message:)
  trace = nil
   = request.routing.merge("execution_mode" => "batch")
  LlmLogs.trace(request.purpose, metadata: ) do |t|
    trace = t
    prompt_version_id = request.routing["prompt_version_id"]
    t.update_column(:prompt_version_id, prompt_version_id) if prompt_version_id

    span = LlmLogs::Tracer.start_span(
      name: "batch.complete",
      span_type: "llm",
      model: message.model_id || request.model,
      provider: LlmLogs.batch_provider.to_s,
      input: request.payload["input"]
    )
    span.update!(
      output: { "content" => span.serialize_content(message.content) },
      input_tokens: message.input_tokens,
      output_tokens: message.output_tokens,
      cost: compute_cost(message)
    )
    span.finish
  end
  trace
end