AI From Zero · AI for Developers

AI Costs and Token Usage

Learn how AI applications consume tokens, what affects AI costs, and how developers can design efficient, predictable, and sustainable AI systems.

Estimated learning time: 50 minutes

What You'll Learn

  • Understand what tokens are and why they matter for AI applications
  • Learn how input and output tokens contribute to AI usage
  • Understand how conversation history affects token consumption
  • Learn how prompts and retrieved documents affect request size
  • Understand the relationship between token usage and application cost
  • Learn how model selection affects AI costs
  • Understand caching, summarization, and context management
  • Learn how to reduce unnecessary AI requests
  • Understand token usage in tool-enabled and agent systems
  • Learn how to monitor AI usage and costs
  • Understand cost controls such as budgets, limits, and rate limiting
  • Design an efficient AI application cost-management approach

1. Introduction

AI applications consume computing resources whenever they send requests to AI models. In many AI services, usage is measured partly through tokens.

Understanding tokens and AI costs helps developers build applications that are useful without creating unnecessary expense. Cost management is especially important when an application has many users, long conversations, large documents, frequent requests, or autonomous workflows.

2. What Is a Token?

A token is a unit of text processed by an AI model.

Tokens are not always equal to individual words. Depending on the language and text, a token may represent part of a word, a complete short word, punctuation, or another piece of text.

The exact tokenization method depends on the model and tokenizer.

3. Input Tokens

Input tokens are the tokens provided to the AI model as part of a request.

Input can include:

  • System instructions
  • User messages
  • Conversation history
  • Retrieved documents
  • Tool information
  • Structured data
  • Other application context

The more information included in a request, the greater the potential input token usage.

4. Output Tokens

Output tokens are generated by the AI model in its response.

A short response normally consumes fewer output tokens than a long detailed response.

Applications should therefore consider both input and output when estimating usage.

5. Why Token Usage Matters

Token usage can affect several aspects of an AI application:

  • API cost
  • Response latency
  • Context-window usage
  • Application scalability
  • Resource consumption

Efficient token usage can improve both economic and technical performance.

6. Input and Output Pricing

AI providers may price input and output usage separately. Pricing can also vary between models and service tiers.

Developers should therefore check the current pricing information for the specific model and provider being used rather than assuming that all AI requests have the same cost.

7. Model Choice and Cost

Different AI models can have different capabilities, speeds, context limits, and prices.

A highly capable model may be appropriate for a complex reasoning task, while a smaller or less expensive model may be sufficient for simple classification, extraction, or formatting.

Model selection should consider both task requirements and operating cost.

8. Prompt Size

Prompt size directly affects the amount of information sent to the model.

Long prompts may contain useful instructions and context, but unnecessary repetition increases token usage without necessarily improving the result.

Developers should keep instructions clear and include only relevant information.

9. Conversation History

Chat applications often send previous messages so the model can understand the current conversation.

As a conversation becomes longer, the amount of history included in requests can increase significantly.

Sending the entire conversation indefinitely may therefore increase both cost and latency.

10. Managing Long Conversations

Applications can manage long conversations in several ways.

  • Limit the amount of history sent
  • Summarize older messages
  • Keep only relevant previous messages
  • Store long-term information separately
  • Retrieve relevant history when needed

The appropriate approach depends on the application.

11. Retrieved Documents and Token Usage

RAG applications can add retrieved documents to the model context.

Retrieving too much information can increase token usage and may make the model response less focused.

Retrieval should aim to provide enough relevant information without unnecessarily filling the context.

12. Chunking and Cost

Document chunking affects how much information is retrieved.

Very large chunks may include unnecessary material, while very small chunks may require retrieval of many separate sections to reconstruct enough context.

Good chunking can therefore improve both retrieval quality and token efficiency.

13. Context Windows

A model context window determines how much information can be processed in a request and response context.

A larger context capacity does not mean an application should always send the maximum amount of information.

Relevant and focused context is generally more useful than unnecessary volume.

14. Output Length

Applications can often influence output length through instructions, response limits, or structured formats.

For tasks that require a short classification result, generating several paragraphs is unnecessary.

