Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.
Cloud & DevOps

AI API Spend Attribution per Feature and Customer with Budget Guards

AI features have a cost per request that changes with prompt length, model choice and retries, and a single heavy customer or a bug in an agent loop can burn a month's budget in a day. Build a lightweight library and collector that tag every AI call with feature, customer and model, compute cost from token counts, show cost per feature and per customer, detect unusual spend quickly, and enforce budgets before the bill arrives.

Difficulty

Intermediate

A small team, or one strong student willing to learn something new.
Effort

1 semester · 2 students

Deliverables

7 to ship

3 optional extras

Suggested stack

TypeScript and Python SDK wrappersOpenTelemetryClickHouse or PostgreSQLGrafanaRedis
A suggestion, not a requirement. Swap anything for what you already know.

What you should ship

  • Small wrappers for two languages that add feature, customer and request tags to every AI API call and emit OpenTelemetry spans using the GenAI semantic conventions
  • A price table per model that can be updated without code changes, and cost calculation from token counts including cached and reasoning tokens where reported
  • Dashboards: cost per feature, per customer, per model and cost per successful outcome
  • Spend anomaly detection that compares each feature's hourly spend with its own history and alerts within minutes
  • Budget guards: soft and hard limits per customer and per feature, enforced in the wrapper with Redis counters
  • A demo SaaS app with three AI features and simulated customers, including a planted runaway loop
  • Reconciliation of your computed costs against the provider's usage export

If you have time left

  • Suggesting cheaper models for features where quality data shows they would be enough
  • Per-customer cost reports for usage-based pricing
  • Export in FOCUS format

The problem: the AI bill arrives as one number

A startup adds a chatbot, a summariser and an agent to its product. The AI provider's bill shows total tokens per model, but not which feature or customer used them. One enterprise customer uploads huge documents; an agent gets stuck retrying; a prompt change doubles the context length. Nobody notices until the invoice.

Cloud cost tools were built for servers and storage. AI spend behaves differently: it is per request, varies widely by input, and can spike within hours.

What already exists and the gap

  • Provider dashboards show usage by model and API key.
  • LLM gateways and observability tools such as LiteLLM, Langfuse and Helicone track cost per request, and LiteLLM supports budgets per key.
  • The OpenTelemetry GenAI semantic conventions standardise attributes such as model and token usage on spans.
  • The FinOps Foundation has published guidance on applying FinOps to AI.

The gap is attribution to business dimensions (feature and customer), unit costs per successful outcome, and fast anomaly alerts, built on open standards so it is not tied to one gateway.

Inspiration

FinOps practice says engineers should see the cost of what they build, close to when they build it. Putting cost per feature next to latency and errors in the same dashboard is a practical step towards that.

What you build

  • Wrappers that tag and measure each call.
  • A collector, a cost model and dashboards.
  • Anomaly alerts and budget guards.
  • A demo app with a planted runaway loop.

The technically interesting part

  • Accurate costs. Cached input tokens, reasoning tokens and batch discounts change the price. Keep prices in a data file and reconcile against the provider's own usage export.
  • Fast anomaly detection. Seasonal baselines per feature, so a busy Monday is not an alert and a stuck loop at 3 a.m. is.
  • Guards without outages. A hard budget stop can break a customer's workflow. Design graceful degradation, such as falling back to a cheaper model.

How you will evaluate it

  • Time from the start of the planted runaway loop to the alert, and to the guard stopping it.
  • Difference between computed and invoiced cost.
  • Overhead added per request.

Path to a product

Every company selling AI features needs to know its margin per customer. A small open source library with a hosted dashboard, or AI cost reviews as a service, are realistic paths.

Scope warning

Use your own API keys with strict spending limits and simulated customers. Keep prices in data and note the date you checked them; do not hard-code them in your report as facts.

Learn what you need on Sefism

Get help with this idea

The risk is cost numbers that do not match the invoice. Tauseef can help you design the cost model, the alerts and a convincing demo. If you are new here, create a free account first.

Ideas and guidance, not finished projects

These are project ideas and scoping guidance, published free for students to use as a starting point. I do not build, write, or sell final-year projects, and I do not complete coursework for anyone. Take an idea, make it yours, and build it.

Not sure this one fits you?

I help students pick an idea that matches their skills and their deadline, then scope it down until it is finishable. Guidance only, never done for you.

Discuss this idea
Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.