Cost Optimization
An AI feature has a meter that runs on every request. A prompt that is a little too long, sent a million times, becomes a large bill. Cost optimization finds the tokens that do no work and stops paying for them, without making the answers worse.
6 min read6 মিনিটে পড়া যায় · 7 sections7টি সেকশন · 7 sources7টি সোর্স · updated Nov 2026আপডেট: Nov 2026
The daily bill, lever by leverদৈনিক বিল, এক এক উপায়ে
Apply caching, batch and caps and watch $110 fall toward $62.50 or less.ক্যাশিং, ব্যাচ আর সীমা দিন, আর দেখুন $110 কমে $62.50-এর দিকে নামে, বা আরও কমে।
You’ll learnআপনি শিখবেন
- Why output costs more than inputআউটপুট কেন ইনপুটের চেয়ে দামি
- How caching cuts $110 to $62.50ক্যাশিং কীভাবে $110 থেকে $62.50 করে
- What the batch discount addsব্যাচের ছাড় কী যোগ করে
- Why a cap and a spend limit matterসীমা ও খরচের সীমা কেন জরুরি
- What a cache miss costsক্যাশে না পেলে কত খরচ
In plain words
The Claude API bills by tokens: input tokens that you send, and output tokens that the model writes. Prices are in USD per million tokens (MTok) Source 1সোর্স 1. Output tokens cost more than input tokens. For Claude Sonnet 5.5, input is $2 / MTok and output is $10 / MTok Source 1সোর্স 1.
So the cost of one request depends on three things: the model, the number of input tokens, and the number of output tokens. Cost optimization works on all three, with these levers:
- a smaller model for simple work,
- prompt caching for text that repeats,
- batches for work that can wait,
- limits on tokens and spend.
Anthropic advises to judge the result by the cost of a completed task, not the price of a token Source 5সোর্স 5.
সহজ English
The Claude API charges by tokens. Input tokens are what you send. Output tokens are what the model writes. Prices are in USD for each million tokens (MTok) Source 1সোর্স 1. Output tokens cost more than input tokens. For Claude Sonnet 5.5, input costs $2 / MTok and output costs $10 / MTok Source 1সোর্স 1.
So the cost of one request depends on three things: the model, the number of input tokens, and the number of output tokens. Cost optimization works on all three. It uses these tools:
- a smaller model for simple work,
- prompt caching for text that repeats,
- batches for work that can wait,
- limits on tokens and on spend.
Anthropic says to judge the result by the cost of a finished task, not the price of a token Source 5সোর্স 5.
বাংলা ব্যাখ্যা
Claude API টোকেন (token) অনুযায়ী বিল করে। ইনপুট টোকেন হলো যা আপনি পাঠান। আউটপুট টোকেন হলো যা মডেল লেখে। দাম USD-তে, প্রতি মিলিয়ন টোকেনে (MTok) Source 1সোর্স 1। আউটপুট টোকেনের দাম ইনপুট টোকেনের চেয়ে বেশি। Claude Sonnet 5.5-এর ইনপুট $2 / MTok, আউটপুট $10 / MTok Source 1সোর্স 1।
তাই একটা রিকুয়েস্টের খরচ তিনটা জিনিসের ওপর নির্ভর করে: মডেল, ইনপুট টোকেনের সংখ্যা ও আউটপুট টোকেনের সংখ্যা। কস্ট অপ্টিমাইজেশন (cost optimization) তিনটা নিয়েই কাজ করে। এটা এই উপায়গুলো ব্যবহার করে:
- সহজ কাজের জন্য ছোট মডেল,
- বারবার আসা টেক্সটের জন্য প্রম্পট ক্যাশিং,
- অপেক্ষা করা যায় এমন কাজের জন্য ব্যাচ,
- টোকেন ও খরচের সীমা।
Anthropic বলে, ফল বিচার করুন একটা শেষ হওয়া কাজের খরচ দিয়ে, একটা টোকেনের দাম দিয়ে নয় Source 5সোর্স 5।
How it works
Prices of the current models
| Model | Input / MTok | Output / MTok | Cache hit / MTok |
|---|---|---|---|
| Claude Fable 5.2 | $8 | $40 | $0.20 |
| Claude Opus 5.5 | $4 | $20 | $0.20 |
| Claude Sonnet 5.5 | $2 | $10 | $0.10 |
| Claude Haiku 5.5 (prompt up to 100,000 tokens) | $0.10 | $0.50 | $0.01 |
All prices are from the Claude pricing page Source 1সোর্স 1. Claude Haiku 5.5 has higher prices for a prompt over 100,000 tokens Source 1সোর্স 1.
Lever 1: prompt caching
A cache write costs more than normal input: 1.25 times the base input price for a 5-minute cache, and 2 times for a 1-hour cache Source 2সোর্স 2. A cache read costs much less. It costs 0.1 times the base input price on most models, 0.025 times on Claude Fable 5.2, and 0.05 times on Claude Opus 5.5 and Claude Sonnet 5.5 Source 2সোর্স 2. So a long system prompt that every request repeats is the first thing to cache.
Lever 2: batches
The Batch API gives a 50 % discount on input and output tokens Source 1সোর্স 1. The work runs asynchronously, so use it only when nobody waits for the answer Source 3সোর্স 3. The caching multipliers stack with the batch discount Source 2সোর্স 2.
সহজ English
These are the prices of the current models, from the Claude pricing page Source 1সোর্স 1:
- Claude Fable 5.2: $8 input, $40 output, $0.20 cache hit, for each million tokens.
- Claude Opus 5.5: $4 input, $20 output, $0.20 cache hit, for each million tokens.
- Claude Sonnet 5.5: $2 input, $10 output, $0.10 cache hit, for each million tokens.
- Claude Haiku 5.5 (prompt up to 100,000 tokens): $0.10 input, $0.50 output, $0.01 cache hit, for each million tokens.
Claude Haiku 5.5 has higher prices for a prompt over 100,000 tokens Source 1সোর্স 1.
Lever 1: prompt caching. A cache write costs more than normal input. It is 1.25 times the base input price for a 5-minute cache. It is 2 times for a 1-hour cache Source 2সোর্স 2. A cache read costs much less. It is 0.1 times the base input price on most models. It is 0.025 times on Claude Fable 5.2. It is 0.05 times on Claude Opus 5.5 and Claude Sonnet 5.5 Source 2সোর্স 2. So cache a long system prompt that every request repeats first.
Lever 2: batches. The Batch API gives a 50 % discount on input and output tokens Source 1সোর্স 1. The work runs in the background. So use it only when nobody waits for the answer Source 3সোর্স 3. The batch discount also applies on top of the caching prices Source 2সোর্স 2.
বাংলা ব্যাখ্যা
Claude-এর দামের পেজ থেকে বর্তমান মডেলগুলোর দাম Source 1সোর্স 1:
- Claude Fable 5.2: প্রতি মিলিয়ন টোকেনে ইনপুট $8, আউটপুট $40, ক্যাশ হিট $0.20।
- Claude Opus 5.5: প্রতি মিলিয়ন টোকেনে ইনপুট $4, আউটপুট $20, ক্যাশ হিট $0.20।
- Claude Sonnet 5.5: প্রতি মিলিয়ন টোকেনে ইনপুট $2, আউটপুট $10, ক্যাশ হিট $0.10।
- Claude Haiku 5.5 (100,000 টোকেন পর্যন্ত প্রম্পট): প্রতি মিলিয়ন টোকেনে ইনপুট $0.10, আউটপুট $0.50, ক্যাশ হিট $0.01।
100,000 টোকেনের বেশি প্রম্পটে Claude Haiku 5.5-এর দাম বেশি Source 1সোর্স 1।
উপায় 1: প্রম্পট ক্যাশিং। একটা ক্যাশ রাইটের দাম সাধারণ ইনপুটের চেয়ে বেশি। 5 মিনিটের ক্যাশে এটা মূল ইনপুট দামের 1.25 গুণ। 1 ঘণ্টার ক্যাশে 2 গুণ Source 2সোর্স 2। একটা ক্যাশ রিডের দাম অনেক কম। বেশিরভাগ মডেলে এটা মূল ইনপুট দামের 0.1 গুণ। Claude Fable 5.2-এ 0.025 গুণ, Claude Opus 5.5 ও Claude Sonnet 5.5-এ 0.05 গুণ Source 2সোর্স 2। তাই প্রতিটা রিকুয়েস্টে যে লম্বা সিস্টেম প্রম্পট আসে, সেটা আগে ক্যাশ করুন।
উপায় 2: ব্যাচ। Batch API ইনপুট ও আউটপুট টোকেনে 50 % ছাড় দেয় Source 1সোর্স 1। কাজটা ব্যাকগ্রাউন্ডে চলে। তাই যখন কেউ উত্তরের জন্য অপেক্ষা করে না, শুধু তখনই এটা ব্যবহার করুন Source 3সোর্স 3। ক্যাশিংয়ের দামের ওপরও ব্যাচের ছাড় খাটে Source 2সোর্স 2।
Lever 3: count and cap tokens
The token counting endpoint returns the number of input tokens of a message before you send it Source 4সোর্স 4. Use it to find prompts that grew too large. The API ID of Claude Sonnet 5.5 is claude-sonnet-5-5 Source 7সোর্স 7.
import anthropic
client = anthropic.Anthropic()
count = client.messages.count_tokens(
model="claude-sonnet-5-5",
system="You write test cases from user stories.",
messages=[{"role": "user", "content": "Story: a user resets a password."}],
)
print(count.input_tokens)Every tool definition that you attach is input on every turn Source 5সোর্স 5. max_tokens limits the length of one response. The model cannot see it, so a lower value does not make the model save tokens. A cut-off response is still billed Source 5সোর্স 5.
Lever 4: spend limits
The Start, Build, and Scale usage tiers each have a monthly spend cap. You can set your own lower limit for the organization Source 6সোর্স 6. This is the last safety net if a bug sends too many requests.
সহজ English
Lever 3: count and cap tokens. The token counting endpoint tells you the number of input tokens of a message before you send it Source 4সোর্স 4. Use it to find prompts that grew too large. The Technical text has a short Python example of this call.
Every tool definition that you attach is input on every turn Source 5সোর্স 5. max_tokens limits the length of one response. The model cannot see it. So a lower value does not make the model use fewer tokens. A response that was cut off is still billed Source 5সোর্স 5.
Lever 4: spend limits. The Start, Build, and Scale usage tiers each have a monthly spend cap. You can set your own lower limit for the organization Source 6সোর্স 6. This is the last safety net if a bug sends too many requests.
বাংলা ব্যাখ্যা
উপায় 3: টোকেন গুনুন ও সীমা দিন। Token counting এন্ডপয়েন্ট পাঠানোর আগেই একটা মেসেজের ইনপুট টোকেনের সংখ্যা জানায় Source 4সোর্স 4। যে প্রম্পট অনেক বড় হয়ে গেছে, তা খুঁজতে এটা ব্যবহার করুন। Technical টেক্সটে এই কলের একটা ছোট Python উদাহরণ আছে।
আপনার যোগ করা প্রতিটা টুলের সংজ্ঞা প্রতিটা টার্নে ইনপুট হিসেবে যায় Source 5সোর্স 5। max_tokens একটা রেসপন্সের দৈর্ঘ্য সীমিত করে। মডেল এটা দেখতে পায় না। তাই কম মান দিলে মডেল কম টোকেন ব্যবহার করে না। কেটে যাওয়া রেসপন্সেরও বিল হয় Source 5সোর্স 5।
উপায় 4: খরচের সীমা। Start, Build ও Scale usage tier-এর প্রতিটার একটা মাসিক খরচের সীমা আছে। আপনি অর্গানাইজেশনের জন্য নিজের একটা কম সীমা দিতে পারেন Source 6সোর্স 6। কোনো বাগ যদি অনেক বেশি রিকুয়েস্ট পাঠায়, এটাই শেষ নিরাপত্তা জাল।
A real-life example
A team runs a test-case generator on Claude Sonnet 5.5. It gets 10,000 requests a day. Each request has 3,000 input tokens (2,500 of them are the same system prompt) and 500 output tokens. The numbers below are the tutorial's own calculation, with the prices above Source 1সোর্স 1:
Before:
input 10,000 x 3,000 = 30 M tokens x $2.00 = $60.00
output 10,000 x 500 = 5 M tokens x $10.00 = $50.00
total per day = $110.00
After caching the 2,500-token system prompt (cache hit $0.10 / MTok):
cached 10,000 x 2,500 = 25 M tokens x $0.10 = $2.50
input 10,000 x 500 = 5 M tokens x $2.00 = $10.00
output (no change) = $50.00
total per day (plus a few cache writes) = about $62.50The output is now the largest part of the bill. The team asks for shorter test cases, and moves the nightly regression run of 2,000 stories to the Batch API, at 50 % off Source 1সোর্স 1Source 3সোর্স 3.
সহজ English
A team runs a test-case generator on Claude Sonnet 5.5. It gets 10,000 requests a day. Each request has 3,000 input tokens. 2,500 of them are the same system prompt. Each request also has 500 output tokens. The numbers below are the tutorial's own calculation, with the prices above Source 1সোর্স 1:
- Before: the input is 30 million tokens, which costs $60. The output is 5 million tokens, which costs $50. The total is $110 a day.
- After the team caches the system prompt of 2,500 tokens: 25 million cached tokens cost $2.50. The other 5 million input tokens cost $10. The output still costs $50. The total is about $62.50 a day, plus a few cache writes.
The output is now the largest part of the bill. The team asks for shorter test cases. It moves the nightly regression run of 2,000 stories to the Batch API, at 50 % off Source 1সোর্স 1Source 3সোর্স 3.
বাংলা ব্যাখ্যা
একটা দল Claude Sonnet 5.5-এ একটা টেস্ট কেস জেনারেটর চালায়। দিনে 10,000 রিকুয়েস্ট আসে। প্রতিটা রিকুয়েস্টে 3,000 ইনপুট টোকেন। এর 2,500 টোকেন একই সিস্টেম প্রম্পট। প্রতিটা রিকুয়েস্টে 500 আউটপুট টোকেনও আছে। নিচের হিসাবটা টিউটোরিয়ালের নিজের, ওপরের দাম দিয়ে করা Source 1সোর্স 1:
- আগে: ইনপুট 30 মিলিয়ন টোকেন, খরচ $60। আউটপুট 5 মিলিয়ন টোকেন, খরচ $50। মোট দিনে $110।
- দল 2,500 টোকেনের সিস্টেম প্রম্পট ক্যাশ করার পর: 25 মিলিয়ন ক্যাশ করা টোকেনের খরচ $2.50। বাকি 5 মিলিয়ন ইনপুট টোকেনের খরচ $10। আউটপুটের খরচ এখনো $50। মোট দিনে প্রায় $62.50, সাথে কয়েকটা ক্যাশ রাইট।
এখন আউটপুটই বিলের সবচেয়ে বড় অংশ। দল ছোট টেস্ট কেস চায়। রাতের 2,000 স্টোরির রিগ্রেশন রান Batch API-তে সরায়, 50 % ছাড়ে Source 1সোর্স 1Source 3সোর্স 3।
Common mistakes
- Comparing token prices only. A failed task still bills its tokens and then the retry, so compare cost per completed task Source 5সোর্স 5.
- Caching a prompt that is too short. A prompt below the minimum length for the model is not cached, and the API returns no error Source 2সোর্স 2.
- Trusting a low
max_tokensto save money. The model does not see the cap, and cut-off answers are still billed Source 5সোর্স 5. - Attaching every tool to every request. Each definition is input on every turn Source 5সোর্স 5.
- No spend limit. Without your own limit, only the tier cap stops a runaway job Source 6সোর্স 6.
সহজ English
- Comparing token prices only. A failed task still costs its tokens. Then the retry costs more. So compare the cost of each finished task Source 5সোর্স 5.
- Caching a prompt that is too short. A prompt below the minimum length for the model is not cached. The API returns no error Source 2সোর্স 2.
- Trusting a low
max_tokensto save money. The model does not see the limit. Answers that were cut off are still billed Source 5সোর্স 5. - Attaching every tool to every request. Each tool definition is input on every turn Source 5সোর্স 5.
- No spend limit. Without your own limit, only the tier cap stops a job that runs out of control Source 6সোর্স 6.
বাংলা ব্যাখ্যা
- শুধু টোকেনের দাম তুলনা করা। একটা ব্যর্থ কাজেও তার টোকেনের বিল হয়। তারপর রিট্রাইয়ের খরচ যোগ হয়। তাই প্রতিটা শেষ হওয়া কাজের খরচ তুলনা করুন Source 5সোর্স 5।
- খুব ছোট প্রম্পট ক্যাশ করা। মডেলের ন্যূনতম দৈর্ঘ্যের চেয়ে ছোট প্রম্পট ক্যাশ হয় না। API কোনো এরর দেয় না Source 2সোর্স 2।
- টাকা বাঁচাতে কম
max_tokens-এ ভরসা করা। মডেল সীমাটা দেখে না। কেটে যাওয়া উত্তরেরও বিল হয় Source 5সোর্স 5। - প্রতিটা রিকুয়েস্টে সব টুল যোগ করা। প্রতিটা টুলের সংজ্ঞা প্রতিটা টার্নে ইনপুট হিসেবে যায় Source 5সোর্স 5।
- খরচের কোনো সীমা না থাকা। নিজের সীমা না থাকলে নিয়ন্ত্রণের বাইরে চলে যাওয়া একটা কাজ শুধু tier-এর সীমাই থামায় Source 6সোর্স 6।
What a tester can check
Tutorial advice
These test ideas are the tutorial's own advice.
- Cost per test run. Log the
usageof every call in your AI test suite. Report the cost of each run, and flag a run that costs much more than the last one. - Prompt size gate. In CI, count the tokens of each prompt template. Fail the build when a template grows past an agreed budget.
- Cache hit rate. In a staging run, check how many requests show
cache_read_input_tokensabove 0. A low rate means the cached prefix changes between requests. - Runaway loop. Make an agent loop forever in a test setup. Check that a step limit or a spend limit stops it.
- Same quality, lower cost. After each cost change, run the evaluation set again. Check that the pass rate did not drop.
সহজ English
These test ideas are the tutorial's own advice.
- Cost for each test run. Log the
usageof every call in your AI test suite. Report the cost of each run. Flag a run that costs much more than the last one. - A gate for prompt size. In CI, count the tokens of each prompt template. Fail the build when a template grows past an agreed budget.
- Cache hit rate. In a staging run, check how many requests show
cache_read_input_tokensabove 0. A low rate means that the cached prefix changes between requests. - A loop that never ends. In a test setup, make an agent loop forever. Check that a step limit or a spend limit stops it.
- Same quality, lower cost. After each cost change, run the evaluation set again. Check that the pass rate did not drop.
বাংলা ব্যাখ্যা
এই টেস্টের ধারণাগুলো টিউটোরিয়ালের নিজের পরামর্শ।
- প্রতিটা টেস্ট রানের খরচ। আপনার AI টেস্ট স্যুটের প্রতিটা কলের
usageলগ করুন। প্রতিটা রানের খরচ জানান। আগের রানের চেয়ে অনেক বেশি খরচের রান চিহ্নিত করুন। - প্রম্পটের আকারের গেট। CI-তে প্রতিটা প্রম্পট টেমপ্লেটের টোকেন গুনুন। কোনো টেমপ্লেট ঠিক করা বাজেটের বেশি বড় হলে বিল্ড ফেল করান।
- ক্যাশ হিট রেট। একটা স্টেজিং রানে দেখুন কতগুলো রিকুয়েস্টে
cache_read_input_tokens0-এর বেশি। রেট কম হলে বুঝবেন রিকুয়েস্টের মধ্যে ক্যাশ করা প্রিফিক্স বদলায়। - কখনো শেষ না হওয়া লুপ। একটা টেস্ট সেটআপে একটা এজেন্টকে চিরকাল লুপে চালান। দেখুন একটা ধাপের সীমা বা খরচের সীমা এটা থামায় কি না।
- একই মান, কম খরচ। প্রতিটা খরচ পরিবর্তনের পর ইভ্যালুয়েশন সেট আবার চালান। দেখুন পাস রেট কমেনি।
Key words
| Term | Meaning |
|---|---|
| MTok | One million tokens, the unit of the prices Source 1সোর্স 1 |
| Cache read | Input tokens served from the prompt cache, at a much lower price Source 2সোর্স 2 |
| Cache write | Input tokens stored in the cache, at a higher price than normal input Source 2সোর্স 2 |
| Batch API | Asynchronous processing of many requests at 50 % off Source 1সোর্স 1Source 3সোর্স 3 |
| Token counting | An endpoint that counts the input tokens of a message before you send it Source 4সোর্স 4 |
| Cost per task | The full cost of one completed task, retries included Source 5সোর্স 5 |
| Spend limit | A maximum monthly cost for API use Source 6সোর্স 6 |