AI-101

Lesson 29

Cost Optimization

AI Confidence: 90%

AI-generated

Cost Optimization

Learning Objectives

Understand how Claude Code pricing works

Monitor costs with /cost and the status line

Choose the right model for each task

Reduce context usage without losing effectiveness

Build cost-aware workflows

Introduction

Claude Code uses API tokens for every interaction. More complex tasks, longer sessions, and larger contexts cost more. Understanding the cost model helps you work efficiently and avoid surprise bills.

This lesson covers pricing basics, monitoring tools, and practical strategies for cost optimization.

How Pricing Works

Claude Code is billed through the Anthropic API by tokens:

Input tokens: What Claude reads (your prompts, context, file contents)

Output tokens: What Claude generates (responses, code, commands)

Different models have different rates:

Opus - Most capable, highest cost

Sonnet - Balanced capability and cost

Haiku - Fast and cheap, good for simple tasks

Output tokens typically cost more than input tokens per unit.

What consumes tokens:

Every message you send

Every file Claude reads

CLAUDE.md context loaded each turn

Previous conversation history

Tool outputs Claude receives

Claude's responses and reasoning

Monitoring Costs

The /cost command:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Shows token usage for your current session:

Total input tokens

Total output tokens

Estimated cost in dollars

Breakdown by turn (optional)

Run this periodically to track usage.

Status line:

Configure the status line to show context usage:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

A visual indicator helps you notice when context is filling up.

Session review:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Running compact shows you context usage before and after optimization.

Choosing Models

Different tasks need different models:

Use Opus for:

Complex architectural decisions

Multi-step refactors across many files

Tasks requiring deep reasoning

When quality matters more than speed

Use Sonnet for:

Most daily coding tasks

Feature implementation

Bug fixes

Code review

Use Haiku for:

Simple questions

Quick lookups

Repetitive operations

CI/CD automation

When speed matters most

Switching models:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Or set environment variable:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Cost comparison:

Haiku can be 10-20x cheaper than Opus for simple tasks. Use the cheapest model that can do the job well.

Reducing Context Usage

Context is the biggest cost driver. Here is how to minimize it:

Compact sessions:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Claude summarizes and clears old conversation history. Do this regularly in long sessions.

Focused prompts:

Instead of:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Try:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Narrow scope means less context.

Specific file references:

Instead of:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Try:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Claude reads only what it needs.

Fresh sessions:

Start new sessions for unrelated tasks. Do not carry context you do not need.

Clear context before big operations:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Then:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Starting clean reduces baseline cost.

Efficient Prompting

How you prompt affects cost:

Be specific:

Vague prompts cause Claude to explore broadly, reading more files and trying more approaches.

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

One thing at a time:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Break large tasks into focused steps.

Provide context in prompts:

If you already know relevant files:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Claude skips exploration.

Ask for plans before execution:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Review the plan, then execute. Catches wrong directions early.

Effort Levels

Effort levels control how much Claude explores:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Minimal exploration

Quick responses

Lower cost

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Moderate exploration (default)

Balanced speed and thoroughness

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Deep exploration

Most thorough

Higher cost

Use low effort for well-defined tasks. Use high effort when you need deep analysis.

Batch Operations

The /batch skill can be cost-efficient for repetitive tasks:

Without batch:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Each interaction has overhead.

With batch:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

One planning phase, parallel execution. Often more cost-effective for 5+ files.

However:

Batch spawns subagents, which have startup costs. For 2-3 files, direct execution may be cheaper.

Cost-Aware Patterns

Research then execute:

Use Haiku or low effort to research

Switch to Sonnet for implementation

Use Opus only for critical decisions

Progressive refinement:

Get a rough implementation fast

Refine specific areas as needed

Less thorough initial pass, then focused improvements.

Cache your knowledge:

Add findings to CLAUDE.md so Claude does not have to rediscover them:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Claude reads this instead of exploring.

Session management:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop

Named, compacted sessions are cheap to resume.

Billing Monitoring

Check usage regularly:

The Anthropic console shows API usage and costs. Review weekly.

Set spending limits:

The Anthropic console lets you set budget caps. This prevents runaway costs.

Track by project:

Use different API keys for different projects if you need cost attribution.

CI budgets:

For automated tasks, set timeouts and consider using Haiku:

[@portabletext/react] Unknown block type "codeBlock", specify a component for it in the `components.types` prop
Key Takeaways

Claude Code costs depend on input tokens, output tokens, and model choice

Monitor costs with /cost and status line context indicators

Use Haiku for simple tasks, Sonnet for daily work, Opus for complex reasoning

Compact sessions regularly to reduce context costs

Write specific, focused prompts to minimize exploration

Use effort levels to control depth of work

Add project knowledge to CLAUDE.md to avoid rediscovery

Set spending limits in the Anthropic console

Try It Yourself

Practice cost-aware usage:

Start a new Claude Code session.

Run /cost to see baseline usage (should be near zero).

Ask Claude to explain a complex part of your codebase.

Run /cost again and note the increase.

Run /compact to reduce context.

Run /cost and notice the reduction.

Try /model haiku and ask a simple question.

Compare the cost to previous queries.

Configure your status line to show context: /statusline show context

Watch the indicator as you work.

This exercise builds cost awareness.

Sources

https://code.claude.com/docs/en/costs - Pricing and cost documentation

https://code.claude.com/docs/en/models - Model selection guide

https://code.claude.com/docs/en/context - Context management