Skip to content

feat(chat): meter image requests on the AI budget at the provider's real cost (W4-07) - #2095

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-image-metering
Nov 4, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-image-metering

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

W4-07, the first of the three image pull requests (CHAT-15 §E): image requests on the one AI budget, and the OpenRouter image client. Nothing calls them yet; Chat (W4-08) and the editor (W4-09) wire them in. No change to engine.ts.

Owner decisions this follows (2026-11-03): OpenRouter image models through plain fetch, no new package; four images a request, 1K; an image is billed at the provider's real cost (usage.cost), with no markup; the share shown is that cost over the plan's monthly limit; the default model is the cheapest by the price data.

Server (chat-budget.js, internal-chat-budget.js)

  • Hold: reserve takes images: 1–10 with feature: 'image'. It holds each image at the model's image price (imageUnitUsd) through the same reserve_chat_ai_usage lock as a text step, so two requests at once can't pass the limit. The row's units (00462) is the number held.

  • Charge: charge takes images: { produced, costUsd? }. The row's cost_usd becomes exactly costUsd, what OpenRouter said the images cost. When no cost was reported, it is produced × the image price. units becomes produced. Credits draw the same amount. On the workspace's own key nothing counts, as for text.

  • Fit: fit takes image: true in place of perUnit, to say how many of the images asked for the limit left covers.

  • Shares: new GET /image-shares?orgId=&models=a,b (at most 10 models) answers one image's share of the plan's monthly limit per model (null without a limit). The model menu shows it, and the default model is the one with the smallest share.

  • Image price: the synced catalog's image_output_per_m × a 1K image's tokens; $0.25 when the catalog has no price, so an image is never held at nothing. The tokens are per provider:

    • Google: 1,290 (CHAT-16 item 10). Its 1K images bill 1,120, so this leaves room.
    • OpenAI: 5,500. A high-quality 1024² image is 4,160 tokens, and GPT-5 Image Mini's text side adds about a fifth.

    Measured on 2026-11-03, one image each:

    Model Real cost (usage.cost) Price at 1,290 tokens Price here
    Nano Banana 2 Lite $0.0336 $0.0387 $0.0387
    GPT-5 Image Mini $0.0410 $0.0103 $0.0440

    At 1,290 tokens, GPT-5 Image Mini would have been held at a quarter of its cost, and it would have been the "cheapest" default.

Web (lib/chat/budget.ts, lib/chat/images/)

  • reserveImages / fitImages / readImageShares call the endpoints above. A reservation is settled once (charge, release or keep), as for text.
  • images/provider.ts (generateImages) calls OpenRouter's chat completions with modalities: ['image', 'text'] and image_config: { image_size: '1K', aspect_ratio }. It sends usage: { include: true } and provider: { data_collection: 'deny' }, and runs at most four requests at a time with a 90 s timeout. A refused request costs nothing. When a request reports no cost, the result's cost is unknown, so the server charges the price.
  • images/models.ts: Nano Banana 2 Lite, Nano Banana 2, Nano Banana Pro and GPT-5 Image Mini; four variants in Chat, two to four in the editor; defaultImageModel picks the smallest share.

Related issue

None. Chat Wave 4 plan, W4-07 (CHAT-15 §E, CHAT-16 item 10).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. cd server && npx vitest run src/lib/chat-budget.test.js src/routes/internal-chat-budget.test.js. Covered:
    • The provider's cost X takes exactly X from the budget; credits draw exactly X.
    • Four images are 4X. Three produced with no reported cost are 3 × the price.
    • The workspace's own key takes nothing.
    • Two image requests at once with room for one: one is held, the other gets 429, and the spend stays under the limit.
    • The limit left covers 2 of 4 images.
    • The shares on Starter and Growth. GPT-5 Image Mini's price is above Nano Banana 2 Lite's.
    • The endpoints refuse a bad image body with 400.
  2. cd web && yarn vitest run src/lib/chat/budget.test.ts src/lib/chat/images. Covered: the client's request body, four at a time at most, a refusal, an unknown cost and abort; the model list and the default.

Storing the images (chat-files for Chat, content-images for the editor) and counting them in storage come with W4-08 and W4-09, where they are kept.

No migration (00462 has units and the image feature). No new environment variables (OPENROUTER_API_KEY exists). No user-visible change yet, so no Help article.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

CI passed on this branch (7646f06), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41642038350): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

…eal cost (W4-07)

An image request now holds each image it may make at the model's image
price, then takes from the one AI budget exactly what OpenRouter said the
images cost (usage.cost), with no markup; credits draw the same amount and
the workspace's own key counts nothing. The server answers how many images
the limit left covers and what share of the plan's monthly limit one image
is, per model, so the menu can show it and pick the cheapest as default.

The web side adds the OpenRouter image client (plain fetch, four at a time,
1K, refusals cost nothing) and the image models list. Nothing calls them
yet: Chat (W4-08) and the editor (W4-09) wire them in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
gkhngyk previously approved these changes Nov 3, 2026
@gkhngyk

gkhngyk commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Conflicts with main. Please merge main in; #2101 needs the same after it.

…metering

# Conflicts:
#	server/src/lib/chat-budget.test.js
#	server/src/routes/internal-chat-budget.js
#	web/src/lib/chat/budget.test.ts

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@gkhngyk
gkhngyk merged commit 8130dc1 into ansvisor:main Nov 4, 2026
7 checks passed
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 4, 2026
…mages

ansvisor#2095 (W4-07) is now in main. The budget files keep main's ansvisor#2095 code
plus ansvisor#2101's reserve-draft-images, identical to ansvisor#2101's latest
(feature/image-budget-editor f80a465), and this branch's W4-09 editor
additions in web/src/lib/chat/budget.ts.

# Conflicts:
#	server/src/lib/chat-budget.test.js
#	server/src/routes/internal-chat-budget.js
#	web/src/lib/chat/budget.test.ts

web/.env.example: main now documents OPENROUTER_API_KEY (W4-03); the
editor's images join that entry's comment instead of a second key line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants