惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
MIT News - Artificial intelligence
U
Unit 42
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
I
InfoQ
WordPress大学
WordPress大学
H
Help Net Security
D
Docker
B
Blog
腾讯CDC
A
About on SuperTechFans
Recent Announcements
Recent Announcements
雷峰网
雷峰网
有赞技术团队
有赞技术团队
C
Check Point Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
AI API cost control is a routing problem, not a pricing s...
Tokens Forge · 2026-06-27 · via DEV Community

Most teams start AI cost control with a spreadsheet: model A costs this much, model B costs that much, so use the cheaper one.

That helps for a week. Then production traffic arrives.

The real cost problem is not the model price. It is losing the path between a user request and the billable provider call.

Once a product has multiple features, API keys, environments, retries, and fallback routes, the invoice stops answering the question founders actually care about:

Which product path created this spend, and could we have routed it better?

The failure mode

A typical early setup looks like this:

  • one OpenAI key in an environment variable
  • one Claude key for higher quality tasks
  • maybe Gemini or a proxy for cheaper workloads
  • logs that show application errors, but not token economics
  • a monthly provider invoice that arrives too late

This is fine while one developer is experimenting.

It breaks when several workflows share the same provider account. A single retry loop, a background summarizer, or a test environment can quietly become the largest customer in your AI budget.

The bad part is not only that money was spent. The bad part is that you cannot reconstruct the route.

Treat every AI request like a billable event

The cleaner pattern is to attach accounting data before the request leaves your system.

At minimum, every call should carry:

  • user or API key owner
  • project or workspace
  • requested model
  • actual upstream model
  • route type, such as direct, backup, or cheaper pool
  • input and output tokens
  • settlement bucket, such as credits, wallet balance, or internal cost center
  • request id for debugging

This makes the gateway the source of truth, not the provider invoice.

If a request starts as gpt-5.5 but gets served by a backup route, that decision should be visible. If a cheaper model pool handles a non-critical workflow, that should be visible too. If a premium direct route is used, it should be attached to the right balance and owner immediately.

Route policy matters more than average price

Averages hide the thing you need to tune.

For example, a team may discover that 80% of its calls are low-risk transformations that can tolerate a cheaper route, while 20% need the official direct model path. If both are merged into one monthly spend line, nobody can make a good routing decision.

A practical setup separates:

  • official/direct models for workloads where predictability matters
  • ordinary or pooled routes for lower-cost throughput
  • fallback channels for provider instability
  • per-route usage and error logs
  • clear balances or budgets for each settlement path

That is also how you avoid confusing product pricing with provider pricing. A product might sell usage-based credits while still routing internally across several providers. The customer should see a stable API surface; the operator should see the routing economics.

Alerts should trigger on velocity, not just totals

Daily spend alerts are too slow for runaway loops.

Token velocity catches problems earlier. A workflow that normally burns 20k tokens per hour and suddenly burns 2M tokens in 10 minutes is the event you care about. The absolute daily total may still look acceptable when the damage starts.

Useful alert signals include:

  • tokens per minute by API key
  • error rate by upstream channel
  • fallback route frequency
  • spend by model route
  • sudden provider/model mix changes
  • failed requests that still consumed tokens

This is where gateway-level logs beat provider dashboards. Provider dashboards are useful, but they do not know your feature boundaries.

What we are building

I am building Tokens Forge around this idea: one OpenAI-compatible API surface, but with model routing, official/direct and lower-cost routes, usage logs, balance separation, and AI Researcher workflows in one place.

The goal is not to hide complexity with a black-box proxy. The goal is to make the routing and billing path inspectable enough that a founder can answer:

  • which users or keys are spending
  • which models are actually serving requests
  • which routes are expensive but necessary
  • which routes can be moved to a cheaper path
  • which failures need operational attention

If you are building AI features, I would treat gateway instrumentation as product infrastructure, not billing admin.

Once the request leaves your app, the chance to attach useful business context is already mostly gone.

Tokens Forge: https://tokens-forge.com/