Compare countTokens with usageMetadata on the same Gemini payload. Isolate chat history, tools, cached input and multilingual text before disputing a bill.
A developer reported 123,777 input tokens on one request and 463,740 on another, roughly 3.75 times as many. The typed question can stay the same while an agent appends tool definitions, command output or conversation history. To investigate, start with a reproducible payload made from non-sensitive test data.
Use one file for both calls
Save a JSON object containing the contents array as request.json. Start with a single text message. Call countTokens and generateContent on the same model version at the same service. The example below uses the native Gemini API. Supply a model ID and key available on that service. Generation consumes tokens.
cat > request.json <<'JSON'
{"contents":[{"role":"user","parts":[{"text":"Hello, Gemini!"}]}]}
JSON
# GEMINI_BASE_URL, GEMINI_MODEL, GEMINI_API_KEY
for method in countTokens generateContent; do
curl --fail-with-body -sS \
"${GEMINI_BASE_URL%/}/v1beta/models/${GEMINI_MODEL}:${method}" \
-H "x-goog-api-key: ${GEMINI_API_KEY}" \
-H 'Content-Type: application/json' --data-binary @request.json
done
Compare input with input
Compare totalTokens from countTokens with usageMetadata.promptTokenCount from generation. The generation response also has a totalTokenCount that includes other usage. cachedContentTokenCount is part of the prompt count, so adding it to the prompt again would double-count it.
Reintroduce the expensive parts
Repeat with multilingual text, source code and a long chat history. Add one component at a time. When adding tools or system instructions, use the generateContentRequest form supported by countTokens so that both calls measure equivalent input. If a relay does not implement counting, record that limitation. Model ID, API format, UTC time, both counts and the generation request ID make a useful billing report.
API pricing · Setup guide · Google Gemini
- Codex Auto Review pricing: what does one pull request cost?
Estimate small, medium and large reviews at current CheapAI rates. Include the follow-up pass and compare the bill with findings your team can reproduce.
- Claude Fable API pricing for a 100k-token document
Price an initial Fable response and five follow-up questions. Separate uncached input, cache reads and output and check the exact model version.
- Opus vs Sonnet for coding: when does the higher price pay off?
Compare cost per accepted patch, include failed runs and calculate a break-even point before choosing a Claude model for your repository.
- Codex 401 with a custom API key: check the host before buying credits
Diagnose invalid_api_key with the request URL, a catalog check and three provider settings. Keep the key out of terminal output and support tickets.
- Do Russian prompts cost more than English? Measure your own API tokens
Run a paired Claude token-count test, keep the code and tools fixed, then convert the difference to dollars before changing your team’s working language.
- An AI stream ended after HTTP 200. Did the response finish?
Inspect the final stream event, distinguish an output limit from a disconnect, and decide whether retrying a tool call could duplicate work.
- What can a pelican SVG tell you about a coding model?
Evaluate rendering, instruction following and animation separately. Then compare the result with a reproducible patch in your own repository.
- How much does a Codex coding task cost through the API?
Estimate the cost of one bug fix and a batch of twenty using live API rates. Compare models by the cost of a passing patch, including retries.
- Claude wrote the prompt cache. Why did the next request read zero?
Read Claude usage fields, price a cache miss and run a controlled two-request check before changing your prompts or your API provider.