MCPcopy Index your code
hub / github.com/codeaashu/claude-code / countTokensWithBedrock

Function countTokensWithBedrock

src/services/tokenEstimation.ts:437–495  ·  view source on GitHub ↗
({
  model,
  messages,
  tools,
  betas,
  containsThinking,
}: {
  model: string
  messages: Anthropic.Beta.Messages.BetaMessageParam[]
  tools: Anthropic.Beta.Messages.BetaToolUnion[]
  betas: string[]
  containsThinking: boolean
})

Source from the content-addressed store, hash-verified

435}
436
437async function countTokensWithBedrock({
438 model,
439 messages,
440 tools,
441 betas,
442 containsThinking,
443}: {
444 model: string
445 messages: Anthropic.Beta.Messages.BetaMessageParam[]
446 tools: Anthropic.Beta.Messages.BetaToolUnion[]
447 betas: string[]
448 containsThinking: boolean
449}): Promise<number | null> {
450 try {
451 const client = await createBedrockRuntimeClient()
452 // Bedrock CountTokens requires a model ID, not an inference profile / ARN
453 const modelId = isFoundationModel(model)
454 ? model
455 : await getInferenceProfileBackingModel(model)
456 if (!modelId) {
457 return null
458 }
459
460 const requestBody = {
461 anthropic_version: 'bedrock-2023-05-31',
462 // When we pass tools and no messages, we need to pass a dummy message
463 // to get an accurate tool token count.
464 messages:
465 messages.length > 0 ? messages : [{ role: 'user', content: 'foo' }],
466 max_tokens: containsThinking ? TOKEN_COUNT_MAX_TOKENS : 1,
467 ...(tools.length > 0 && { tools }),
468 ...(betas.length > 0 && { anthropic_beta: betas }),
469 ...(containsThinking && {
470 thinking: {
471 type: 'enabled',
472 budget_tokens: TOKEN_COUNT_THINKING_BUDGET,
473 },
474 }),
475 }
476
477 const { CountTokensCommand } = await import(
478 '@aws-sdk/client-bedrock-runtime'
479 )
480 const input: CountTokensCommandInput = {
481 modelId,
482 input: {
483 invokeModel: {
484 body: new TextEncoder().encode(jsonStringify(requestBody)),
485 },
486 },
487 }
488 const response = await client.send(new CountTokensCommand(input))
489 const tokenCount = response.inputTokens ?? null
490 return tokenCount
491 } catch (error) {
492 logError(error)
493 return null
494 }

Callers 1

Calls 5

isFoundationModelFunction · 0.85
jsonStringifyFunction · 0.85
sendMethod · 0.65
logErrorFunction · 0.50

Tested by

no test coverage detected