Repository navigation
fix: bill for serving fallback model in anthropic #5387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -887,6 +887,11 @@ func HandleAnthropicChatCompletionStreaming( | |
| // across events and set on the final chunk: fast mode and data residency. | ||
| var servedSpeed *string | ||
| var servedInferenceGeo *string | ||
| // Model that actually served the turn after a server-side fallback handoff. | ||
| // Latched like the two above because it appears only on the usage event's | ||
| // iterations, and the neutral chat usage drops iterations before pricing — | ||
| // so the final chunk's RoutingInfo is the only place it can survive. | ||
| var servedFallbackModel *string | ||
| // Register the accumulating usage handle so a mid-stream cancel/timeout | ||
| // can bill for tokens already processed Mutated in place below; | ||
| // the deferred HandleStreamCancellation/Timeout reads it from context. | ||
|
|
@@ -982,6 +987,10 @@ func HandleAnthropicChatCompletionStreaming( | |
| servedInferenceGeo = usageToProcess.InferenceGeo | ||
| usage.InferenceGeo = usageToProcess.InferenceGeo | ||
| } | ||
| if served := usageToProcess.ServerSideFallbackModel(); served != nil { | ||
| servedFallbackModel = served | ||
| usage.ServerSideFallbackModel = served | ||
| } | ||
|
Comment on lines
+990
to
+993
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // Collect usage information and send at the end of the stream | ||
| // Here in some cases usage comes before final message | ||
| // So we need to check if the response.Usage is nil and then if usage != nil | ||
|
|
@@ -1150,6 +1159,9 @@ func HandleAnthropicChatCompletionStreaming( | |
| if servedInferenceGeo != nil { | ||
| response.InferenceGeo = servedInferenceGeo | ||
| } | ||
| if servedFallbackModel != nil { | ||
| response.ExtraFields.RoutingInfo.ServerSideFallbackModel = servedFallbackModel | ||
| } | ||
| // Set raw request if enabled | ||
| if sendBackRawRequest { | ||
| providerUtils.ParseAndSetRawRequest(&response.ExtraFields, jsonBody) | ||
|
|
@@ -1508,6 +1520,10 @@ func HandleAnthropicResponsesStream( | |
| var modelName string | ||
| var servedSpeed *string | ||
| var servedInferenceGeo *string | ||
| // Model that served the turn after a server-side fallback handoff. Latched | ||
| // here rather than stamped in the converter: the per-chunk loop below replaces | ||
| // ExtraFields wholesale, so anything the converter puts there is discarded. | ||
| var servedFallbackModel *string | ||
|
|
||
| for { | ||
| // If context was cancelled/timed out, let defer handle it | ||
|
|
@@ -1570,6 +1586,12 @@ func HandleAnthropicResponsesStream( | |
| billedUsage.InferenceGeo = usageToProcess.InferenceGeo | ||
| } | ||
| } | ||
| if served := usageToProcess.ServerSideFallbackModel(); served != nil { | ||
| servedFallbackModel = served | ||
| if billedUsage != nil { | ||
| billedUsage.ServerSideFallbackModel = served | ||
| } | ||
| } | ||
| } | ||
|
|
||
| responses, bifrostErr, isLastChunk := event.ToBifrostResponsesStream(ctx, chunkIndex, streamState) | ||
|
|
@@ -1634,6 +1656,11 @@ func HandleAnthropicResponsesStream( | |
| if servedInferenceGeo != nil { | ||
| response.Response.InferenceGeo = servedInferenceGeo | ||
| } | ||
| // Applied after the per-chunk ExtraFields reset above, which is | ||
| // what makes this survive to the cost calculation. | ||
| if servedFallbackModel != nil { | ||
| response.ExtraFields.RoutingInfo.ServerSideFallbackModel = servedFallbackModel | ||
| } | ||
| // Set raw request if enabled | ||
| if sendBackRawRequest { | ||
| providerUtils.ParseAndSetRawRequest(&response.ExtraFields, jsonBody) | ||
|
|
@@ -2738,6 +2765,8 @@ func (provider *AnthropicProvider) CountTokens(ctx *schemas.BifrostContext, key | |
| request, | ||
| AnthropicRequestBuildConfig{ | ||
| Provider: schemas.Anthropic, | ||
| Model: request.Model, | ||
| IsCountTokens: true, | ||
| IsStreaming: false, | ||
| BetaHeaderOverrides: provider.networkConfig.BetaHeaderOverrides, | ||
| ShouldSendBackRawRequest: provider.sendBackRawRequest, | ||
|
|
@@ -3058,4 +3087,4 @@ func (provider *AnthropicProvider) PassthroughStream( | |
| }, | ||
| }, | ||
| ), nil | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.