Repository navigation
feat(chat): meter image requests on the AI budget at the provider's real cost (W4-07) - #2095
Merged
Merged
Conversation
…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
previously approved these changes
Nov 3, 2026
Contributor
|
Conflicts with main. Please merge main in; #2101 needs the same after it. |
8 of 13 tasks
…metering # Conflicts: # server/src/lib/chat-budget.test.js # server/src/routes/internal-chat-budget.js # web/src/lib/chat/budget.test.ts
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
reservetakesimages: 1–10withfeature: 'image'. It holds each image at the model's image price (imageUnitUsd) through the samereserve_chat_ai_usagelock as a text step, so two requests at once can't pass the limit. The row'sunits(00462) is the number held.Charge:
chargetakesimages: { produced, costUsd? }. The row'scost_usdbecomes exactlycostUsd, what OpenRouter said the images cost. When no cost was reported, it isproduced ×the image price.unitsbecomesproduced. Credits draw the same amount. On the workspace's own key nothing counts, as for text.Fit:
fittakesimage: truein place ofperUnit, 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 (nullwithout 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:Measured on 2026-11-03, one image each:
usage.cost)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/readImageSharescall the endpoints above. A reservation is settled once (charge, release or keep), as for text.images/provider.ts(generateImages) calls OpenRouter's chat completions withmodalities: ['image', 'text']andimage_config: { image_size: '1K', aspect_ratio }. It sendsusage: { include: true }andprovider: { 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;defaultImageModelpicks the smallest share.Related issue
None. Chat Wave 4 plan, W4-07 (CHAT-15 §E, CHAT-16 item 10).
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
cd server && npx vitest run src/lib/chat-budget.test.js src/routes/internal-chat-budget.test.js. Covered: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-filesfor Chat,content-imagesfor the editor) and counting them in storage come with W4-08 and W4-09, where they are kept.No migration (00462 has
unitsand theimagefeature). No new environment variables (OPENROUTER_API_KEYexists). No user-visible change yet, so no Help article.Checklist
yarn lintpassesyarn typecheckpassesyarn format:checkpasseshelp/(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 andyarn buildpass; server lint,format:checkand all tests pass.🤖 Generated with Claude Code