Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/req_llm/billing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ defmodule ReqLLM.Billing do
total: total_cost,
line_items: line_items,
input_cost: token_costs.input_cost,
output_cost: token_costs.output_cost
output_cost: token_costs.output_cost,
reasoning_cost: token_costs.reasoning_cost
}
end

Expand Down Expand Up @@ -216,12 +217,17 @@ defmodule ReqLLM.Billing do
|> Enum.filter(&token_input_item?/1)
|> Enum.reduce(0.0, fn item, acc -> Float.round(acc + item.cost, 6) end)

reasoning_cost =
line_items
|> Enum.filter(&token_reasoning_item?/1)
|> Enum.reduce(0.0, fn item, acc -> Float.round(acc + item.cost, 6) end)

output_cost =
line_items
|> Enum.filter(&token_output_item?/1)
|> Enum.reduce(0.0, fn item, acc -> Float.round(acc + item.cost, 6) end)

%{input_cost: input_cost, output_cost: output_cost}
%{input_cost: input_cost, output_cost: output_cost, reasoning_cost: reasoning_cost}
end

defp token_input_item?(%{id: id}) when is_binary(id) do
Expand All @@ -230,6 +236,12 @@ defmodule ReqLLM.Billing do

defp token_input_item?(_), do: false

defp token_reasoning_item?(%{id: id}) when is_binary(id) do
String.starts_with?(id, "token.reasoning")
end

defp token_reasoning_item?(_), do: false

defp token_output_item?(%{id: id}) when is_binary(id) do
String.starts_with?(id, "token.output") or String.starts_with?(id, "token.reasoning")
end
Expand Down
1 change: 1 addition & 0 deletions lib/req_llm/step/usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ defmodule ReqLLM.Step.Usage do
Map.merge(meta, %{
input_cost: cost_breakdown.input_cost,
output_cost: cost_breakdown.output_cost,
reasoning_cost: cost_breakdown.reasoning_cost,
total_cost: cost_breakdown.total_cost
})
else
Expand Down
2 changes: 2 additions & 0 deletions lib/req_llm/usage/cost.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ defmodule ReqLLM.Usage.Cost do
%{
input_cost: cost.input_cost,
output_cost: cost.output_cost,
reasoning_cost: cost.reasoning_cost,
total_cost: cost.total,
cost: cost
}}
Expand All @@ -57,6 +58,7 @@ defmodule ReqLLM.Usage.Cost do
|> Map.put(:cost, cost_breakdown.cost)
|> Map.put(:input_cost, cost_breakdown.input_cost)
|> Map.put(:output_cost, cost_breakdown.output_cost)
|> Map.put(:reasoning_cost, cost_breakdown.reasoning_cost)

if Keyword.get(opts, :preserve_total_cost, false) do
Map.put_new(usage, :total_cost, cost_breakdown.total_cost)
Expand Down
50 changes: 50 additions & 0 deletions test/req_llm/billing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,54 @@ defmodule ReqLLM.BillingTest do
assert cost.tokens == 0.00074
assert cost.total == 0.00074
end

test "calculates reasoning_cost separately when token.reasoning component exists" do
model = %LLMDB.Model{
provider: :test,
id: "m1",
pricing: %{
components: [
%{id: "token.input", kind: "token", per: 1_000_000, rate: 1.0},
%{id: "token.output", kind: "token", per: 1_000_000, rate: 2.0},
%{id: "token.reasoning", kind: "token", per: 1_000_000, rate: 3.0}
]
}
}

usage = %{
input_tokens: 1_000_000,
output_tokens: 500_000,
reasoning_tokens: 200_000
}

assert {:ok, cost} = Billing.calculate(usage, model)
assert cost.input_cost == 1.0
assert cost.output_cost == 1.6
assert cost.reasoning_cost == 0.6
assert cost.total == 2.6
end

test "reasoning_cost is zero when no reasoning tokens" do
model = %LLMDB.Model{
provider: :test,
id: "m1",
pricing: %{
components: [
%{id: "token.input", kind: "token", per: 1_000_000, rate: 1.0},
%{id: "token.output", kind: "token", per: 1_000_000, rate: 2.0}
]
}
}

usage = %{
input_tokens: 1_000_000,
output_tokens: 500_000
}

assert {:ok, cost} = Billing.calculate(usage, model)
assert cost.input_cost == 1.0
assert cost.output_cost == 1.0
assert cost.reasoning_cost == 0.0
assert cost.total == 2.0
end
end