Skip to content

Commit 4ef7e80

Browse files
committed
skip token-count request for deepseek flash freebuff
1 parent 99b3d0a commit 4ef7e80

3 files changed

Lines changed: 85 additions & 23 deletions

File tree

common/src/__tests__/free-agents.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getFreebuffRootAgentIdForModel,
1313
isFreebuffGeminiThinkerAgent,
1414
isFreeModeAllowedAgentModel,
15+
shouldUseLocalTokenCountForFreebuffDeepseekFlash,
1516
} from '../constants/free-agents'
1617

1718
describe('free mode agent model allowlist', () => {
@@ -168,4 +169,37 @@ describe('free mode agent model allowlist', () => {
168169
),
169170
).toBe(false)
170171
})
172+
173+
test('uses local token count only for the DeepSeek Flash freebuff root', () => {
174+
expect(
175+
shouldUseLocalTokenCountForFreebuffDeepseekFlash({
176+
agentId: 'base2-free-deepseek-flash',
177+
model: FREEBUFF_DEEPSEEK_V4_FLASH_MODEL_ID,
178+
}),
179+
).toBe(true)
180+
expect(
181+
shouldUseLocalTokenCountForFreebuffDeepseekFlash({
182+
agentId: 'codebuff/base2-free-deepseek-flash@0.0.1',
183+
model: FREEBUFF_DEEPSEEK_V4_FLASH_MODEL_ID,
184+
}),
185+
).toBe(true)
186+
expect(
187+
shouldUseLocalTokenCountForFreebuffDeepseekFlash({
188+
agentId: 'base2-free-deepseek',
189+
model: FREEBUFF_DEEPSEEK_V4_FLASH_MODEL_ID,
190+
}),
191+
).toBe(false)
192+
expect(
193+
shouldUseLocalTokenCountForFreebuffDeepseekFlash({
194+
agentId: 'base2-free-deepseek-flash',
195+
model: FREEBUFF_DEEPSEEK_V4_PRO_MODEL_ID,
196+
}),
197+
).toBe(false)
198+
expect(
199+
shouldUseLocalTokenCountForFreebuffDeepseekFlash({
200+
agentId: 'other/base2-free-deepseek-flash@0.0.1',
201+
model: FREEBUFF_DEEPSEEK_V4_FLASH_MODEL_ID,
202+
}),
203+
).toBe(false)
204+
})
171205
})

common/src/constants/free-agents.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ export function isFreebuffGeminiThinkerAgent(fullAgentId: string): boolean {
161161
return agentId === FREEBUFF_GEMINI_THINKER_AGENT_ID
162162
}
163163

164+
export function shouldUseLocalTokenCountForFreebuffDeepseekFlash(params: {
165+
agentId: string | undefined
166+
model: string | undefined
167+
}): boolean {
168+
const { agentId: fullAgentId, model } = params
169+
if (!fullAgentId || model !== FREEBUFF_DEEPSEEK_V4_FLASH_MODEL_ID) {
170+
return false
171+
}
172+
173+
const { publisherId, agentId } = parseAgentId(fullAgentId)
174+
if (publisherId && publisherId !== 'codebuff') return false
175+
return agentId === 'base2-free-deepseek-flash'
176+
}
177+
164178
/**
165179
* Check if a specific agent is allowed to use a specific model in FREE mode.
166180
* This is the strictest check - validates both the agent AND model combination.

packages/agent-runtime/src/run-agent-step.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
2+
import { shouldUseLocalTokenCountForFreebuffDeepseekFlash } from '@codebuff/common/constants/free-agents'
23
import { supportsCacheControl } from '@codebuff/common/old-constants'
34
import { TOOLS_WHICH_WONT_FORCE_NEXT_STEP } from '@codebuff/common/tools/constants'
45
import { buildArray } from '@codebuff/common/util/array'
@@ -864,29 +865,42 @@ export async function loopAgentSteps(
864865
}),
865866
)
866867

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+
}
890904
}
891905

892906
// 1. Run programmatic step first if it exists

0 commit comments

Comments
 (0)