Token Counter
v1.0.0Estimate token counts and dollar cost for any text across Claude, GPT, Gemini, Llama and Mistral — before you make the call.
- Price per call
- $0.0002
- Free each month
- 5K calls
- Rate limit
- 300/min
- MCP server
- Included
Overview
Token Counter estimates how many tokens a piece of text will consume and what it will cost, for the models people actually budget against.
Two questions it answers before you spend anything: will this fit in the model's context window, and what will it cost at that model's rate. Both are far cheaper to answer in advance than to discover from a truncation error or an invoice.
The estimate is an approximation and every response says so with approximate: true. An exact count would require each provider's own tokeniser vocabulary, which is correct for one model and wrong for every other. On ordinary prose this lands within a few percent of real counts — the accuracy a budgeting decision actually needs, without the weight.
Prices are maintained by us rather than scraped live, so a vendor changing their pricing page cannot break your cost dashboard. Every response includes pricesUpdated so you can see how fresh the figures are.
Make your first call
Every request goes through the Twotic gateway, which authenticates your key, applies the rate limit, meters the call and forwards it. The exact amount charged comes back on the X-Twotic-Cost header.
curl -X POST "https://www.twotic.dev/v1/token-counter/count" \
-H "Authorization: Bearer $TWOTIC_KEY"const res = await fetch("https://www.twotic.dev/v1/token-counter/count", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.TWOTIC_KEY}`,
},
})
if (!res.ok) throw new Error(await res.text())
const data = await res.json()
console.log(res.headers.get("X-Twotic-Cost"))import os, requests
res = requests.post(
"https://www.twotic.dev/v1/token-counter/count",
headers={"Authorization": f"Bearer {os.environ['TWOTIC_KEY']}"},
)
res.raise_for_status()
data = res.json()
print(res.headers["X-Twotic-Cost"])package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
req, _ := http.NewRequest("POST", "https://www.twotic.dev/v1/token-counter/count", nil)
req.Header.Set("Authorization", "Bearer "+os.Getenv("TWOTIC_KEY"))
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
out, _ := io.ReadAll(res.Body)
fmt.Println(string(out))
fmt.Println(res.Header.Get("X-Twotic-Cost"))
}<?php
$ch = curl_init("https://www.twotic.dev/v1/token-counter/count");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . getenv("TWOTIC_KEY"),
],
]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
print_r($data);require "net/http"
require "json"
uri = URI("https://www.twotic.dev/v1/token-counter/count")
req = Net::HTTP::Post.new(uri)
req["Authorization"] = "Bearer #{ENV.fetch('TWOTIC_KEY')}"
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
http.request(req)
end
puts JSON.parse(res.body)
puts res["X-Twotic-Cost"]{
"mcpServers": {
"token-counter": {
"type": "http",
"url": "https://www.twotic.dev/api/mcp/token-counter",
"headers": { "Authorization": "Bearer tk_live_your_key" }
}
}
}Endpoints
What you can build
Guard a context window before you call
Estimate first and branch: if the prompt would not fit, chunk or summarise instead of sending it and handling a truncation error afterwards.
Show users what a request will cost
In any product that exposes model usage, an up-front cost estimate turns an opaque bill into an informed decision — and reduces the support load that opacity generates.
Compare models on the same workload
Run the same text against several model ids and compare the totals. The cost difference across models for identical output is frequently an order of magnitude.
Use Token Counter from an AI agent
Every endpoint above is also an MCP tool. Pick your client and the steps below fill in with this API’s real server URL, so there is nothing to substitute.
In claude.ai and the Claude desktop app
- 1Open Settings, then Connectors.
- 2Choose Add custom connector.
- 3Paste the server URL below and confirm.
- 4A Twotic sign-in page opens. Approve the connection.
- 5Start a new conversation. The tools are available there.
Server URL
https://www.twotic.dev/api/mcp/token-counterSigns you in through Twotic. Access expires hourly and renews itself, and you can revoke this one app without touching your API keys.
Once connected, the tools are named token-counter_<endpoint>. Every Token Counter endpoint becomes one. Calls are metered exactly like an HTTP request, against the same allowance and balance.
Frequently asked
How accurate is the estimate?+
Within a few percent on ordinary English prose. It is less accurate on dense code, long base64 strings and heavily mixed scripts. Every response is marked approximate: true — use it for budgeting and context-fit checks, not for reconciling an invoice to the token.
Why not use the real tokeniser?+
A count from one provider's tokeniser is simply wrong for another's, and carrying all of them would make this slower and more expensive than the model call it exists to protect you from.
How current are the prices?+
Every response includes a pricesUpdated date. The table tracks published list prices for the models developers most commonly budget against, and is updated as vendors change them.
Can I check whether my prompt fits the context window?+
Yes — that is what fitsInContext and contextUsedPercent are for. Checking before you send turns a failed call into a branch in your own code.