agentleFS
MacroTechTitanMacroTechTitan/MaximusAI/skills/maximus-ai-cost-control/SKILL.md

maximus-ai-cost-control

Skill1 starsChanged 4 months agoMITView on GitHub

Token economics for AI products: cost architecture, billing-spike audits, prompt caching, cheaper-model routing, context compression, $/request and $/user budgets. Use for 'AI costs', 'token spend', 'too expensive', 'prompt caching', 'cost per request'.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    If you're not yet calling an LLM in production, the caching and monitoring sections are premature — read maximus-ai-product-spec first to …

    line 21
  2. Distinctive:

    Audit spikes: When $/request spikes: check for context length regression, model routing misconfiguration, caching miss-rate increase, and …

    line 42

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Maximus — AI Cost Control

A language model is a metered API. Every token in and out costs money. Ship without a cost model and you will discover your unit economics at the worst possible time — when you're trying to scale. Build the cost model first.

When to use

  • Designing the cost architecture for a new AI feature before it ships.
  • Auditing a cost spike in production.
  • Setting up caching, batching, or model routing.
  • Doing the unit-economics check before a growth push.
  • Monitoring $/request and $/active-user on an ongoing basis.

If you're not yet calling an LLM in production, the caching and monitoring sections are premature — read maximus-ai-product-spec first to get to a deployable spec.

Core rules

  1. Know the unit before you scale. $/request and $/active-user must be defined before marketing spend or growth investment. A 10× growth at negative unit economics is 10× the problem.
  2. Cheap by default, expensive when needed. Route to the cheapest model that passes quality eval for the task class. Reserve frontier models for tasks that demonstrably require them.
  3. Cache aggressively. The cheapest token is one you never send. Prompt caching, semantic caching, and result caching each attack a different part of the bill.
  4. Compress context, don't truncate naively. Summarize earlier turns; keep the system prompt tight; strip formatting from retrieved documents before embedding them in context.
  5. Alert before the invoice. Set cost anomaly alerts at the API or infrastructure layer. A runaway eval loop or a forgotten batch job should not be discovered on billing day.

Procedure

  1. Map the cost surface. For each LLM call in your product: input tokens (median, p95), output tokens (median, p95), model, calls/user/day. Build the table in examples/cost-dashboard.md.
  2. Apply the caching hierarchy.
    • Prompt caching: For repeated system prompts or few-shot blocks, use the provider's prompt-caching feature (Anthropic prefix caching, OpenAI prompt caching). A cached prefix is 80–90% cheaper than sending it fresh.
    • Semantic caching: For question-answering features, cache by embedding similarity (e.g., GPTCache, Redis with vector search). If the incoming query is within cosine distance 0.05 of a cached query, return the cached result.
    • Result caching: For deterministic outputs (fixed input → fixed output), cache with an exact-match key. Cheapest possible — no model call at all.
  3. Build the routing table. Assign each task class a default model and a threshold for escalation. See examples/routing-config.md for a worked example. Escalation triggers: user explicitly requests "best answer", confidence score below threshold, task type is in the "frontier-only" set.
  4. Compress context. At each call: (a) summarize conversation history older than N turns, (b) trim retrieved chunks to the relevant passage only, (c) strip HTML/markdown formatting from injected documents, (d) audit system prompt for redundancy every sprint.
  5. Set budgets and alerts. Per-user token budgets enforce cost isolation. Provider-level alerts (AWS Budgets, Anthropic spend limits, OpenAI hard limits) are the last-resort backstop. Your own monitoring layer should fire first.
  6. Run the unit-economics check. At current usage: LLM cost per active user per month. Compare to ARPU. If LLM cost > 20% of ARPU on the free tier, the business model needs adjustment before scaling.
  7. Audit spikes. When $/request spikes: check for context length regression, model routing misconfiguration, caching miss-rate increase, and runaway retry loops. See HOWTO.md → "How to audit a sudden cost spike".

Domain notes

  • Prompt caching requires prefixes to be structurally stable. Dynamic content (timestamps, user IDs) in the system prompt defeats caching. Move dynamic content to the user turn.
  • Semantic caching introduces latency for cache misses. Warm the cache with your most common query classes on deploy.
  • Batching (Anthropic Batch API, OpenAI Batch API) is 50% cheaper than real-time for async workloads. Use for evals, bulk enrichment, and nightly jobs — not for user-facing latency-sensitive paths.
  • Model routing logic should be tested with the same rigor as product code. A routing bug that sends all traffic to the frontier model is a silent cost explosion.

Cross-references

  • Quality gates for routed models: maximus-eval-and-test
  • Prompt structure that minimizes tokens: maximus-prompt-engineering
  • Deploying and versioning the cost model itself: maximus-mlops-deploy
  • System design that affects cost architecture: maximus-ai-product-spec

Gotchas

  • Caching with non-deterministic prompts — any dynamic value in a cached prefix (timestamp, session ID) causes a cache miss every time. Audit prompts for hidden dynamism.
  • Routing to a cheaper model without an eval gate — quality regressions are invisible until users complain. Run evals before and after any routing change.
  • Ignoring output token costs — on GPT-4o and Claude 3.5 Sonnet, output tokens are 3–5× more expensive than input. Streaming verbose responses is a bill, not a feature.
  • Context window waste — retrieved documents injected verbatim often contain boilerplate (headers, nav, ads) that contributes tokens but not signal. Pre-process before injection.
  • Forgetting eval costs — running an LLM-as-judge eval at scale is itself an LLM cost. Factor it into the total model of the system.

Output

A cost architecture document covering: the cost-surface table, caching strategy, routing table, compression plan, budget thresholds, and alert configuration. Runnable with examples/cost-dashboard.md as the monitoring template.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.