Token Optimization Advanced

Reducing token usage saves money, decreases latency, and lets you fit more context into each request. Here are practical techniques for optimizing token usage in production.

Prompt Compression Techniques

Remove Redundancy

Before vs After
# BEFORE: 47 tokens
"I would like you to please analyze the following piece of text
and then provide me with a detailed summary of the main points
that are discussed in the text. The text is as follows:"

# AFTER: 9 tokens
"Summarize the main points of this text:"

# Savings: 80% fewer tokens, same result quality

Use Shorter System Prompts

System Prompt Optimization
# BEFORE: ~150 tokens
"You are a highly knowledgeable and experienced customer support
agent for our company. Your role is to help customers with their
questions and concerns. You should always be polite, professional,
and helpful. You should provide accurate information based on our
company policies. If you don't know the answer, you should let
the customer know and offer to escalate their issue."

# AFTER: ~40 tokens
"Customer support agent. Be polite and accurate. Follow company
policy. Escalate if unsure. Never fabricate information."
Tip: Use bullet points and abbreviated instructions in system prompts. Models understand terse instructions just as well as verbose ones. Every token saved in the system prompt is saved on every single request.

Caching Strategies

  1. Prompt Caching (API-level)

    Use Anthropic's cache_control or OpenAI's automatic caching to avoid re-processing static prompt content. This is the highest-impact optimization for applications with consistent system prompts.

  2. Response Caching (Application-level)

    Cache API responses for identical or near-identical inputs. Use a hash of the input as the cache key. Great for FAQ-type applications.

  3. Semantic Caching

    Use embeddings to find semantically similar past queries and return cached responses when similarity is above a threshold.

Python
import hashlib, json

class ResponseCache:
    def __init__(self):
        self.cache = {}

    def get_key(self, messages):
        content = json.dumps(messages, sort_keys=True)
        return hashlib.sha256(content.encode()).hexdigest()

    def get(self, messages):
        key = self.get_key(messages)
        return self.cache.get(key)

    def set(self, messages, response):
        key = self.get_key(messages)
        self.cache[key] = response

cache = ResponseCache()

# Check cache before API call
cached = cache.get(messages)
if cached:
    response = cached  # Free! No tokens used.
else:
    response = client.messages.create(...)
    cache.set(messages, response)

Batching Requests

Batch APIs process multiple requests in a single job at 50% discount:

Python
# Anthropic Batch API example
from anthropic import Anthropic

client = Anthropic()

# Create a batch of requests
batch = client.messages.batches.create(
    requests=[
        {
            "custom_id": "request-1",
            "params": {
                "model": "claude-sonnet-4-20250514",
                "max_tokens": 1024,
                "messages": [{"role": "user", "content": "Summarize article 1..."}]
            }
        },
        {
            "custom_id": "request-2",
            "params": {
                "model": "claude-sonnet-4-20250514",
                "max_tokens": 1024,
                "messages": [{"role": "user", "content": "Summarize article 2..."}]
            }
        }
    ]
)
# Results arrive within hours at 50% discount

Token-Efficient Formatting

Format Token Efficiency When to Use
Plain text Most efficient Simple responses, summaries
Markdown Efficient Structured content, headers, lists
JSON (compact) Moderate Structured data extraction
JSON (pretty) Less efficient Human-readable structured data
XML Least efficient Avoid unless required by system

Output Length Control

Use max_tokens wisely: Set max_tokens to the minimum needed for your use case. If you only need a yes/no answer, set max_tokens: 10. For summaries, max_tokens: 500 is usually sufficient. Lower max_tokens means lower maximum cost per request and faster responses.

Measuring and Monitoring Usage

Python
import logging

class TokenTracker:
    def __init__(self):
        self.total_input = 0
        self.total_output = 0
        self.request_count = 0

    def log_usage(self, response):
        self.total_input += response.usage.input_tokens
        self.total_output += response.usage.output_tokens
        self.request_count += 1
        logging.info(
            f"Request {self.request_count}: "
            f"in={response.usage.input_tokens} "
            f"out={response.usage.output_tokens} "
            f"cumulative_in={self.total_input} "
            f"cumulative_out={self.total_output}"
        )

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.