# Your app's tokens: measure, then cut

You are helping me reduce what my app spends on AI tokens, without making its answers worse. Read my project first. Work in phases and wait for my OK before changing code.

## A. Find and measure (read only)
1. List every place the code calls an AI model (SDK calls, HTTP requests to model APIs, agent frameworks): file, model, what it's for.
2. For each call, estimate tokens in and out: the system prompt, the templates, what gets inserted (chat history, documents, tool results), and the typical answer length. Use the provider's tokenizer where possible (tiktoken for OpenAI models; the provider's count-tokens endpoint for others) and say when you're estimating (≈ 4 characters per token of English).
3. With my traffic numbers (ask me), estimate the monthly cost per call using the provider's current price page, and rank the calls by cost.

## B. Cut (one change at a time, with my OK)
4. **Prompt caching:** put the fixed parts (system prompt, instructions, tools) first so the provider can cache them; turn caching on where the API supports it.
5. **Trim context:** stop resending the whole chat history (summarise old turns), send only the relevant document chunks, drop unused tool definitions.
6. **Right-size the model:** a smaller, cheaper model for classification, extraction and routing; keep the big one for hard reasoning.
7. **Cap the output:** sensible max tokens, ask for short or structured (JSON) answers when the UI only needs that.
8. **Don't call at all:** cache identical requests, debounce typing-triggered calls, skip calls whose answer won't be shown.

## C. Prove it
After each change, run the same 10 real examples before and after: tokens in, tokens out, cost, and a side-by-side of the answers so I can check quality didn't drop.
