|
1 | 1 | import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events' |
| 2 | +import { shouldUseLocalTokenCountForFreebuffDeepseekFlash } from '@codebuff/common/constants/free-agents' |
2 | 3 | import { supportsCacheControl } from '@codebuff/common/old-constants' |
3 | 4 | import { TOOLS_WHICH_WONT_FORCE_NEXT_STEP } from '@codebuff/common/tools/constants' |
4 | 5 | import { buildArray } from '@codebuff/common/util/array' |
@@ -864,29 +865,42 @@ export async function loopAgentSteps( |
864 | 865 | }), |
865 | 866 | ) |
866 | 867 |
|
867 | | - // Check context token count via Anthropic API |
868 | | - const tokenCountResult = await callTokenCountAPI({ |
869 | | - messages: messagesWithStepPrompt, |
870 | | - system, |
871 | | - model: agentTemplate.model, |
872 | | - tools: toolsForTokenCount, |
873 | | - fetch, |
874 | | - logger, |
875 | | - env: { clientEnv, ciEnv }, |
876 | | - }) |
877 | | - if (tokenCountResult.inputTokens !== undefined) { |
878 | | - currentAgentState.contextTokenCount = tokenCountResult.inputTokens |
879 | | - } else if (tokenCountResult.error) { |
880 | | - logger.warn( |
881 | | - { error: tokenCountResult.error }, |
882 | | - 'Failed to get token count from Anthropic API', |
883 | | - ) |
884 | | - // Fall back to local estimate |
885 | | - const estimatedTokens = |
886 | | - countTokensJson(currentAgentState.messageHistory) + |
887 | | - countTokensJson(system) + |
888 | | - countTokensJson(toolDefinitions) |
889 | | - currentAgentState.contextTokenCount = estimatedTokens |
| 868 | + const estimateContextTokensLocally = () => |
| 869 | + countTokensJson(messagesWithStepPrompt) + |
| 870 | + countTokensJson(system) + |
| 871 | + countTokensJson(toolsForTokenCount) |
| 872 | + |
| 873 | + if ( |
| 874 | + shouldUseLocalTokenCountForFreebuffDeepseekFlash({ |
| 875 | + agentId: agentTemplate.id, |
| 876 | + model: agentTemplate.model, |
| 877 | + }) |
| 878 | + ) { |
| 879 | + currentAgentState.contextTokenCount = estimateContextTokensLocally() |
| 880 | + } else { |
| 881 | + // Check context token count via the web API. |
| 882 | + const tokenCountResult = await callTokenCountAPI({ |
| 883 | + messages: messagesWithStepPrompt, |
| 884 | + system, |
| 885 | + model: agentTemplate.model, |
| 886 | + tools: toolsForTokenCount, |
| 887 | + fetch, |
| 888 | + logger, |
| 889 | + env: { clientEnv, ciEnv }, |
| 890 | + }) |
| 891 | + if (tokenCountResult.inputTokens !== undefined) { |
| 892 | + currentAgentState.contextTokenCount = tokenCountResult.inputTokens |
| 893 | + } else if (tokenCountResult.error) { |
| 894 | + logger.warn( |
| 895 | + { error: tokenCountResult.error }, |
| 896 | + 'Failed to get token count from web API', |
| 897 | + ) |
| 898 | + const estimatedTokens = |
| 899 | + countTokensJson(currentAgentState.messageHistory) + |
| 900 | + countTokensJson(system) + |
| 901 | + countTokensJson(toolDefinitions) |
| 902 | + currentAgentState.contextTokenCount = estimatedTokens |
| 903 | + } |
890 | 904 | } |
891 | 905 |
|
892 | 906 | // 1. Run programmatic step first if it exists |
|
0 commit comments