12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/legion/llm/pipeline/steps/billing.rb', line 12
def step_billing
return unless @request.billing
billing = @request.billing
cap = billing[:spending_cap]
estimated_cost = cap ? estimate_request_cost : nil
if cap && estimated_cost > cap
log.error(
"[llm][billing] cap_exceeded request_id=#{@request.id} " \
"budget_id=#{billing[:budget_id]} estimated_cost=#{estimated_cost.round(6)} cap=#{cap}"
)
raise Legion::LLM::PipelineError.new(
"budget_exceeded: estimated cost #{estimated_cost.round(6)} exceeds cap #{cap}",
step: :billing
)
end
log.info(
"[llm][billing] checked request_id=#{@request.id} budget_id=#{billing[:budget_id]} " \
"estimated_cost=#{estimated_cost || 0} cap=#{cap || 'none'}"
)
@enrichments['billing:budget_check'] = {
budget_id: billing[:budget_id],
cost_center: billing[:cost_center],
spending_cap: cap,
estimated_cost_usd: estimated_cost,
timestamp: Time.now
}.compact
@audit[:'billing:budget_check'] = {
outcome: :success,
detail: "budget_id=#{billing[:budget_id]}, cap=#{cap}, estimated=#{estimated_cost}",
duration_ms: 0,
timestamp: Time.now
}
@timeline.record(
category: :audit, key: 'billing:budget_check',
direction: :internal, detail: "outcome=success, budget_id=#{billing[:budget_id]}",
from: 'pipeline', to: 'billing'
)
end
|