# LLM Cost Guard & Model Router

> Add hard budget caps, agent-loop detection, light/strong model routing and per-call token/cost logging to your Python LLM app, then get a cost report with savings estimates. Works with Anthropic, OpenAI or any provider. Standard library only; prices come from your config, never guessed.

LLM Cost Guard & Model Router is a free AI agent skill on Agensi by finance_kim, in LLM Ops. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [finance_kim](https://www.agensi.io/creators/creator-1335)
- Category: [LLM Ops](https://www.agensi.io/skills/llm-ops)
- Version: 1.0
- Last updated: 2026-11-26
- Security: scan passed (2026-11-26); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/llm-cost-guard-model-router
- Tags: llm-ops, agents-orchestration

## About this skill

Stop LLM cost blowups before they happen. This skill has your coding agent add hard budget caps, agent-loop protection, light/strong model routing and per-call cost logging to your own Python LLM app - then turns the logs into a cost report with concrete savings.

**What it adds to your project**
- **Budgets that act before the call** - per-call, per-session and per-day caps in money and/or tokens. The worst case of each call (estimated input + max_tokens) is checked first, so a call that would cross a cap is never sent. Thread-safe, so parallel workers cannot jointly overshoot.
- **Runaway-agent protection** - max iterations, the same tool call repeated, A-B-A-B cycles, "no progress" detection and a wall-clock limit.
- **Light vs. strong model routing** - transparent rule scoring (input length, code, hard/easy keywords, tool count, output size) with a capped escalation when the light model's answer fails your check. Every decision lists its reasons.
- **Per-call log** - JSONL with model, input/output/cache-read/cache-write tokens, cost, latency, route reason and session. No prompt or completion text is stored. Size-based rotation.
- **Cost report** - totals per model, day and session, top expensive sessions, loop suspects, "what if these calls ran on the light model" savings, and recommendations (missing prices, caching, output-heavy models, error rates, budget hits). Markdown or JSON.

**How it works**
The agent finds your LLM call sites, vendors one standard-library Python file into your project, wires it into your Anthropic, OpenAI or other provider calls (sync, streaming or async), configures caps and routing, adds tests with a fake client (no real API spend), and reports what changed.

**Honest numbers**
No vendor prices or model IDs are built in. You fill a price template from your own pricing page or invoice; any model without a price is reported as "cost unknown", never guessed. Savings estimates are labelled as upper bounds.

**Requirements**
Python 3.9+, standard library only. No API keys, no network access, no telemetry.

Includes: SKILL.md workflow, the vendorable module and report CLI, price and router templates, integration recipes, budget-sizing and routing guides, troubleshooting, and four worked examples produced by running the code on clearly labelled synthetic data.

## Requires

- Python 3.9+
- Python app calling an LLM API
- Your own model prices

## Known limitations

Budgets are enforced per process; multi-process deployments split the cap or add a shared counter. The pre-call input estimate is offline and approximate (deliberately high) unless you plug in an exact token counter. Routing is a rule-based heuristic and needs a quality check on your own traffic before switching. Reports only see calls that went through the logger, and invoice totals can differ (discounts, server-side tool fees, failed calls). The bundled module is Python; other stacks can use the report via the documented log format but need their own port of the guard.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the LLM Cost Guard & Model Router skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### Does it include model prices?

No, on purpose. Prices change and differ by platform and contract. You copy the included template and fill in your own prices; a model without a price is reported as cost unknown instead of being guessed.

### Which providers does it support?

Anthropic and OpenAI Python SDKs out of the box (including cache read/write tokens, reasoning tokens and streaming). Any other provider works with a three-line usage extractor; recipes are included.

### Will it call any API or send my data anywhere?

No. It runs inside your code, wraps the calls you already make, has no network access of its own, and the logger stores token counts and a short hash, never prompt text.

### How can a budget stop a call before it is made?

Before each call it prices the worst case: estimated input tokens plus your max_tokens at your prices. If that would push the call, session or day over its cap, it raises BudgetExceeded and the request is never sent. The actual usage replaces the estimate afterwards.

### Is the router a trained model?

No. It is a transparent rule score (length, code, keywords, tools, output size) with configurable thresholds and capped escalation. The skill tells the agent to validate quality on a sample of your traffic before switching.

### My app is not in Python. Can I still use it?

The cost report works on any app that writes the documented JSONL log format. The guard itself is a single Python file; for other languages the agent can port the parts you need, with its own tests.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
