Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion relay/channel/openai/relay-openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,12 @@ func OpenaiHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Respo
completionTokens := simpleResponse.Usage.CompletionTokens
if completionTokens == 0 {
for _, choice := range simpleResponse.Choices {
ctkm := service.CountTextToken(choice.Message.StringContent()+choice.Message.GetReasoningContent(), info.UpstreamModelName)
textContent := choice.Message.StringContent() + choice.Message.GetReasoningContent()
// Count tool call name/arguments too, otherwise completion tokens are undercounted when upstream omits usage for tool-calling responses.
for _, tool := range choice.Message.ParseToolCalls() {
textContent += tool.Function.Name + tool.Function.Arguments
}
ctkm := service.CountTextToken(textContent, info.UpstreamModelName)
completionTokens += ctkm
}
}
Expand Down