[Feature] Add Anthropic and Google (Gemini) response format support to requestcostmetadata extractor#234
Conversation
d2259a9 to
1fbae59
Compare
1fbae59 to
4aae3db
Compare
4aae3db to
8491ce2
Compare
davidbreitgand
left a comment
There was a problem hiding this comment.
@abdallahsamabd : Nice! Thanks for this PR.
| } | ||
|
|
||
| func extractOpenAI(body map[string]any) (prompt, completion float64, ok bool) { | ||
| usage := body["usage"].(map[string]any) |
There was a problem hiding this comment.
Nit: potentially confusing usage of unchecked type assertion
| usage := body["usage"].(map[string]any) | |
| // Safe: detectFormat() verified "usage" key exists and is map[string]any | |
| usage := body["usage"].(map[string]any) |
There was a problem hiding this comment.
@abdallahsamabd , actually, thinking more about this... I'm not sure why you need this type assertion at all. detectFormat() already validated the structure, the extract functions seem to re-validate the exact same structure. No mutation occurs between detectFormat() and extractOpenAI()/extractAnthropic()/extractGoogle() calls. Why do we need this additional assertion?
The same question for Lines 290 and 303.
| } | ||
|
|
||
| func extractAnthropic(body map[string]any) (prompt, completion float64, ok bool) { | ||
| usage := body["usage"].(map[string]any) |
| } | ||
|
|
||
| func extractGoogle(body map[string]any) (prompt, completion float64, ok bool) { | ||
| usageMetadata := body["usageMetadata"].(map[string]any) |
There was a problem hiding this comment.
I refactored to inline the detection and pass the validated sub-map directly to each extractor, eliminating both the redundant assertions and the separate detectFormat function
…metadata extractor The extractTokenCounts function now auto-detects the response format by inspecting the response body structure and dispatches to format-specific extraction helpers for OpenAI, Anthropic, and Google/Gemini. This enables CostGuard to observe actual inference costs from all major LLM providers without any user configuration. Fixes llm-d#230 Signed-off-by: Abdallah Samara <abdallahsamabd@gmail.com>
8491ce2 to
e229a5b
Compare
|
/lgtm |
What type of PR is this?
/kind feature
/kind test
What this PR does / why we need it:
The
requestcostmetadataextractor currently only parses OpenAI-formatted responses (usage.prompt_tokens/usage.completion_tokens). This PR adds auto-detection and extraction support for Anthropic (usage.input_tokens/usage.output_tokens) and Google/Gemini (usageMetadata.promptTokenCount/usageMetadata.candidatesTokenCount) response formats.The extractor inspects the response body structure to determine the format — no user configuration needed. This ensures CostGuard can observe actual inference costs from all major LLM providers.
Which issue(s) this PR fixes:
Fixes #230
Release note (write
NONEif no user-facing change):