@@ -284,6 +284,48 @@ describe('Langfuse integration', () => {
284284 } ) )
285285 expect ( mockRootEnd ) . toHaveBeenCalled ( )
286286 } )
287+
288+ test ( 'includes cache tokens in usageDetails when provided' , async ( ) => {
289+ process . env . LANGFUSE_PUBLIC_KEY = 'pk-test'
290+ process . env . LANGFUSE_SECRET_KEY = 'sk-test'
291+ const { createTrace, recordLLMObservation } = await import ( '../tracing.js' )
292+ const span = createTrace ( { sessionId : 's1' , model : 'claude-3' , provider : 'firstParty' } )
293+ mockStartObservation . mockClear ( )
294+ mockRootUpdate . mockClear ( )
295+ recordLLMObservation ( span , {
296+ model : 'claude-3' ,
297+ provider : 'firstParty' ,
298+ input : [ ] ,
299+ output : [ ] ,
300+ usage : { input_tokens : 10000 , output_tokens : 50 , cache_creation_input_tokens : 2000 , cache_read_input_tokens : 7000 } ,
301+ } )
302+ expect ( mockRootUpdate ) . toHaveBeenCalledWith ( expect . objectContaining ( {
303+ usageDetails : {
304+ input : 19000 , // 10000 + 2000 + 7000
305+ output : 50 ,
306+ cache_read : 7000 ,
307+ cache_creation : 2000 ,
308+ } ,
309+ } ) )
310+ } )
311+
312+ test ( 'omits cache fields when not provided' , async ( ) => {
313+ process . env . LANGFUSE_PUBLIC_KEY = 'pk-test'
314+ process . env . LANGFUSE_SECRET_KEY = 'sk-test'
315+ const { createTrace, recordLLMObservation } = await import ( '../tracing.js' )
316+ const span = createTrace ( { sessionId : 's1' , model : 'claude-3' , provider : 'firstParty' } )
317+ mockRootUpdate . mockClear ( )
318+ recordLLMObservation ( span , {
319+ model : 'claude-3' ,
320+ provider : 'firstParty' ,
321+ input : [ ] ,
322+ output : [ ] ,
323+ usage : { input_tokens : 100 , output_tokens : 20 } ,
324+ } )
325+ expect ( mockRootUpdate ) . toHaveBeenCalledWith ( expect . objectContaining ( {
326+ usageDetails : { input : 100 , output : 20 } ,
327+ } ) )
328+ } )
287329 } )
288330
289331 describe ( 'recordToolObservation' , ( ) => {
0 commit comments