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 37 38 39 40 |
# 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 cache_write = usage["cache_creation_input_tokens"].to_i ParsedUsage.build( provider: "anthropic", provider_response_id: response["id"], 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 + cache_read + cache_write, cache_read_input_tokens: usage["cache_read_input_tokens"], cache_write_input_tokens: usage["cache_creation_input_tokens"], usage_source: :response ) end |
#parse_stream(_request_url, request_body, response_status, events) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/llm_cost_tracker/parsers/anthropic.rb', line 42 def parse_stream(_request_url, request_body, response_status, events) return nil unless response_status == 200 request = safe_json_parse(request_body) model = stream_model(events) || request["model"] usage = stream_usage(events) response_id = stream_response_id(events) if usage build_stream_result(model, usage, response_id) else build_unknown_stream_usage( provider: "anthropic", model: model, provider_response_id: response_id ) end end |
#provider_names ⇒ Object
14 15 16 |
# File 'lib/llm_cost_tracker/parsers/anthropic.rb', line 14 def provider_names %w[anthropic] end |