Lesson

Tracking Token Usage and Costs

The API is not free. Every time you send a request, you pay for it. If you build a popular app, those costs can spiral out of control if you aren't paying attention.

Input vs. Output Tokens

Anthropic bills you based on two separate metrics:

  1. Input Tokens: This is the text you send to Claude (your prompt, the system prompt, the tools, and the entire chatHistory array).
  2. Output Tokens: This is the text Claude generates and sends back to you.

Crucially, Output Tokens are significantly more expensive than Input Tokens. (Often 3x to 5x more expensive, depending on the model).

This makes sense: reading text is computationally easier than generating original text.

Reading the Usage Data

Every time you make an API call, the response object contains a usage block. You should log this data to track your costs.

const response = await anthropic.messages.create({ /* payload */ });

console.log("Input Tokens:", response.usage.input_tokens);
console.log("Output Tokens:", response.usage.output_tokens);

If you are building a SaaS product, you should save this data to your database, tied to the specific user who made the request. If one user is burning through 100,000 tokens a day, you need to know so you can rate-limit them or upgrade their billing tier.

Strategies for Reducing Costs

  1. Use the Right Model: Don't use Opus (claude-3-opus) to categorize emails or extract simple JSON. Use Haiku (claude-3-haiku). Haiku is vastly cheaper and faster, and more than capable of handling 90% of basic data tasks.
  2. Truncate History: As discussed, sending a 50-message chat history means you are paying to process those 50 messages on every single turn. Implement a sliding window in your code that only sends the last 10 messages of the conversation.
  3. Be Brief: If you don't need Claude to be polite, tell it in the System Prompt: "Output the data immediately. Do not say hello. Do not explain your reasoning." Remember, you pay a premium for every word Claude generates. Cut the conversational fluff.
  4. Cache Responses: If multiple users ask your bot "What are the company hours?", don't send that request to Anthropic 500 times. Store the answer in a Redis cache on your server the first time, and serve the cached response to subsequent users for free.

Ready to test your understanding? Take the quiz to reinforce what you learned.

© 2026 Ant Skillsv.26.07.07-23:01
Ant Skills - Lesson