DEV Community

Cover image for How to Reduce Your AI API Costs by 40% Without Changing Models
Anoop Kumar
Anoop Kumar

Posted on

How to Reduce Your AI API Costs by 40% Without Changing Models

Most cost reduction advice for AI APIs focuses on switching to cheaper models. That works, but it trades quality for savings. These techniques reduce costs without touching model selection — by eliminating token waste that was never producing value in the first place.

After tracking 30 days of AI usage with TokenPulse, I identified patterns that consistently waste 30-50% of tokens. Here is how to eliminate them.

Technique 1: Targeted context pasting

The biggest single source of token waste is pasting entire files when you only need part of them.

The expensive pattern:

User: Here's my entire auth module (500 lines).
      Why is the token validation failing?

// 500 lines = ~30,000 tokens
// Re-sent on every subsequent message in the conversation
Enter fullscreen mode Exit fullscreen mode

The efficient pattern:

User: Here's the validateToken function (20 lines)
      and the middleware calling it (10 lines).
      Why is validation failing?

// 30 lines = ~1,200 tokens
// 96% reduction for equivalent answer quality
Enter fullscreen mode Exit fullscreen mode

For a 10-message debugging session, the efficient pattern saves approximately 260,000 tokens in context overhead — roughly $0.78 on Claude Sonnet or $0.65 on GPT-4o.

The rule: paste the specific function plus 10-15 lines of surrounding context. If the model needs more, it will ask.

Technique 2: Conversation restarts with summaries

Every message in a conversation re-sends the entire history. A 20-message conversation where each exchange averages 500 tokens accumulates 10,000 tokens of history that is re-sent on every new message.

Message 20 input cost:

History tokens: 20 exchanges × 500 tokens = 10,000 tokens
New message:    200 tokens
Total input:    10,200 tokens per message
Enter fullscreen mode Exit fullscreen mode

Compare to starting a new conversation with a 200-word summary:

Summary:     ~150 tokens
New message: 200 tokens
Total input: 350 tokens per message
Enter fullscreen mode Exit fullscreen mode

That is 96% less input tokens for message 20.

The summarize-and-restart pattern:

After 8-10 exchanges:

You: Summarize the key decisions, constraints, and 
current state of this implementation in under 150 words.

[Start new conversation]

You: Context: [paste summary]

     Continue from here: [your next question]
Enter fullscreen mode Exit fullscreen mode

Implement this consistently and your per-conversation cost drops dramatically on long sessions.

Technique 3: System prompt caching (Claude only)

Anthropic's API supports prompt caching for system prompts longer than 1,024 tokens. Cached tokens cost 10% of the normal input price on subsequent requests.

If you are using the Claude API and have a long system prompt — project context, coding standards, architecture documentation — cache it:

const response = await anthropic.messages.create({
  model: 'claude-sonnet-4-5',
  max_tokens: 1024,
  system: [
    {
      type: 'text',
      text: longSystemPrompt, // Your project context
      cache_control: { type: 'ephemeral' }
    }
  ],
  messages: [{ role: 'user', content: userMessage }]
})
Enter fullscreen mode Exit fullscreen mode

For a 5,000-token system prompt sent 100 times:

Without caching:

5,000 tokens × 100 requests × $3.00/1M = $1.50
Enter fullscreen mode Exit fullscreen mode

With caching (first request full price, subsequent at 10%):

5,000 tokens × $3.00/1M           = $0.015 (first)
5,000 tokens × 99 × $0.30/1M      = $0.149 (cached)
Total: $0.164 — 89% reduction
Enter fullscreen mode Exit fullscreen mode

Technique 4: Match message specificity to model

Vague questions produce long, hedged answers. Specific questions produce concise, targeted answers. Concise answers cost less.

Expensive prompt:

Explain authentication in web applications.
Enter fullscreen mode Exit fullscreen mode

Produces: 800-1,500 token response covering every aspect of auth

Efficient prompt:

In my Express app using JWT, should I store 
the refresh token in httpOnly cookie or localStorage?
One paragraph.
Enter fullscreen mode Exit fullscreen mode

Produces: 150-250 token response that answers exactly what you need

Adding "one paragraph", "in two sentences", or "bullet points only" to prompts consistently reduces output token consumption by 40-70% without losing useful information.

Technique 5: Batch related questions

Every API call has a minimum cost — the overhead of the request, the model's context loading, and the response structure. Making five separate one-question API calls costs more than one five-question call.

Five separate calls:

Call 1: "What does this function do?"         → $0.001
Call 2: "What are edge cases?"                → $0.001
Call 3: "How would you test this?"            → $0.001
Call 4: "Any performance concerns?"           → $0.001
Call 5: "Suggest a better variable name?"     → $0.001
Total: $0.005
Enter fullscreen mode Exit fullscreen mode

One batched call:

"For this function: (1) what does it do, 
(2) edge cases, (3) how to test, 
(4) performance concerns, (5) better variable name?"
→ $0.002
Enter fullscreen mode Exit fullscreen mode

Batching reduces overhead and gives the model context across all questions simultaneously — often producing better answers too.

Technique 6: Use cheaper models for context preparation

A common pattern: use a cheap model to prepare and filter context, then send only relevant context to the expensive model.

// Step 1: Use cheap model to identify relevant sections
const relevantSections = await anthropic.messages.create({
  model: 'claude-haiku-4-5', // $0.80/1M input
  max_tokens: 500,
  messages: [{
    role: 'user',
    content: `Given this 500-line file, identify only the 
    functions relevant to JWT authentication. 
    Return just the function names.

    File: ${entireFile}`
  }]
})

// Step 2: Extract only relevant code
const relevantCode = extractFunctions(
  entireFile,
  relevantSections.content[0].text
)

// Step 3: Send targeted context to expensive model
const analysis = await anthropic.messages.create({
  model: 'claude-sonnet-4-5', // $3.00/1M input
  max_tokens: 2048,
  messages: [{
    role: 'user',
    content: `Analyze this JWT authentication code: ${relevantCode}`
  }]
})
Enter fullscreen mode Exit fullscreen mode

The Haiku call costs approximately $0.0003 to process the full file and identify relevant sections. The Sonnet call processes 90% less code. Net savings on large files: 60-80%.

Technique 7: Stop re-explaining context the model already has

A common pattern in long conversations is re-explaining context that the model already has:

Message 15: "As I mentioned earlier, this is a 
Next.js app using TypeScript and Prisma..."
Enter fullscreen mode Exit fullscreen mode

The model already has this information — you told it in message 1. Re-stating it wastes tokens on every message where you do it. Trust the model's context and only re-state when you have genuinely lost track of what is in the conversation history.

Measuring the impact

Without tracking, these optimizations are invisible. After implementing all seven techniques, my monthly estimated AI cost dropped from $0.439 for 157 conversations to a projected $0.26-0.28 for the same volume — a 37-41% reduction.

The biggest single impact was targeted context pasting (technique 1), which alone accounted for approximately 25% of total savings.

TokenPulse makes it easy to see the cost impact of these changes in real time — cost per conversation updates as you chat, so you can immediately see whether a new approach is saving tokens.

Free, no API key, works on Claude, ChatGPT, Gemini, DeepSeek and Grok.

Top comments (0)