Most LLM cost doesn't come from the questions users ask. It comes from everything sent along with each question: the system prompt, the tool definitions, the documents, the conversation so far and the output of the last few tool calls. A user types twelve words and the model reads thirty thousand tokens.

That makes LLM cost optimization mostly an cengineering problem. This post covers where AI token costs come from, seven levers that reduce them, and how FlowGenX builds those levers into every AI Teammate and workflow.

Where AI token costs come from

Input is resent on every turn. Models don't remember, so an agent or chat loop sends the entire history again with each new message. Take a modest agent with 11k tokens of tools and instructions that adds about 2k tokens per turn. Over ten turns it bills 200k input tokens, and almost all of them were sent before.

Input tokens per turn of a ten-turn agent conversation, with and without prompt caching

Output costs more per token. Output tokens are typically priced four to five times higher than input. Reasoning models add "thinking" tokens, which are billed as output and never appear in the answer. The same prompt can use 1,000 thinking tokens on one run and 9,700 on the next.

Model choice multiplies everything. A frontier model can cost a hundred times more per token than a small model from the same vendor. If a classification step runs on the frontier model, every other optimisation is working against a 100x handicap.

7 levers that reduce LLM costs

Each lever acts on a different part of the bill, so they stack rather than add up to one number.

Typical savings reported for each LLM cost lever

1. Prompt caching

Providers keep the start of a prompt on hand and charge far less to reuse it. On Anthropic, a cached read costs 0.1x the normal input price and a cache write costs 1.25x. A PwC study of 500 agent sessions found prompt caching cut API costs by 41–80%. In the ten-turn example above, it brings 200k billed tokens down to about 53k, a 73% saving with no change in output.

2. Model routing with an LLM router

An LLM router sends easy calls to a small model and hard ones to a strong model. In the RouteLLM research, trained routers kept 95% of GPT-4's quality while cutting cost by 35–85%, depending on the benchmark. The catch: moving a conversation to another model throws away its prompt cache, so routing has to be sticky.

3. Context engineering for long sessions

In long agent runs, old tool results and stale turns dominate token cost. Good context engineering clears old tool results and compacts long histories into a summary. In Anthropic's cookbook, a research agent reached 335k tokens in five turns unmanaged and about 70k with compaction. Do it rarely and in large steps, because each change breaks the cache from that point on.

4. Sending fewer tool definitions

Every tool's name, description and schema is input on every call. A large MCP server setup can cost tens of thousands of tokens before the user says hello. Loading tools on demand ("tool search") cuts this overhead by more than 85%.

5. Output discipline

Set an explicit max_tokens, ask for structured output where the answer is data, and choose a thinking effort per task rather than one setting for everything. Log reasoning tokens separately so they show up in reports.

6. Batch and flex pricing

OpenAI and Anthropic both offer 50% off for work that can wait, and the discount stacks with prompt caching. Evaluations, backfills and nightly jobs rarely need an answer in two seconds.

7. Response (semantic) caching

A response cache returns a stored answer when a new question means the same as an old one, skipping the model entirely. Expect 20–45% hit rates in production: about 10% for open chat, up to 70% for FAQ-style support. It is a poor fit for agents and tool calls.

Getting prompt caching right

Prompt caching is the largest lever and the easiest to break by accident. The cache matches the prompt from the first token, and the first token that differs ends the match. Put what never changes first and the new message last.

Layout of a cache-friendly prompt, and what breaks the cache

The common mistakes are mundane: a timestamp at the top of the system prompt, tools that change order between calls, a router that moves the conversation to another model, and calls sent with no session. Each one turns a 0.1x read into a full-price write.

Where to start: LLM observability first

  1. Measure. Record input, output, cached and reasoning tokens per call, by feature and team. LLM observability shows you where the token cost actually is.
  2. Turn on prompt caching and fix the prompt layout.
  3. Cap output with max_tokens and a thinking effort per task.
  4. Trim context and load tools on demand.
  5. Route by difficulty, keeping conversations on their model.
  6. Move non-urgent work to batch.
  7. Add a response cache only where questions genuinely repeat.
  8. Set budgets and alerts, so one runaway loop can't eat a month's spend.

How FlowGenX keeps AI Teammate token costs low

An AI Teammate is an AI colleague with a standing role, memory and permissions. It runs long sessions with many tool calls, which is exactly where token costs grow. So we built the levers into the platform, and no one has to remember every rule.

Where FlowGenX saves tokens: app, gateway and console

In the app.

  • AI Teammates, agents and workflows tag every call with its session.
  • Long chats are compacted into a summary at about 60k tokens.
  • Large tool output is saved to a file instead of being resent.
  • Connector tools load only when the task needs them.

At the gateway.

  • The FlowGenX LLM router adds prompt-caching markers to Claude calls that belong to a session. In our internal tests, 99% of input was read from cache from the second turn onward.
  • Conversations stay on the model they started on.
  • Smart model groups weigh quality, price and latency. In one balanced group, 20 of 22 test calls went to Haiku.

In the console.

  • Every call is logged with its cost, its cached tokens and the workflow that made it.
  • Credit checks run before the call, not after the bill.
  • Spend limits on your own model keys, with alerts at 75%, 90% and 100%, are rolling out now.

Automatic per-call routing runs in watch-only mode while we measure its quality, and batch pricing for non-urgent runs is next.

Cut your LLM costs with FlowGenX

Prompt caching, an LLM router and context engineering do most of the work, without a worse model or a worse answer. See what they save on your own workloads: start your 30-day free trial, no credit card required, or book a demo from the same page.


Sources