Skip to main content

Every Token Counts

Your app works. Now it has users, and two questions come up quickly: how much is this costing? and why does that answer take so long? Both come down to a few choices: which model you call, how much text you send and get back, whether repeated text is cached, and how hard you ask the model to think. This lesson shows how each one works on Gloo and how to measure it.

Core Concepts

How a Request Is Priced

You pay per token, at the rate of the model you call. Input tokens (what you send) and output tokens (what the model writes) have separate rates, listed per million tokens. The rates come from the public model catalog, GET /platform/v2/models. Each model’s pricing object has input and output, each with rate_per_1m_tokens. These are list prices. What you pay depends on the endpoint: Every response tells you what it used, in usage. On the Responses endpoints that’s input_tokens and output_tokens; on Chat Completions, prompt_tokens and completion_tokens.

Prompt Caching

Many requests start with the same text: a long system prompt, a shared document, the early turns of a conversation. Prompt caching lets the provider reuse its work on that repeated beginning, called the prompt prefix. Cached tokens are billed at a discounted rate, and responses arrive faster. How you turn it on depends on the provider: For the discount to apply, the repeated text has to come first. Put your instructions and any shared documents at the start, and the parts that change, such as the user’s question, at the end. Rates differ by provider. For example, Anthropic cache reads cost 10% of the input rate on most models and less on some newer ones (5% on Claude Opus 5.5 and Sonnet 5.5, 2.5% on Claude Fable 5.1), while cache writes cost 125% (for 5m) or 200% (for 1h). Most OpenAI models charge nothing to write the cache, though some newer ones do. The Prompt Caching guide explains each provider, and the models endpoint lists every model’s exact rates.

Choosing a Model

Pick a model for each job:
  • Start from what the task needs. Check the catalog for the capabilities you rely on (supports_tools, supports_vision, supports_reasoning) and a context_window big enough for your inputs.
  • Compare rates. Models that can do the job may differ a lot in price.
  • Test with your own prompts. Run a handful of real examples through two or three candidates and compare quality, speed and cost. The Chat Playground is a quick way to try them, and the Model Explorer compares them side by side, with speed ratings.
  • Pin the model in production. A fixed model ID means routing never swaps in a different model. When a pinned model is retired, its requests go to a replacement that can be priced differently, so check your models for deprecations (Lesson 9’s Models Come and Go).

Reasoning Effort

Reasoning models think before they answer, and that thinking is billed as output tokens, whether or not you see it. More effort usually means a better answer on hard problems, and also more tokens and a longer wait. (AI 102’s Models That Think Before They Answer explains reasoning from the start.) On the Responses and Chat Completions endpoints, you set it with reasoning: {"effort": ...} (or reasoning_effort on Chat Completions). The levels run from none through minimal, low, medium, high and xhigh to max. Four things to know:
  • Send a level the model lists. Each model’s reasoning.effort_levels in the catalog shows the levels it runs as sent. A level it doesn’t list isn’t guaranteed: the request may run and list it in ignored_params, or return 400.
  • Many models reason by default. With no setting, a model does what its provider does by default, and you pay for that reasoning too. The catalog’s thinks_by_default tells you which ones.
  • none doesn’t always turn reasoning off. It does on models whose effort_levels include none. On others, such as Claude 5 models, it isn’t reliable, and the reasoning they do is still billed.
  • Reasoning uses your output limit. It counts toward max_output_tokens (or max_tokens). A high effort with a small limit can use it all before the answer starts.

Get Started

This script looks up a model’s rates and reasoning levels in the catalog, sends a request at a level the model lists, and estimates what it cost at list price.
Python
Direct Responses bills the published rate, so with no cached tokens this estimate is what you pay. With prompt caching, cached tokens bill at their cache read and write rates instead. On a guarded endpoint, the price is the model’s rate plus 6.5%.

Turn On Caching

For an Anthropic model on the guarded Completions endpoint, add one header:
cURL
For an OpenAI model, add a stable prompt_cache_key to the body, and change it when you change the system prompt:
Caching only starts once the repeated beginning is long enough: 1,024 tokens on OpenAI, and a model-specific minimum on Anthropic. Replace the placeholder text above with your full system prompt before you test it. To see whether caching is working, look at cached_tokens in the response’s usage (input_tokens_details.cached_tokens on Responses, prompt_tokens_details.cached_tokens on Chat Completions), and compare your input costs over time in the billing dashboard. Go further: the Prompt Caching tutorial restructures a prompt so it hits the cache, turns caching on for Claude and OpenAI models and measures the savings. The Reasoning tutorial picks an effort level the model supports, sizes the output limit and catches cut-off answers.

Good to Know

  • Measure before you optimize. Log usage for every request so you know where tokens go. Conversation history (Lesson 2) grows with every turn, so watch long chats.
  • Stable text first, changing text last. This one habit helps caching on every provider.
  • Failover means fewer cache hits for a while. Caches are held by each provider, so a request served by a different provider doesn’t read the cache built on the original one.
  • Check ignored_params. When an effort isn’t applied as sent, the response lists it there. Match on the params list, not the reason text.
  • Stream for speed you can feel. Streaming (Lesson 1) doesn’t make the answer cheaper, but the first words appear much sooner.

Key Takeaway

Cost is tokens times the model’s rate, plus 6.5% on guarded endpoints and Grounded Completions. Lower it by choosing a model that fits the task, putting repeated text first so it can be cached, and setting a reasoning effort the model lists, no higher than the task needs. Measure with usage on every response and check real spend in Studio.
Try it on Gloo: Supported Models shows live rates, caching support and effort levels for every model, and the Prompt Caching guide covers each provider’s setup and rates.

Learn more


What’s next

In the last lesson, you’ll handle rate limits and errors, see how failover keeps your app running, and keep up with model changes.

Lesson 9: Production-Ready

Rate limits and retries, failover and routing, and model lifecycle.