From d1f3f2c395bf194630f32f8aafded4b0f4ae36d8 Mon Sep 17 00:00:00 2001 From: CaIon Date: Wed, 27 Aug 2025 17:11:33 +0800 Subject: [PATCH] fix(token_counter): update token counting logic to handle media token conditions --- service/token_counter.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/service/token_counter.go b/service/token_counter.go index 84b41328..d1cfaaf8 100644 --- a/service/token_counter.go +++ b/service/token_counter.go @@ -250,13 +250,18 @@ func getImageToken(fileMeta *types.FileMeta, model string, stream bool) (int, er } func CountRequestToken(c *gin.Context, meta *types.TokenCountMeta, info *relaycommon.RelayInfo) (int, error) { - if meta == nil { - return 0, errors.New("token count meta is nil") + if !constant.GetMediaToken { + return 0, nil + } + if !constant.GetMediaTokenNotStream && !info.IsStream { + return 0, nil } - if info.RelayFormat == types.RelayFormatOpenAIRealtime { return 0, nil } + if meta == nil { + return 0, errors.New("token count meta is nil") + } model := common.GetContextKeyString(c, constant.ContextKeyOriginalModel) tkm := 0 @@ -276,7 +281,7 @@ func CountRequestToken(c *gin.Context, meta *types.TokenCountMeta, info *relayco shouldFetchFiles := true - if info.RelayFormat == types.RelayFormatOpenAIRealtime || info.RelayFormat == types.RelayFormatGemini { + if info.RelayFormat == types.RelayFormatGemini { shouldFetchFiles = false }