Class: LlmCostTracker::Parsers::Anthropic
- Defined in:
- lib/llm_cost_tracker/parsers/anthropic.rb
Constant Summary collapse
- HOSTS =
%w[api.anthropic.com].freeze
Instance Method Summary collapse
- #match?(url) ⇒ Boolean
- #parse(_request_url, request_body, response_status, response_body) ⇒ Object
- #parse_stream(_request_url, request_body, response_status, events) ⇒ Object
- #provider_names ⇒ Object
Methods inherited from Base
Instance Method Details
#match?(url) ⇒ Boolean
10 11 12 |
# File 'lib/llm_cost_tracker/parsers/anthropic.rb', line 10 def match?(url) match_uri?(url, hosts: HOSTS, path_includes: "/v1/messages") end |
#parse(_request_url, request_body, response_status, response_body) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/llm_cost_tracker/parsers/anthropic.rb', line 18 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) cache_read = usage["cache_read_input_tokens"].to_i UsageCapture.build( provider: "anthropic", provider_response_id: response["id"], pricing_mode: pricing_mode(request, response, usage), model: response["model"] || request["model"], token_usage: token_usage(usage, cache_read), usage_source: :response ) end |
#parse_stream(_request_url, request_body, response_status, events) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/llm_cost_tracker/parsers/anthropic.rb', line 38 def parse_stream(_request_url, request_body, response_status, events) return nil unless response_status == 200 request = safe_json_parse(request_body) model = find_event_value(events) { |data| data.dig("message", "model") } || request["model"] usage = stream_usage(events) response_id = find_event_value(events) { |data| data.dig("message", "id") || data["id"] } if usage build_stream_result(model, usage, response_id, pricing_mode(request, nil, usage)) else build_unknown_stream_usage( provider: "anthropic", model: model, provider_response_id: response_id, pricing_mode: pricing_mode(request, nil, usage) ) end end |
#provider_names ⇒ Object
14 15 16 |
# File 'lib/llm_cost_tracker/parsers/anthropic.rb', line 14 def provider_names %w[anthropic] end |