Model API costs are invisible until they're not. During development you make a few hundred calls and barely notice the bill. In production with real users, the same agent can cost ten times what you expected β not because something is wrong, but because small inefficiencies multiply at scale.
The good news: most of the cost in a typical AI agent comes from a handful of sources, and most of those sources have straightforward fixes that don't affect output quality at all.
This article covers where token costs actually come from and how to reduce them without making your agent worse.
Where the money goes
Before optimising anything, measure where tokens are actually being spent. Log usage on every request:
ChatResponse chatResponse = chatClient . prompt () . user ( request . message ()) . call () . chatResponse ();
Usage usage = chatResponse . getMetadata (). getUsage (); log . info ( "[cost] conversationId={} inputTokens={} outputTokens={} totalTokens={}" , request . conversationId (), usage .β¦