Lesson 1 of 4 Cost Optimization for GenAI

Reducing Cost with Token Efficiency

This lesson covers estimating and tracking tokens and the techniques that cut input and output tokens without reducing effectiveness, since tokens drive most foundation model costs.

Learning outcomes

After completing this lesson, you will be able to:

  • Estimate and track token usage.
  • Optimize input tokens with fewer, better context chunks.
  • Optimize output tokens with limits and concise formats.

Estimating and tracking tokens

A token is a piece of a word; in English, one token is roughly four characters or three quarters of a word, but ratios vary by model and language. Applications estimate tokens before a call (with a token counting API where the model supports it, a tokeniser, or a character-based approximation) to enforce budgets, and track actual usage from the usage field in each response (input, output and cache tokens).

Usage is aggregated by application, feature, tenant and user. InputTokenCount and OutputTokenCount metrics in the AWS/Bedrock namespace show totals by model. Application inference profiles with cost allocation tags attribute Bedrock spend to applications in AWS Cost Explorer, and AWS Cost Anomaly Detection alerts on unexpected spend.

Fill in the number

Estimating tokens and cost

Exam skills 4.1.1

Estimate token counts and costs from the rules of thumb in the lesson.

Log in to try this activity Interactive activities are free with an account.

Optimizing input tokens

Context window optimisation sends only what the model needs. Retrieve fewer, better chunks by reranking and keeping the top results; trim chunks to relevant sections. Context pruning removes old or irrelevant conversation turns, replacing them with a running summary. Prompt compression removes redundant instructions, verbose boilerplate and excess few-shot examples, and replaces long examples with short ones, measuring quality after each cut. Structured data is sent as compact JSON or tables rather than prose. Repeated long prefixes are handled with prompt caching, covered later in this module.

The following diagram compares what each turn of a costly RAG assistant sends before and after changes that cut its tokens.

Cutting the tokens of a costly RAG assistant. Retrieved chunks fall from 20 to 5 after reranking, history beyond the last 6 turns is summarised, the system prompt keeps two examples and is cached, and answers are capped at 300 tokens with a conciseness instruction. An evaluation confirms quality is unchanged.
The figures come from the scenario above.
Select all that apply

Trimming input tokens

Exam skills 4.1.1

A RAG assistant sends a 4,000-token system prompt, 20 retrieved chunks and the full conversation history with every request. Choose the changes that cut input tokens without hurting quality.

Log in to try this activity Interactive activities are free with an account.

Optimizing output tokens

Output tokens are usually priced several times higher than input tokens, and because they are generated one at a time, every extra output token also adds latency. Reducing output length therefore lowers both cost and response time. Response size controls set maxTokens for each use case and instruct the model to be concise, for example "Answer in at most three sentences".

The form of the output matters as much as its length. Asking for structured output, such as a label or a few JSON fields, instead of an explanation can reduce a response from hundreds of tokens to a handful. Stop sequences end generation as soon as the required content is complete. And step-by-step reasoning, which can multiply output length, should be requested only where evaluation shows that it improves results. The following table summarises the techniques.

TechniqueReducesWatch for
Fewer, reranked chunksInput tokensRecall loss; measure retrieval quality
History pruning and summariesInput tokens in long chatsLosing important early facts
Prompt compressionInput tokens on every callQuality regressions; re-evaluate
maxTokens and concise instructionsOutput tokens and latencyTruncated answers
Structured output instead of proseOutput tokensFit to downstream needs
Application inference profilesNothing directly; attributes costTagging discipline
True or false

Output tokens

Exam skills 4.1.1 · 4.2.1

Check three claims about output tokens.

Log in to try this activity Interactive activities are free with an account.
Worked example

A costly RAG assistant

A RAG assistant sends 20 retrieved chunks, the full chat history and a 1,500-token system prompt with eight examples on every turn, and answers average 600 tokens. Costs are three times the budget.

Reranking and keeping 5 chunks, summarising history beyond the last 6 turns, trimming the examples to two, caching the static system prompt, and setting maxTokens to 300 with a conciseness instruction cut tokens substantially; an evaluation run confirms quality is unchanged.

Check yourself

Why limit maxTokens and ask for concise answers?

Show answer

Output tokens cost more than input tokens and also increase latency.

Exam Tips

Remember for the exam

  • Track tokens per application with usage data, metrics and application inference profiles.
  • Send fewer, reranked chunks and pruned history.
  • Output tokens cost more: limit maxTokens and ask for concise answers.
  • Re-evaluate quality after every token reduction.