For educational or analytical tasks, longer responses may be appropriate.

15. Structured Outputs and Cost

Structured outputs can make responses easier for software to process and can also encourage concise responses when only specific fields are required.

For example, an invoice classification application may only require a category, confidence value, and short explanation rather than a long narrative.

16. Repeated Requests

Unnecessary repeated AI requests can increase cost and latency.

An application should determine whether a new model request is actually required before sending it.

Some operations can use cached results, deterministic application logic, or previously processed information instead.

17. Caching

Caching stores reusable results so the application does not need to perform the same expensive operation repeatedly.

For example, an application may cache the result of processing stable reference information.

Caching should be designed carefully when information can change or when responses contain user-specific data.

18. Deterministic Logic vs AI

Not every application task requires an AI model.

Simple deterministic operations such as arithmetic, date calculations, permission checks, fixed validation rules, and database lookups are often better handled by traditional software.

Using AI only where it provides meaningful value can reduce cost and improve reliability.

19. Tool Calls and Costs

Tool-enabled AI applications can create additional processing steps.

A model may request a database lookup, receive the result, and then generate another response. Each stage may consume resources.

Developers should consider the total cost of the workflow rather than looking only at the first AI request.

20. Agent Costs

AI agents may perform multiple model calls and tool operations while pursuing a goal.

A simple user request can therefore create a much larger number of model interactions than a single-response application.

Agents should have limits on execution steps, token usage, tool calls, and time where appropriate.

21. Cost of Long-Running Workflows

Long-running AI workflows can accumulate costs gradually.

An application should define stopping conditions and monitor usage so that an unexpected loop or repeated operation does not continue indefinitely.

22. Rate Limiting

Rate limiting restricts how frequently users or systems can send requests.

It can help protect both application availability and AI budgets.

Different limits may be appropriate for normal users, administrators, background processes, and automated agents.

23. Usage Limits

Applications can define limits such as:

  • Requests per minute
  • Requests per day
  • Maximum input size
  • Maximum output size
  • Maximum document size
  • Maximum agent steps
  • Maximum tool calls

These controls help prevent accidental or malicious excessive usage.

24. Budget Controls

Production systems should have mechanisms for monitoring and controlling spending.

Depending on the environment, controls may include account budgets, application-level quotas, alerts, user-level limits, and automatic disabling of non-critical operations after a threshold is reached.

25. Monitoring Token Usage

Applications should measure AI usage rather than estimating it only during development.

Useful metrics may include:

  • Input tokens per request
  • Output tokens per request
  • Total tokens
  • Requests per user
  • Requests per feature
  • Model usage
  • Estimated cost
  • Average response time

26. Cost Attribution

It can be useful to identify which application features consume the most AI resources.

For example, document summarization, customer support, research, and agent workflows may have very different cost profiles.

Tracking usage by feature helps developers make informed optimization decisions.

27. Cost Per User

For applications with many users, average cost per user can help estimate scalability.

Developers can examine typical and heavy usage patterns and determine whether the application can remain economically sustainable as usage increases.

28. Cost Per Task

Cost per task can be more useful than cost per request when a feature requires multiple model calls.

For example, an AI research task might involve planning, search, retrieval, analysis, and final response generation.

The total workflow cost should include all relevant operations.

29. Optimizing Prompts

Prompt optimization is not simply about making prompts shorter.

A good prompt should provide the information necessary for reliable behavior without unnecessary repetition.

Developers should test whether removing or restructuring context affects quality before making aggressive reductions.

30. Optimizing Retrieval

RAG systems can reduce token usage by retrieving only the most relevant information.

Metadata filtering, appropriate chunking, top-k selection, reranking, and retrieval thresholds can help control the amount of context sent to the model.

31. Summarization for Context Management

Long information can sometimes be summarized before being passed to another model request.

However, summarization can remove details. Developers should use it only when the loss of information is acceptable for the task.

32. Model Routing

Model routing means selecting different models for different tasks.

A simple classification may use a smaller model, while a complex analysis may use a more capable model.

