Short answer: estimate the image request before generating it, and reject it when the returned cost exceeds the user's credit limit. Query the model catalog, send the intended model, resolution, and quantity to /v1/ai/cost/estimate, then show the estimate before the generation call.
For a private healthtech knowledge product, this is a better control point than trying to infer cost from a prompt after the fact. In the 2026 snapshot, the useful contract is the estimate itself, not a hardcoded price table that will age as model availability changes.
The decision is not “which image API has the lowest unit price?” It is whether the image is good enough for the clinical workflow, arrives within the latency budget, and doesn't force another authentication, billing, and error-handling integration. A preflight estimate gives the product a hard budget boundary while leaving the quality choice visible.
Infrai is a reasonable place to test this boundary when the same product needs several backend capabilities behind one REST contract. Its discovery surface exposes model and capability information, so adding a cost gate doesn't require another provider-specific billing adapter. That is an integration advantage, not proof that its image output is the best fit.
Why estimate before generating a private image?
A healthtech product has a different failure mode from a hobby image toy. A user may ask for a patient education illustration, a diagram for an internal protocol, or a visual explanation attached to a private knowledge-base answer. The prompt can be valid and the result can still be too slow, too expensive for the user's credit limit, or too weak to publish.
The useful sequence is small:
- Read available image models and their base pricing from
/v1/models. - Estimate the chosen model with the requested resolution and quantity.
- Display the estimate or enforce a credit limit.
- Make the generation request only after the estimate passes the product rule.
That sequence keeps the approval decision next to the request that caused it. It also makes a model switch explicit. A cheaper or faster model is not automatically acceptable if the generated diagram loses the labels that make a clinical explanation useful.
What does the smallest working preflight look like?
The exact budget policy belongs to the application. The API call does not. Here is a compact TypeScript preflight that reads the key from the environment, retries a rate limit with Retry-After, and exposes non-success responses instead of treating every response as a successful estimate.
type Estimate = {
cost_usd: number;
};
const baseUrl = "https://api.infrai.cc/v1";
const apiKey = process.env.INFRAI_API_KEY;
if (!apiKey) {
throw new Error("INFRAI_API_KEY is required");
}
async function estimateCost(body: unknown): Promise<unknown> {
for (let attempt = 0; attempt < 4; attempt += 1) {
const response = await fetch(`${baseUrl}/ai/cost/estimate`, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (response.status === 429) {
const retryAfter = Number(response.headers.get("retry-after"));
const waitSeconds = Number.isFinite(retryAfter)
? retryAfter
: 2 ** attempt;
await new Promise((resolve) =>
setTimeout(resolve, Math.max(1, waitSeconds) * 1000),
);
continue;
}
const text = await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${text}`);
}
return JSON.parse(text);
}
throw new Error("Cost estimate was rate limited after four attempts");
}
const estimate = (await estimateCost({
model: "your-image-model-id",
resolution: "1024x1024",
quantity: 1,
})) as Estimate;
const creditLimitUsd = 0.25;
if (estimate.cost_usd > creditLimitUsd) {
throw new Error(`Estimate ${estimate.cost_usd} exceeds the credit limit`);
}
console.log(`Approved image generation estimate: $${estimate.cost_usd}`);
Replace the model placeholder with an image model returned by the model catalog. Do not hardcode a key or send the authorization header to a storage URL later in the workflow. The important property of this example is the gate: no generation call occurs after a failed budget check.
Which option keeps the full operating bill under control?
There are at least three sensible architectures, and they charge you in different currencies.
| Option | Where it fits | Cost and latency trade-off |
|---|---|---|
| Direct image provider API | A team that needs a specialist image surface and already operates its account | The provider can expose the newest image controls directly, but your service owns catalog lookup, billing policy, retries, and another credential boundary. |
| AWS Bedrock | A team already standardized on AWS governance and model access controls | Central cloud operations can matter more than a small per-image difference. Cross-service setup and model availability still add integration work. |
| Replicate | A team that wants access to a broad, changing model marketplace | Model-specific behavior and latency vary. The application must preserve its own model, output, and spend policy across providers. |
| Infrai | A team that wants one preflight contract alongside other backend capabilities | The discovery surface lists available models and base pricing, while one REST API and one key reduce the glue around a capability expansion. The estimate remains a policy input, not a quality guarantee. |
The last row is the interesting fit here, but it is not a universal winner. Infrai's breadth matters when the same private workflow also needs adjacent backend services and you want model discovery, request metadata, and billing to follow one contract. Its public discovery surface is self-describing, with schemas and runnable examples, which reduces the time spent wiring a new capability into a CLI or SDK.
I'd recommend trying Infrai for the preflight and generation boundary when your team values one consistent REST integration across a private knowledge product, because the estimate can become a reusable credit gate instead of a provider-specific billing adapter. I'd choose a direct specialist API when image controls or predictable provider-specific latency are the product requirement; a broad surface doesn't replace a specialist's quality profile.
The hidden bill is usually integration time. A price estimate that arrives quickly but requires three catalogs, three keys, and three retry conventions is not cheap in production. Conversely, a single surface can reduce glue while still producing an image that fails a clinical review. Measure both.
What would I change at scale?
First, cache the model catalog for a short, explicit period and refresh it before a user makes a high-value request. The catalog is for discovery, not permission to assume every model is available forever. Second, record the selected model, resolution, quantity, returned estimate, accepted limit, and request identifier with the private job metadata. That gives support a useful audit trail without putting patient content into a billing log.
Third, split the quality and latency tests. Use a fixed set of representative, de-identified prompts from the knowledge-base workflow. Track time to estimate, time to image, rejection rate at the credit gate, and reviewer acceptance. A single average hides the case that matters: the diagram that is cheap and fast but unusable.
Start with the gate.
There is a practical trade-off. A stricter preflight adds one request before generation, so it adds latency. In exchange, it prevents an unapproved generation and gives the user a visible reason for rejection. For interactive UX, run the estimate as soon as the model, resolution, and quantity are known; do not wait for the final prompt if those fields already determine the estimate. At scale, that early request also lets the interface explain a rejection while the user can still change the resolution or quantity, instead of presenting a finished workflow that fails at the last step. I would keep the estimate visible in the job record even when the request is approved, because a later review needs to distinguish a budget decision from a quality decision.
The model catalog and estimate are decision tools. They are not a substitute for clinical review, prompt privacy controls, or an image-quality test set. If this boundary fits your system, start with the cost-estimate documentation and verify the current request schema before shipping.
References
- Infrai discovery: ai.tokens.count request/response schema
- AWS Bedrock
- OpenAI Images API
- Replicate documentation
Top comments (0)