19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/llm_cost_tracker/parsers/anthropic.rb', line 19
def parse(_request_url, request_body, response_status, response_body)
return nil unless response_status == 200
response = safe_json_parse(response_body)
usage = response["usage"]
return nil unless usage
request = safe_json_parse(request_body)
ParsedUsage.build(
provider: "anthropic",
model: response["model"] || request["model"],
input_tokens: usage["input_tokens"].to_i,
output_tokens: usage["output_tokens"].to_i,
total_tokens: usage["input_tokens"].to_i + usage["output_tokens"].to_i +
usage["cache_read_input_tokens"].to_i + usage["cache_creation_input_tokens"].to_i,
cache_read_input_tokens: usage["cache_read_input_tokens"],
cache_creation_input_tokens: usage["cache_creation_input_tokens"]
)
end
|