Routing can reduce cost while preserving higher capability where it is genuinely required.

33. Batch Processing

Some applications process large numbers of independent items such as documents, records, or classifications.

Where supported by the provider and application requirements, batch processing can be useful for improving operational efficiency.

The exact pricing and behavior depend on the AI service being used.

34. Example: Customer Support Assistant

A customer support assistant may receive a short customer question, retrieve relevant support information, and generate an answer.

Cost can be controlled by keeping instructions concise, retrieving only relevant knowledge, limiting unnecessary conversation history, using an appropriate model, and monitoring usage.

35. Example: Document Summarization

A document summarization system may process large documents and therefore consume significant input tokens.

The application can split documents into appropriate sections, summarize sections separately when needed, and combine the results into a final summary.

The workflow should be evaluated for both cost and information quality.

36. Example: AI Agent

An AI research agent may perform several searches, retrieve documents, call tools, analyze information, and generate a final report.

Because several model and tool operations may occur, the application should enforce limits on steps, tool calls, execution time, and usage.

37. Security and Cost

Cost controls are also a security measure.

An attacker who can generate large numbers of expensive AI requests may cause financial damage even without gaining access to confidential information.

Authentication, rate limiting, quotas, monitoring, and abuse detection can reduce this risk.

38. Common Beginner Mistakes

  • Sending the entire conversation on every request without need
  • Including large documents when only a small section is relevant
  • Using the most expensive model for every task
  • Using AI for simple deterministic operations
  • Allowing agents to run without step limits
  • Ignoring repeated or duplicate requests
  • Failing to monitor token usage
  • Ignoring per-user or per-feature costs
  • Assuming a successful prototype will automatically be affordable at scale

39. A Practical Cost Management Architecture

A useful cost-management flow is:

User → Authentication → Rate Limit → Input Validation → Task Classification → Model Selection → Context Retrieval → AI Request → Usage Measurement → Output Validation → Response

The application can record usage information at the appropriate stages and apply quotas or alerts when necessary.

40. Developer Mental Model

A simple way to think about AI cost is:

More requests + more input context + more output + more expensive models + more workflow steps = higher potential cost.

The goal is not to minimize every token. The goal is to use enough AI processing to deliver the required quality while avoiding unnecessary consumption.

41. Where This Module Goes Next

The next lesson focuses on evaluating AI applications. After understanding how AI applications consume resources, developers need reliable methods for measuring whether those applications actually produce useful, accurate, safe, and consistent results.

Conclusion

Token usage is an important part of designing sustainable AI applications. Input context, generated output, model selection, conversation history, retrieved documents, tool calls, and agent workflows can all affect resource consumption and cost.

Effective cost management combines good architecture, appropriate model selection, efficient context management, rate limiting, usage monitoring, and clear budget controls.

The best optimization is not simply producing the shortest possible response. It is delivering the required result with an appropriate amount of AI processing.

Key Takeaways

• Tokens represent units of text processed by AI models • Both input and output contribute to AI usage • Long prompts and conversation histories can increase token consumption • Retrieved documents can significantly affect input size • Different models can have different costs and capabilities • Caching and deterministic logic can reduce unnecessary AI requests • Tool-enabled applications and agents can create multiple model calls • Rate limits, quotas, and budgets help control usage • Monitoring usage by model, feature, user, and task helps identify expensive workflows • Good retrieval and context management can improve both cost and quality • Security controls such as rate limiting also help protect against AI cost abuse • The goal is efficient AI usage, not minimum token usage at any cost

Try It Yourself

Design a cost-management plan for an AI customer support application. Define: 1) which model type would be used for simple classification and which for complex responses, 2) how conversation history would be controlled, 3) how much document context should be retrieved conceptually, 4) two situations where caching could reduce AI requests, 5) three usage limits, 6) four metrics to monitor, and 7) one strategy for preventing unexpected costs caused by excessive automated requests.

Test Your Knowledge

You've reached the end of this lesson.

Test what you've learned with the Lesson 128 Quiz: AI Costs and Token Usage.

Take the Quiz
← AI Application Security
Evaluating AI Applications →
Back to Course