Managing and Reducing Costs › Strategy 2: Reduce Output Length

Michael BrenndoerferAugust 26, 20252 min read
AI Agent HandbookAll chapters
Part 15 of 15Performance and Cost Optimization - Doing More with Less
Chapter 43Managing and Reducing Costs › Strategy 2: Reduce Output Length
Reading section 5 of 15 · Strategy 2: Reduce Output Length · 2 minSection 5 of 15 · 2 min

Book contents

Section 5 of 15 · 2 min read

Managing and Reducing Costs › Strategy 2: Reduce Output LengthOne section · Sections in this chapter5 / 15 ↓

Remember that output tokens cost more than input tokens. A response with 1000 tokens costs twice as much as one with 500 tokens. If your agent is verbose, you're wasting money.

Example: Concise Responses (Claude Sonnet 4.5)

In[6]:
Code
import os
from anthropic import Anthropic

client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

def compare_response_costs(query):
    """Compare costs of verbose vs concise responses."""
    
    # Verbose response (default behavior)
    verbose_response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": query}]
    )
    
    # Concise response (optimized)
    concise_system = """You are a helpful assistant. Provide concise, direct answers.
    Use 1-2 sentences for simple questions. Avoid unnecessary elaboration."""
    
    concise_response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=200,  # Hard limit
        system=concise_system,
        messages=[{"role": "user", "content": query}]
    )
    
    # Calculate costs
    def calc_cost(usage):
        input_cost = (usage.input_tokens / 1_000_000) * 3.00
        output_cost = (usage.output_tokens / 1_000_000) * 15.00
        return input_cost + output_cost
    
    verbose_cost = calc_cost(verbose_response.usage)
    concise_cost = calc_cost(concise_response.usage)
    savings = ((verbose_cost - concise_cost) / verbose_cost) * 100
    
    return {
        "verbose": {
            "response": verbose_response.content[0].text,
            "tokens": verbose_response.usage.output_tokens,
            "cost": verbose_cost
        },
        "concise": {
            "response": concise_response.content[0].text,
            "tokens": concise_response.usage.output_tokens,
            "cost": concise_cost
        },
        "savings_percent": savings
    }

## Test with a simple query
result = compare_response_costs("What is Python?")

print("Verbose response:")
print(f"Tokens: {result['verbose']['tokens']}")
print(f"Cost: ${result['verbose']['cost']:.6f}")
print(f"Response: {result['verbose']['response'][:150]}...")
print()

print("Concise response:")
print(f"Tokens: {result['concise']['tokens']}")
print(f"Cost: ${result['concise']['cost']:.6f}")
print(f"Response: {result['concise']['response']}")
print()

print(f"Cost savings: {result['savings_percent']:.1f}%")
Out[6]:
Console
Verbose response:
Tokens: 287
Cost: $0.004338
Response: Python is a high-level, general-purpose programming language created by Guido van Rossum and first released in 1991. Here are its key characteristics:...

Concise response:
Tokens: 44
Cost: $0.000792
Response: Python is a high-level, interpreted programming language known for its clear syntax and readability. It's widely used for web development, data science, automation, artificial intelligence, and general-purpose programming.

Cost savings: 81.7%

The concise version might save 60-70% on output tokens for simple queries. Across thousands of interactions, that's substantial savings.