SIGN IN SIGN UP

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

0 0 83 Python

fix(platform): clarify builder cost display for OpenRouter and free providers (#13091)

### Why / What / How

**Why:** The AI Text Generator (and every other LLM block) rendered the
raw `cost_amount=150` for OpenRouter-routed models as if it were per-run
credits → the node card showed `~$1.50/run` for every OR model
regardless of which one the user picked. Backend billing was always
correct (post-flight settles against `x-total-cost`), but the pre-flight
display made cheap models like Qwen 3 look 10× more expensive than
Claude Opus, and Ollama / Z.ai free models had no cost label at all.
Surfaced by Craig in Discord thread
[#1503736802299875441](https://discord.com/channels/1126875755960336515/1503736802299875441/1503736859782811710);
follow-up feedback from Krzysztof (UX is unclear) and Toran (users
shouldn't have to learn the difference between "billed by tokens" vs
"billed by USD").

**What:** Replace the misleading cost amount with the provider's
*published* per-1M-token USD rate, rendered the same way for every LLM
regardless of internal billing path. Show "Free" for Ollama. Show
"Pay-as-you-go" only when we genuinely don't have a published rate.

**How:**
- `BlockCost` now carries optional `input_usd_per_1m`,
`output_usd_per_1m`, `cache_read_usd_per_1m`,
`cache_creation_usd_per_1m` (USD/1M, derived from the existing
`TOKEN_COST` credit/1M rates by dividing by 150 — the cr-per-$
multiplier that already encodes our 1.5× margin). Display-only; the
billing path (`compute_token_credits` / `_coerce_usd × cost_amount`) is
unchanged.
- All TOKENS-typed LLM_COST entries (Anthropic / OpenAI / Groq / AIML /
Llama-API / v0) get rates wired through a new `_tokens_llm_cost()`
helper.
- OpenRouter COST_USD entries *also* get rates wired when the model has
a `TOKEN_COST` row — so OR-billed models (Mistral, Kimi, Cohere, Grok,
Gemini, DeepSeek, Perplexity) display the same `$X.XX in / $Y.YY out per
1M tokens` as direct-billed providers.
- New explicit Ollama loop emits `cost_type=RUN, cost_amount=0` for each
Ollama model so the node card renders "Free" instead of an empty label.
- Added missing `CLAUDE_4_7_OPUS` row to `TOKEN_COST` (was falling back
to flat $0.14/run).
- Frontend `NodeCost.tsx` rewritten to:
- Prefer the rate-pair display (`$X in / $Y out per 1M tokens`) whenever
rates are present, regardless of `cost_type`.
- Tooltip lists cached/cache-write rates when provider publishes them
(Anthropic).
  - "Pay-as-you-go" for COST_USD without rates (no fake number).
  - "Free" for RUN with `cost_amount=0`.

### Changes 🏗️

- `backend/blocks/_base.py`: Add four optional USD/1M rate fields to
`BlockCost`.
- `backend/data/block_cost_config.py`: New `_token_usd_rates()` and
`_tokens_llm_cost()` helpers; populate rates on TOKENS, Groq, OpenRouter
entries; add Ollama free-cost loop; add `CLAUDE_4_7_OPUS` to
`TOKEN_COST`.
- `frontend/.../NodeCost.tsx`: Refactor display logic into a typed
`CostDisplay` union; render in/out USD pair with cache-aware tooltip;
"Free" / "Pay-as-you-go" branches.
- `frontend/.../NodeCost.test.tsx`: Update existing cases for the new
copy; add coverage for token-rate pair (TOKENS + COST_USD), cache rates
in tooltip, "Free" label, and "Pay-as-you-go" label.
- `frontend/src/app/api/openapi.json`: Regenerated to include the new
optional fields.

### Checklist 📋

#### For code changes:
- [ ] I have clearly listed my changes in the PR description
- [ ] I have made a test plan
- [ ] I have tested my changes according to the test plan:
- [ ] Backend: `poetry run pytest
backend/executor/block_usage_cost_test.py
backend/executor/billing_reconciliation_test.py
backend/blocks/test/test_block.py` (1031 passed locally)
- [ ] Frontend: `npx vitest run .../NodeCost.test.tsx` (14 passed
locally)
- [ ] Manual: open AI Text Generator block in builder, verify Opus shows
`$5.00 in / $25.00 out per 1M tokens` with cached rates in tooltip; Qwen
3 Coder shows "Pay-as-you-go"; an Ollama model shows "Free"
- [ ] Manual: confirm post-flight billing for an OpenRouter call still
settles against `x-total-cost` (no regression — `_coerce_usd ×
cost_amount=150` path untouched)
Z
Zamil Majdy committed
6dc836ab868bd5eab50dea2d749fb4c8f65a006c
Parent: c7210ba
Committed by GitHub <noreply@github.com> on 5/12/2026, 5:01:22 PM