From 2498e2eaf49d66a664ed1fdcd7bbd331979cf5b2 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Thu, 9 Jan 2025 19:57:46 +0000 Subject: [PATCH] fix(examples): add token counting example --- examples/count-tokens.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/examples/count-tokens.ts b/examples/count-tokens.ts index e69de29b..22fae1f1 100755 --- a/examples/count-tokens.ts +++ b/examples/count-tokens.ts @@ -0,0 +1,20 @@ +#!/usr/bin/env -S npm run tsn -T + +import Anthropic from '@anthropic-ai/sdk'; + +const client = new Anthropic(); // gets API Key from environment variable ANTHROPIC_API_KEY + +async function main() { + const result = await client.messages.countTokens({ + messages: [ + { + role: 'user', + content: 'Hey Claude!?', + }, + ], + model: 'claude-3-5-sonnet-latest', + }); + console.dir(result); +} + +main();