DEV Community

Cover image for How I Cut My LLM API Costs by 70% Without Touching My Code
Shaw Sha
Shaw Sha

Posted on

How I Cut My LLM API Costs by 70% Without Touching My Code

I was spending $200/month on AI APIs. Now it's $60. Same quality, different approach.

The wake-up call came on a Tuesday morning. I opened my provider dashboard and saw a number that made me choke on my coffee: $214. That was my monthly bill for calling LLM APIs from my side projects. Not a huge team, not a massive product — just me, a few cron jobs, and a Telegram bot that summarizes news.

I knew something was wrong. Not because the usage was unexpected — I knew I was making thousands of calls a day. But because I realized I was paying premium prices for things that didn't need premium intelligence.

The Wake-Up Call: Reading My API Bill Like a Horror Story

Here's the thing about LLM APIs: they're incredibly easy to use. You paste in a key, write a few lines of Python, and suddenly your app can "understand" text. But that ease of use is exactly what made me lazy.

I was using GPT-4 for everything. Extracting names from emails? GPT-4. Classifying support tickets? GPT-4. Generating a one-line summary of a YouTube video? GPT-4. It was like using a sports car to go to the grocery store — it works, but you're paying for a lot of horsepower you never use.

The math was brutal: I was burning roughly $6–7 every day on requests that a much smaller model could handle perfectly well.

The First Temptation: Rewriting Everything

My first instinct was to rewrite my code. I thought, "I'll just use GPT-4o-mini for the simple stuff and keep GPT-4 for the hard stuff." I even started mapping out which functions needed which model.

But then I stopped. Because I realized that's a maintenance nightmare:

  • Every new feature requires a model-selection decision
  • Different models have different response formats, even with the same prompts
  • If a model gets deprecated or pricing changes, I have to go back and edit code
  • My codebase would get littered with model names scattered everywhere

This is the trap I see a lot of developers fall into. They think cost optimization means rewriting their application to be "model-aware." But that couples your code to a specific provider's pricing, which changes all the time.

What I Actually Did: Changed the Endpoint, Not the Code

A friend told me about something called an LLM API gateway. The idea is simple: instead of calling OpenAI (or Anthropic, or whoever) directly, your code calls a gateway that sits between you and the providers. And because almost every LLM provider now offers an OpenAI-compatible API, you can use the exact same SDK you're already using.

Here's what my code looked like before:

from openai import OpenAI

client = OpenAI(
    api_key="sk-anthropic-or-openai-key",
    base_url="https://api.expensive-provider.com/v1"
)

response = client.chat.completions.create(
    model="gpt-4o",  # my one-size-fits-all choice
    messages=[{"role": "user", "content": "Classify this ticket: ..."}]
)
Enter fullscreen mode Exit fullscreen mode

And here's what it looks like now:

from openai import OpenAI

client = OpenAI(
    api_key="sk-my-unified-gateway-key",
    base_url="https://tai.shadie-oneapi.com/v1"
)

response = client.chat.completions.create(
    model="gpt-4o",  # same name, routed automatically behind the scenes
    messages=[{"role": "user", "content": "Classify this ticket: ..."}]
)
Enter fullscreen mode Exit fullscreen mode

That's it. Two lines changed. The rest of my code is untouched. Same SDK, same model names, same response handling.

The gateway takes my request, looks at what it needs, and routes it to the cheapest provider that can actually handle it well. Sometimes that's a smaller model from a lesser-known provider that charges a fraction of OpenAI's price. Sometimes it's the actual GPT-4o. The gateway decides.

Why This Works: The Router Does the Thinking

The key insight is that not all requests are created equal. When I ask for a "yes/no" classification, that doesn't need GPT-4. It doesn't even need GPT-4o-mini. A model like Llama 3.1 8B running on a provider with aggressive pricing handles it perfectly.

But when I ask the system to rewrite a legal document or analyze the sentiment of a nuanced customer complaint, then yes, I want the big models.

The gateway I'm using has a routing engine that evaluates each request and picks the best model based on:

  • The complexity of the prompt (length, language, task type)
  • The current cost of each available model
  • Latency requirements
  • Provider availability

The result: about 65–70% of my requests now go to models that cost 80–90% less than GPT-4o. And my application's behavior hasn't changed at all, because these cheaper models are surprisingly capable for routine tasks.

The Numbers: Where the 70% Came From

Let me put real numbers on this.

Before, with everything going to GPT-4o:

  • 200K input tokens/day, 20K output tokens/day
  • GPT-4o pricing: $2.50 per 1M input, $10 per 1M output
  • Daily cost: ~$0.50 + ~$0.20 = $0.70/day for direct usage
  • But with retries, multiple calls per request, and some heavier tasks, the actual bill was closer to $6–7/day

After switching to the gateway with routing:

  • ~65% of requests → Llama 3.1 70B or similar at $0.20–0.30 per 1M input
  • ~25% of requests → GPT-4o-mini at $0.15 per 1M input
  • ~10% of requests → GPT-4o or Claude for the hard stuff

The weighted average dropped my effective cost per million tokens from about $3.20 to under $1.00. And because the gateway also does response caching for identical requests, I saved another chunk.

My bill went from $200–215 to $58–62. That's a 70–72% reduction, depending on the month. I didn't touch my code. I didn't change my prompts. I didn't sacrifice quality. The only thing I changed was where my requests were sent.

What About Latency and Reliability?

This was my biggest concern. I assumed a gateway would add overhead and create a single point of failure.

In practice, the opposite happened. Because the gateway has access to multiple providers, it automatically fails over. When one provider has an outage (OpenAI had several last year), requests get routed to another provider with a compatible model. My Telegram bot didn't go down once during any of the provider outages that other developers were complaining about on Twitter.

Latency was also fine. The gateway adds maybe 5–15ms of overhead on the routing decision, which is invisible compared to the 300–800ms you're already waiting on the LLM itself. And sometimes it's faster, because the cheaper models are often less congested.

One thing to watch: you need to make sure the gateway supports the exact model names your code references. If your code says model="gpt-4o", the gateway needs to either have that model or a mapping to an equivalent. The one I'm using handles this transparently, but I'd recommend testing with a few representative requests before flipping everything over.

The Tool I Ended Up Using

I'm not going to pretend I built any of this myself. I tried a few open-source gateway projects first, but self-hosting a router means you're the one responsible for keeping it up to date with every provider's API changes. That's a whole job I didn't want.

I ended up using a hosted service that does all of this: tai.shadie-oneapi.com. It's a pay-as-you-go endpoint that speaks the OpenAI SDK language, so you literally just swap your base_url and api_key and you're done. No monthly subscription, no minimum spend, no contract. You put in $20, use it for months, and the routing engine handles the rest.

Is it the only option? No. There are other gateways and aggregators out there. But this is the one that worked for me, and the pricing model matches how I like to run my side projects: no commitments, just usage.

Final Thoughts

Here's what this whole experiment taught me: LLM APIs are a commodity, and they should be treated like one. Your code shouldn't be married to a single provider's pricing, because that pricing will change. A gateway layer decouples your application from the volatile world of AI pricing and lets you benefit from market competition without lifting a finger.

If you're spending more than $50/month on AI APIs and you've never questioned whether every request needs the absolute best model, you're probably overpaying. The fix isn't a big refactor. It's a config change and a smart router.

My code looks exactly the same as it did six months ago. My bills don't.

Top comments (0)