A simple Node.js application that makes a test call to Claude AI and displays the token usage statistics.
- Node.js (v18 or higher)
- An Anthropic API key
First, navigate to your project directory and install the required packages:
npm installThis will install:
@anthropic-ai/sdk- The Anthropic API clientdotenv- For environment variable management
Create a .env file in the project root and add your Anthropic API key:
ANTHROPIC_API_KEY=your-actual-api-key-hereTo get your API key:
- Go to https://console.anthropic.com/
- Sign up or log in
- Navigate to "API Keys" in the settings
- Create a new API key
- Copy and paste it into your
.envfile
Important: Never commit your .env file to version control. It's already included in .gitignore.
While in the project root, execute the script with:
node src/index.jsThe application will:
- Send a test prompt to Claude (Claude Sonnet 4.5)
- Receive a response
- Display the response text
- Show token usage statistics:
- Input tokens (tokens in your prompt)
- Output tokens (tokens in Claude's response)
- Total tokens used
🤖 Making a test call to Claude...
📝 Response: The International Space Station travels at about 17,500 miles per hour, completing an orbit around Earth every 90 minutes.
📊 Token Usage:
Input tokens: 15
Output tokens: 28
Total tokens: 43
To test with different prompts, edit the prompt in src/index.js:
const result = await callLLM("Your custom prompt here");project-root/
├── package.json # Node.js dependencies and scripts
├── .env # Your API key (not in git)
├── .gitignore # Files to ignore in git
├── README.md # This file
└── src/
├── index.js # Main entry point
└── llmCall.js # LLM API call handler
Run npm install to install dependencies.
Make sure your .env file exists and contains a valid API key.
Your API key may be invalid or expired. Generate a new one from the Anthropic console.
Each API call uses tokens and costs money based on Anthropic's pricing. Monitor your usage at https://console.anthropic.com/
Current pricing (as of January 2025):
- Claude Sonnet 4.5: Check https://www.anthropic.com/pricing for latest rates