Tracking Cost
Tokens per request.
Models charge by the token, for both the prompt and the answer. A RAG prompt full of context, or an agent that loops, can use far more tokens than you expect. Measure first, and you'll know where the money goes.

- Charged per token
- Input and output
- Measure first
Usage metadata
Every reply carries usage metadata. It counts input tokens, output tokens, and the total. We compare a short prompt with a long one, that includes all the shop's policies as context. Run it. The long prompt uses far more input tokens, even though both answers are short. Context is usually the biggest cost in RAG.
from pathlib import Path
from langchain.chat_models import init_chat_model
model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
policies = "\n".join(p.read_text() for p in Path("kb").glob("*.md"))
prompts = {"short": "Do gift cards expire? Yes or no.",
"with all policies": f"{policies}\n\nDo gift cards expire? Yes or no."}
for name, prompt in prompts.items():
usage = model.invoke(prompt).usage_metadata
print(f"{name:<18} in={usage['input_tokens']:<5} out={usage['output_tokens']:<3} total={usage['total_tokens']}")short in=10 out=36 total=46 with all policies in=428 out=2 total=430
Also uses kb/shipping.md
# Shipping
Standard delivery in the UK takes 3 to 5 working days and costs £2.99.
Delivery is free on orders over £25.
Express delivery arrives the next working day if you order before 2pm, and costs £5.99.
We ship to Ireland and France in 5 to 8 working days for £7.50.
Every order gets a tracking link by email once it leaves our warehouse.Also uses kb/returns.md
# Returns and refunds
You can return any book within 30 days of delivery for a full refund.
Books must be unread and in the condition you received them.
Damaged or faulty books can be returned at any time, and we pay the return postage.
Refunds go back to the original payment method within 5 working days of us receiving the book.
To start a return, reply to your order confirmation email.Also uses kb/gift_cards.md
# Gift cards
Riverside Books gift cards come in values from £10 to £100.
Gift cards never expire and can be used online or in our shop.
A gift card can be used across several orders until the balance is spent.
Gift cards cannot be exchanged for cash.Also uses kb/loyalty.md
# Riverside Readers loyalty club
Joining the Riverside Readers club is free.
Members earn one point for every pound they spend.
100 points can be swapped for a £5 voucher.
Members get early access to signed editions and a birthday discount of 10 percent.Also uses kb/shop.md
# About the shop
Riverside Books has one physical shop, at 12 Mill Lane, Bath.
The shop is open from 9am to 6pm Monday to Saturday, and 11am to 4pm on Sunday.
We sell new and second-hand books, and run a children's story time every Saturday at 10am.
You can contact us at hello@riverside-books.example or on 01225 000 000.Keeping costs down
Retrieve fewer, smaller chunks. Keep system prompts short. Use a small, cheap model where it's good enough. Cache repeated questions. And limit how many calls an agent can make. Multiply tokens by your provider's price per million, to estimate the bill.
| key | value |
|---|---|
| Context | fewer, smaller chunks |
| Model | small where possible |
| Cache | repeated prompts |
| Agents | limit calls |
Summary
Usage metadata shows input and output tokens for every reply. Context is often the biggest cost. Next, logging every call with callbacks.
Key takeaways- reply.usage_metadata
- input + output tokens
- Context costs most