-
Couldn't load subscription status.
- Fork 64
fix(token-estimation): refine logic #921
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,47 +19,42 @@ export function estimateTokens( | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| let calculatedPromptTokens = promptTokens; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let calculatedCompletionTokens = completionTokens; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Always estimate missing tokens for any provider | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!promptTokens || !completionTokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Estimate prompt tokens using encodeChat for better accuracy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!promptTokens && messages && messages.length > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Convert messages to the format expected by gpt-tokenizer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const chatMessages: ChatMessage[] = messages.map((m) => ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| role: m.role, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| typeof m.content === "string" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? m.content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : JSON.stringify(m.content), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: m.name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculatedPromptTokens = encodeChat( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chatMessages, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DEFAULT_TOKENIZER_MODEL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ).length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fallback to simple estimation if encoding fails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.error( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Failed to encode chat messages in estimate tokens", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error instanceof Error ? error : new Error(String(error)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculatedPromptTokens = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| messages.reduce((acc, m) => acc + (m.content?.length || 0), 0) / 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Estimate prompt tokens only if not provided by the API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!promptTokens && messages && messages.length > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Convert messages to the format expected by gpt-tokenizer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const chatMessages: ChatMessage[] = messages.map((m) => ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| role: m.role, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| typeof m.content === "string" ? m.content : JSON.stringify(m.content), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: m.name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculatedPromptTokens = encodeChat( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chatMessages, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DEFAULT_TOKENIZER_MODEL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ).length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fallback to simple estimation if encoding fails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.error( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Failed to encode chat messages in estimate tokens", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error instanceof Error ? error : new Error(String(error)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculatedPromptTokens = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| messages.reduce((acc, m) => acc + (m.content?.length || 0), 0) / 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Estimate completion tokens using encode for better accuracy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!completionTokens && content) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculatedCompletionTokens = encode(JSON.stringify(content)).length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fallback to simple estimation if encoding fails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.error( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Failed to encode completion text", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error instanceof Error ? error : new Error(String(error)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculatedCompletionTokens = content.length / 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Estimate completion tokens only if not provided by the API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!completionTokens && content) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculatedCompletionTokens = encode(JSON.stringify(content)).length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fallback to simple estimation if encoding fails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.error( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Failed to encode completion text", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error instanceof Error ? error : new Error(String(error)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculatedCompletionTokens = content.length / 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
48
to
60
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. Fix completion-path guard and encoding; avoid JSON.stringify and round fallback
Apply this diff: - // Estimate completion tokens only if not provided by the API
- if (!completionTokens && content) {
+ // Estimate completion tokens only if not provided by the API
+ if (completionTokens == null && content != null) {
try {
- calculatedCompletionTokens = encode(JSON.stringify(content)).length;
+ calculatedCompletionTokens = encode(content).length;
} catch (error) {
@@
- calculatedCompletionTokens = content.length / 4;
+ calculatedCompletionTokens = Math.round(content.length / 4);
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard for “missing” must not treat 0 as missing; fix fallback undercount and return integers
!promptTokensre-estimates when the API legitimately returns0. Use a null/undefined check.contentis non-string (objects/arrays). Mirror the encoding path and stringify per‑message before length. Also round to an integer token count.Apply this diff:
Additionally: the parameter
messages: any[]violates the project guideline to avoidany. Consider typing it (e.g., aChatMessageLikewithcontent: unknown) and narrowing in place.📝 Committable suggestion