Capture optional provider reported costs
This commit is contained in:
@@ -105,6 +105,7 @@ type AttemptOutcome struct {
|
||||
StatusCode int
|
||||
Usage domain.TokenUsage
|
||||
UsagePresence *domain.TokenUsagePresence
|
||||
CostUSD *float64
|
||||
RetryNotBefore time.Time
|
||||
Evidence HTTPExecutionEvidence
|
||||
StructuredOutputRequested *bool
|
||||
@@ -299,6 +300,9 @@ func (p *PreparedGeneration) execute(ctx context.Context, captureEvidence bool)
|
||||
TotalTokens: wireResp.Usage.TotalTokens != nil, CachedTokens: wireResp.Usage.PromptTokensDetails.CachedTokens != nil,
|
||||
CacheWriteTokens: writes != nil, ReasoningTokens: reasoning != nil,
|
||||
}
|
||||
if p.provider == "openrouter" {
|
||||
outcome.CostUSD = optionalFloat(wireResp.Usage.Cost)
|
||||
}
|
||||
|
||||
if len(wireResp.Choices) == 0 {
|
||||
outcome.Err = fmt.Errorf("%w: no choices returned", ErrMalformedResponse)
|
||||
@@ -319,7 +323,7 @@ func (p *PreparedGeneration) execute(ctx context.Context, captureEvidence bool)
|
||||
}
|
||||
return outcome
|
||||
}
|
||||
outcome.Response = &domain.GenerateResponse{Content: *content, Usage: outcome.Usage, UsagePresence: outcome.UsagePresence}
|
||||
outcome.Response = &domain.GenerateResponse{Content: *content, Usage: outcome.Usage, UsagePresence: outcome.UsagePresence, CostUSD: outcome.CostUSD}
|
||||
outcome.GenerationCompleted = true
|
||||
if captureEvidence {
|
||||
if contextErr := observedContextError(requestContext, ctx, nil); contextErr != nil {
|
||||
@@ -568,6 +572,7 @@ type openAIChatResponse struct {
|
||||
} `json:"prompt_tokens_details"`
|
||||
CompletionTokensDetails json.RawMessage `json:"completion_tokens_details"`
|
||||
CacheWriteTokens json.RawMessage `json:"cache_write_tokens"`
|
||||
Cost json.RawMessage `json:"cost"`
|
||||
} `json:"usage"`
|
||||
}
|
||||
|
||||
@@ -582,6 +587,17 @@ func optionalInteger(raw json.RawMessage) *int {
|
||||
return value
|
||||
}
|
||||
|
||||
func optionalFloat(raw json.RawMessage) *float64 {
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
var value *float64
|
||||
if json.Unmarshal(raw, &value) != nil || (value != nil && (math.IsNaN(*value) || math.IsInf(*value, 0))) {
|
||||
return nil
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
type openAIResponseFormat struct {
|
||||
Type string `json:"type"`
|
||||
JSONSchema *openAIJSONSchemaEnvelope `json:"json_schema,omitempty"`
|
||||
|
||||
Reference in New Issue
Block a user