@@ -30,6 +30,7 @@ import {
3030 type TurbopufferRecord
3131} from "../index" ;
3232import { getCodeIndexName } from "../turbopuffer/utils/get-turbopuffer-namespace" ;
33+ import { estimateTokens , estimateTokensFromArray } from "../utils/estimate-tokens" ;
3334import { runQueryTurbopuffer } from "./run-query-turbopuffer" ;
3435import { MAX_QUERY_ATTEMPTS , TOP_K , TOP_K_CODE } from "./stream-constants" ;
3536
@@ -138,6 +139,13 @@ export async function runRouteForAnthropic({
138139 let timeToFirstToken : number | undefined = undefined ;
139140 let responseText = "" ;
140141
142+ const initialSearchResultTokens = estimateTokensFromArray ( systemPromptDocuments ) ;
143+ let toolCallResultTokens = 0 ;
144+ const toolCallDocumentCounts : { documentationSearch : number ; codeSearch : number } = {
145+ documentationSearch : 0 ,
146+ codeSearch : 0
147+ } ;
148+
141149 const assistantQueryId = crypto . randomUUID ( ) ;
142150
143151 const uiMessageStream = createUIMessageStream ( {
@@ -201,19 +209,18 @@ export async function runRouteForAnthropic({
201209 documentIdsToIgnore . push ( hit . id ) ;
202210 if ( url != null && ! urlsToIgnore . includes ( url ) ) {
203211 urlsToIgnore . push ( url ) ;
204- if ( hit . attributes . document . length > 20000 ) {
205- response . push ( {
206- ...hit . attributes ,
207- document : hit . attributes . document . slice ( 0 , 20000 ) ,
208- url
209- } ) ;
210- } else {
211- response . push ( {
212- ...hit . attributes ,
213- document : hit . attributes . document ,
214- url
215- } ) ;
216- }
212+ const document =
213+ hit . attributes . document . length > 20000
214+ ? hit . attributes . document . slice ( 0 , 20000 )
215+ : hit . attributes . document ;
216+ response . push ( {
217+ ...hit . attributes ,
218+ document,
219+ url
220+ } ) ;
221+
222+ toolCallResultTokens += estimateTokens ( document ) ;
223+ toolCallDocumentCounts . documentationSearch ++ ;
217224 if ( response . length >= TOP_K ) {
218225 return response ;
219226 }
@@ -253,13 +260,19 @@ export async function runRouteForAnthropic({
253260 ] ;
254261 }
255262
256- return result . map ( ( hit ) => ( {
257- ...hit . attributes ,
258- document :
263+ return result . map ( ( hit ) => {
264+ const document =
259265 hit . attributes . document . length > 20000
260266 ? hit . attributes . document . slice ( 0 , 20000 )
261- : hit . attributes . document
262- } ) ) ;
267+ : hit . attributes . document ;
268+
269+ toolCallResultTokens += estimateTokens ( document ) ;
270+ toolCallDocumentCounts . codeSearch ++ ;
271+ return {
272+ ...hit . attributes ,
273+ document
274+ } ;
275+ } ) ;
263276 }
264277 } )
265278 } ,
@@ -327,6 +340,11 @@ export async function runRouteForAnthropic({
327340 namespace : turbopufferNamespace ,
328341 numToolCalls,
329342 finishReason : e . finishReason ,
343+ estimatedInitialSearchResultTokens : initialSearchResultTokens ,
344+ estimatedToolCallResultTokens : toolCallResultTokens ,
345+ numInitialSearchResults : searchResults . length ,
346+ numDocumentationSearchResults : toolCallDocumentCounts . documentationSearch ,
347+ numCodeSearchResults : toolCallDocumentCounts . codeSearch ,
330348 ...e . usage
331349 } ) ;
332350 e . warnings ?. forEach ( ( warning ) => {
0 commit comments