Last reviewed: November 2, 2026.
Direct answer
CometAPI request coalescing lets equivalent callers share one generation that is already running. After authorizing each caller and constructing the exact request, let one owner dispatch to CometAPI while matching callers wait for that owner’s result. Reserve and record cost for the upstream attempt once, then record each logical caller separately.
This closes a gap that a completed-response cache can leave open. Cloudflare’s AI Gateway caching documentation explicitly warns that two simultaneous identical requests can both reach the origin before the first response enters the cache. Its documented behavior illustrates the race; it does not establish any CometAPI caching behavior.
Use this control only where the product permits callers to receive the same answer. Separate requests for independent samples, tool execution, or personalized results require different treatment. Start with authorized, non-streaming, read-only text tasks whose output can safely be shared within one defined scope.
Who this is for
This guide is for platform engineers and AI budget owners whose services repeatedly generate the same report, summary, or answer during a short burst. It assumes the application controls upstream dispatch and can distinguish user requests from actual model attempts.
The design and acceptance checks below are implementation recommendations. No authenticated CometAPI generation, billing experiment, or production load test was performed for this article. Savings depend on how much eligible work overlaps, how many replicas receive it, and what the upstream attempts actually cost.
Key takeaways
- Join equivalent work before financial admission and dispatch; only the owner needs an upstream reservation.
- Require explicit permission to share an answer, even when request bodies match.
- Give callers independent waiting deadlines and give the shared generation its own bounded lifetime.
- Count dispatched attempts and observed usage separately from logical callers.
- Bound active generations and waiters; coalescing does not create unlimited capacity.
- Treat an in-memory group as a local optimization with a defined process boundary.
Contract details to verify
Decide when one answer is acceptable
An exact request match is necessary for this design, but product semantics determine whether sharing is allowed. Two callers asking for a summary of the same immutable document may accept one shared result. A request to regenerate a different answer or produce twenty independent candidates should still perform the approved independent generations.
Authorize every caller before joining. Build the sharing identity from an opaque authorization scope, billing scope, endpoint, selected model, routing revision, exact outbound body, and any response-affecting configuration outside that body. Include the source-data version and prompt or policy revision where applicable. Preserve message order, generation parameters, response schema, and meaningful content when serializing the body.
CometAPI’s chat-completion reference documents the model and messages request fields, output controls, response choices, and usage accounting. It supports those interface facts. The grouping, authorization, and accounting rules here belong to the application; the reference does not promise server-side duplicate suppression.
Use internal opaque identifiers rather than raw credentials in keys and logs. Keep full request fingerprints private, and log only a bounded correlation identifier. A matching body must never allow one tenant to join another tenant’s work unless an explicit sharing policy permits that boundary.
Define the owner and waiting behavior
Go’s official singleflight package documentation
provides a concrete mechanism: within a Group, Do allows one in-flight function execution per key; duplicate callers wait and receive the same result. DoChan returns a channel that receives the result when ready. That channel is not closed, so consumers should receive one result rather than range over it awaiting closure.
Use a service-owned context with an explicit timeout for the shared generation. Keep it independent of the first caller’s cancellation, while connecting it to service shutdown. Each waiter selects between its own deadline and the result channel. The Go context reference documents timeout contexts and cancellation signals; it also states that calling a cancellation function does not wait for work to stop.
For an initial implementation, let a bounded generation finish even if a waiter disconnects. Reference-counted cancellation when all waiters leave is an optional extension that needs race testing. Either policy must retain the upstream attempt’s financial state until usage or reconciliation resolves it. A client disconnect is not evidence of zero charge.
Do not call Forget merely because one waiter times out. The singleflight reference says future calls after Forget can execute a new function instead of waiting for the earlier one. That can deliberately create overlapping upstream work. A timed-out waiter should leave; it should not silently start a replacement generation.
Put the budget check inside the owner’s work
The following sequence is pseudocode for an application-owned registry, not a complete singleflight implementation:
authorize caller; build eligible sharing identity
under registry lock:
if a flight exists for this identity:
if its waiter allowance permits: join that flight
else: reject or defer this caller; do not create another owner
else:
if active-flight capacity permits: register one new flight and its owner
else: reject or defer this caller
outside registry lock:
owner reserves budget for one bounded upstream attempt
owner dispatches once and records attempt identity
owner validates completion and settles or retains unresolved cost
owner publishes result or error, then removes its own flight entry
each caller receives a separate response or leaves at its own deadline
The join-or-create decision must be atomic. Create an owner only when no flight exists for that identity. A full waiter allowance never permits a second owner, reservation, or dispatch; a deferred caller must re-enter the same registry decision before doing any work. Never hold the registry lock across budget storage, network calls, or response delivery. Remove an entry only if it still belongs to that flight, and guarantee cleanup on every terminal path. A budget denial must reach all waiters without dispatching.
Bound active keys, waiters per key, request bytes, response bytes, and fill duration. When a bound is reached, use the workload’s explicit rejection or deferral policy. Sending every rejected join directly upstream would bypass the intended control. singleflight.Group itself is not a complete resource limiter; add those limits in the surrounding application.
Give retries one owner too. A shared failure should not trigger a separate immediate retry from every waiting caller. Disable hidden retries where appropriate, or include the configured retry exposure in the owner’s admission and attempt records. A permitted replacement attempt needs its own budget treatment, and an earlier uncertain attempt remains visible.
Use the site’s atomic budget reservation guide for durable financial admission. Coalescing decides which callers can share work; the ledger decides whether that work can be funded.
Measure savings without multiplying the credit
Track three different counts: logical callers, owner executions, and actual upstream attempts, including retries. Record cost against the attempt, then link the callers to it. Do not add the same usage object to the spend ledger for every recipient of a shared response.
CometAPI’s chat reference describes the response usage object as token accounting used for billing. Preserve those reported counts once per attempt and apply the verified rate map. If usage is missing after a timeout, mark it unresolved. Zero is a measured value, not a substitute for missing evidence.
A useful counterfactual is:
estimated_net_saving = estimated_cost_without_coalescing
- recorded_upstream_cost - added_coordination_cost
Consider twenty equivalent callers arriving while one generation runs. Assume, solely for illustration, each independent call would use 1,000 input tokens and 200 output tokens, at hypothetical rates of $2 and $8 per million tokens. That makes one call $0.0036. Twenty independent calls would cost an estimated $0.072; one successful shared call would cost $0.0036. The estimated difference is $0.0684 before coordination overhead.
These are hypothetical rates and a counterfactual with identical token counts, no retries, and one owner. They are not CometAPI prices or measured savings. Real independent outputs can have different lengths, and upstream input caching can change their costs. Label the estimate accordingly and compare actual recorded spend with a representative baseline.
Do not count singleflight’s Shared flag as a follower count. Its documentation says the value indicates whether a result went to multiple callers, so the owner can also observe a shared result. Track owner creation and joins explicitly. Preserve the business denominator too: twenty successfully served logical requests remain twenty served requests even when only one model call occurs.
Operator workflow and verification
Begin with one eligible request class and one process. Use a fake upstream that can hold a response open while several callers join. Disable completed-response caching in this test so a later cache hit cannot be mistaken for in-flight joining. The observations below are acceptance criteria, not reported results:
| Scenario | Required observation |
|---|---|
| Twenty equivalent callers overlap while the fake upstream is blocked | One owner, nineteen joins, one dispatch, and one usage settlement |
| An existing flight is at its waiter limit while active-flight capacity remains | Extra caller is rejected or deferred; no additional owner, budget reservation, or upstream dispatch |
| Tenant, billing scope, model, prompt revision, or body differs | Separate flight or explicit ineligibility; no unauthorized join |
| First caller cancels while a second still waits | First caller leaves; bounded shared work can still satisfy the second |
| Budget admission fails | All joined callers receive the controlled failure; no upstream request starts |
| Upstream fails | One shared error outcome; waiters do not each start an automatic retry |
| Upstream times out after dispatch | Attempt remains unresolved; cancellation does not release its cost assumption |
| A later request arrives after flight removal | A new owner is allowed only through fresh admission |
| Two independent service replicas receive equivalent work | Local groups can create two owners; telemetry exposes the actual scope |
Record flight ID, caller outcome, owner or join role, wait duration, attempt ID, dispatch count, reported usage, settlement state, and denial reason. Keep prompts, generated text, authorization headers, and personal identifiers out of routine cost events.
During a small production canary, compare upstream attempts per served logical request, unresolved cost, timeout rate, and response quality with the baseline. Pair this with the per-workload concurrency guide so many distinct keys cannot overwhelm the service.
Failure modes
One Group per handler invocation. Callers never reach the same registry, so duplicate work continues. Use a deliberately shared group for the intended process and workload scope.
Local suppression is presented as global suppression. Separate processes do not share an in-memory Group. Either accept and measure that boundary or design a separate coordinated service with explicit ownership and crash recovery. A distributed lease alone does not prove an old upstream call stopped.
Shared objects are mutated by recipients. A caller adds user-specific fields to a response that another caller also receives. Return independent serialized copies or immutable values and keep delivery metadata outside the shared result.
Errors are forgotten and retried by everyone. Removing a key early or allowing independent retry loops recreates the burst. Publish the owner’s outcome and apply one bounded retry policy.
Freshness or sampling requirements are lost. Similar questions with different data revisions join, or independent candidate requests receive one answer. Tighten eligibility and identity before optimizing the join rate.
The registry is mistaken for durable idempotency. Restarting a process loses local state. Coalescing does not guarantee exactly-once external execution or recover unknown charges; durable attempt identity and reconciliation still matter.
Sources checked
Four primary public references were checked on November 2, 2026:
- CometAPI: Create a chat completion — endpoint, request and response fields, and usage accounting. The page shows an October 8, 2026 modification date.
- Cloudflare: AI Gateway caching — exact-request caching and the documented simultaneous-miss race. The page shows a September 30, 2026 update.
- Go: singleflight
— per-key suppression, shared results,
DoChan, andForgetbehavior. - Go: context — deadlines, cancellation, timeout cleanup, and the distinction between signaling cancellation and waiting for completion.
These are current technical references for stable interface behavior, rather than a recent product announcement. Verify them against the library versions and CometAPI model route used in your deployment. The financial model and rollout rules are recommendations derived from that behavior.
FAQ
Is this the same as provider prompt caching?
No. Prompt caching can reduce processing of repeated input while a new generation still occurs. In-flight coalescing lets authorized callers share one generation. A completed-response cache adds another reuse interval after generation finishes.
Does matching the request guarantee identical independent answers?
No. Sampling and backend behavior can vary. Coalescing intentionally returns one shared answer, so use it only when that behavior is acceptable. A low temperature does not establish permission to merge independent requests.
Can this coalesce streaming or tool-calling requests?
The initial design excludes them. Streaming adds buffering, late-join, backpressure, and disconnect rules. Tool execution adds side-effect ownership. Extend the contract only after those behaviors have separate acceptance checks.
Does this impose a hard spending cap?
No. It can reduce duplicate attempts within its scope. Distinct requests, retries, bypass traffic, and other replicas still consume budget. Financial admission and reconciliation remain necessary.
Reader next step
Choose one read-only task whose users may share a result, write its sharing identity and deadline policy, and implement the overlapping-call test first. Confirm that joins create no extra dispatch or spend entries before enabling a small canary.
When the local checks pass, Start with CometAPI to verify the selected model route with bounded, non-sensitive traffic. Reconcile the upstream attempt count and reported usage before crediting the optimization in a budget forecast.