@@ -25,6 +25,8 @@ import { jsonStringify } from '../utils/slowOperations.js'
2525import { isToolReferenceBlock } from '../utils/toolSearch.js'
2626import { getAPIMetadata , getExtraBodyParams } from './api/claude.js'
2727import { getAnthropicClient } from './api/client.js'
28+ import { createTrace , endTrace , isLangfuseEnabled , recordLLMObservation } from './langfuse/index.js'
29+ import { getSessionId } from '../bootstrap/state.js'
2830import { withTokenCountVCR } from './vcr.js'
2931
3032// Minimal values for token counting with thinking enabled
@@ -309,6 +311,15 @@ export async function countTokensViaHaikuFallback(
309311 : betas
310312
311313 // biome-ignore lint/plugin: token counting needs specialized parameters (thinking, betas) that sideQuery doesn't support
314+ const apiStart = Date . now ( )
315+ const langfuseTrace = isLangfuseEnabled ( )
316+ ? createTrace ( {
317+ sessionId : getSessionId ( ) ,
318+ model : normalizeModelStringForAPI ( model ) ,
319+ provider : getAPIProvider ( ) ,
320+ name : 'token-estimation' ,
321+ } )
322+ : null
312323 const response = await anthropic . beta . messages . create ( {
313324 model : normalizeModelStringForAPI ( model ) ,
314325 max_tokens : containsThinking ? TOKEN_COUNT_MAX_TOKENS : 1 ,
@@ -331,6 +342,22 @@ export async function countTokensViaHaikuFallback(
331342 const cacheCreationTokens = usage . cache_creation_input_tokens || 0
332343 const cacheReadTokens = usage . cache_read_input_tokens || 0
333344
345+ recordLLMObservation ( langfuseTrace , {
346+ model : normalizeModelStringForAPI ( model ) ,
347+ provider : getAPIProvider ( ) ,
348+ input : messagesToSend ,
349+ output : response . content ,
350+ usage : {
351+ input_tokens : inputTokens ,
352+ output_tokens : usage . output_tokens ,
353+ cache_creation_input_tokens : cacheCreationTokens || undefined ,
354+ cache_read_input_tokens : cacheReadTokens || undefined ,
355+ } ,
356+ startTime : new Date ( apiStart ) ,
357+ endTime : new Date ( ) ,
358+ } )
359+ endTrace ( langfuseTrace )
360+
334361 return inputTokens + cacheCreationTokens + cacheReadTokens
335362}
336363
0 commit comments