liveBook · Manning

Cost optimization

As explained in Build Python Web Apps with Streamlit

Cost optimization is the practice of reducing the resources or money required to operate a system while preserving required outcomes.

Cost optimization reduces the money or resources needed to operate an LLM application while preserving its required behavior. LLM charges depend on input and output tokens, and model pricing varies. section 9.6.2

Practical ways to reduce LLM costs include:

  • Keep prompts short and limit generated output with concise instructions or a maximum token value. section 9.6.2
  • Batch requests when possible instead of sending repeated prompts separately. section 9.6.2
  • Use cheaper models for less complex tasks and check current provider pricing. section 9.6.2
  • Ask users to provide their own API keys, recognizing the resulting account-creation burden. section 9.6.2

Trivia-game requests

A trivia game can reduce costs by generating several questions in one request rather than repeatedly sending the list of previously asked questions. Use a tokenizer to estimate prompt and response costs before choosing the approach. section 9.6.2

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains token-based pricing and practical reductions, but does not quantify savings or compare model quality.
Examples
Uses an OpenAI tokenizer, gpt-5.1 pricing, max_tokens, batched trivia questions, and user-provided API keys.
What it assumes
Assumes an LLM application context and introduces tokens as input or output text chunks.
What you can do after
Apply token counting, prompt reduction, output limits, batching, and model selection to lower LLM costs.
How it connects
Connects tokens, prompts, output limits, batching, model selection, pricing, and API keys.

Also explained in

Compare how 4 books explain cost optimization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.