How I Slashed My Claude Token Costs

Rishav Sinha
Published on · 4 min read

The Problem
My initial approach to using Large Language Models (LLMs) like Claude was to provide as much context as possible. I assumed more context would lead to better results. However, this led to high API bills and inconsistent, verbose answers. It became clear that simply "talking" to an AI wasn't enough. I needed to optimize my prompts to reduce costs and improve the quality of the output.
Understanding Tokens
Tokens are the basic units of text processed by LLMs like Claude. They can be words, parts of words, punctuation, or spaces. Both the input you provide (the prompt) and the output you receive are measured in tokens, and both contribute to your API costs.
Token optimization is not just about shortening prompts, but about making them more efficient. The goal is to be clear and concise to guide the model to produce the desired output with minimal token usage on both ends.
The Smallest Working Example
Let's look at a common task: extracting specific information from a piece of text. Imagine you're building a system to process customer support tickets. You want to pull out the customer's name, the type of problem, and the urgency.
Here’s how an unoptimized approach might look, versus one that's designed to be efficient:
# extract_ticket_info.py
ticket_text = """
Subject: My app is crashing constantly!
Hi team, my name is Sarah Johnson and I'm really frustrated.
My mobile app, version 2.3.1, keeps crashing every time I try to open the camera.
This has been happening since yesterday morning. I need this fixed ASAP as I use the camera for my business.
My account ID is SJ789.
Thanks,
Sarah
"""
# Inefficient prompt
inefficient_prompt = f"""
Read the following customer support ticket and explain everything you can about it.
Tell me the customer's name, their main issue, and the urgency.
Make sure your explanation is thorough.
<ticket>
{ticket_text}
</ticket>
"""
# Optimized prompt
optimized_prompt = f"""
You are an expert support agent assistant. Your goal is to extract key information from a customer ticket.
Extract the following from the <ticket> provided:
1. Customer Name
2. Problem Type (e.g., "App Crash", "Login Issue")
3. Urgency (Choose from: Low, Medium, High, ASAP)
Provide the output in JSON format.
<ticket>
{ticket_text}
</ticket>
"""
# Example API call (conceptual)
# from anthropic import Anthropic
# client = Anthropic()
# response = client.messages.create(
# model="claude-3-sonnet-20240229",
# max_tokens=200,
# messages=[
# {"role": "user", "content": optimized_prompt}
# ]
# )
# print(response.content)
What did I do here?
- Specific Instructions: Instead of a vague "explain everything," I listed exactly what I wanted to extract.
- Structured Output: Asking for JSON format is huge. It forces Claude to be concise and predictable. This not only reduces output tokens but also makes it trivial for my backend code to parse the response.
- Role Setting: Giving Claude a persona ("You are an expert support agent assistant") subtly guides its behavior and helps it focus on the task with the right tone and efficiency.
- Limiting Output: While not in the prompt itself, when making API calls, setting
max_tokensto a reasonable number (e.g., 200 for this task) prevents Claude from rambling if something goes wrong.
These small changes might seem minor, but across hundreds or thousands of API calls, they add up to significant cost savings and more reliable outputs.
Next Steps
To apply these principles, refactor one of your existing Claude prompts:
- Make instructions more explicit.
- Request a structured output format like JSON or a bulleted list.
- Assign a clear role or persona to the model at the start of the prompt.
Use the Anthropic console's token counter to measure the reduction in prompt size and compare the generated output. The result should be a more consistent and cost-effective response.