Preserve cache-write usage before estimating CometAPI cache savings

A cache-read counter is not enough to estimate cache savings on a CometAPI route. If your cost report consumes LangChain usage metadata, first check whether its adapter preserves cache creation, not just cache reads. Otherwise the report can lose the very category needed to account for populating the cache.

This is a usage-to-billing reconciliation problem: start with a returned usage record, establish what each field means, then attach the applicable CometAPI price. Do not start with a discounted read rate and fit every cache-related token into it.

What the adapter documentation establishes

In its Cache write tokens section, LangChain documents that ChatOpenAI surfaces reported cache_write_tokens as cache_creation in input_token_details. It also documents tier-prefixed keys for the priority and flex service tiers, giving priority_cache_read and priority_cache_creation as examples.

That matters for a cost collector which recognizes only cache_read: recognizing one documented category does not mean it captures the other categories or their tier-qualified names.

These are documented adapter mappings—not proof that a particular CometAPI response contains those fields, or that CometAPI supports those service tiers. Compare the actual response and the adapter output for the route you use before relying on the mapping.

Keep the distinction through to CometAPI billing

CometAPI’s cached-input pricing guide recommends keeping cache reads, writes and storage separate, preserving usage fields, and checking the exact model and service tier. It also explains why a provider’s direct price does not by itself establish a gateway’s commercial terms. A “caching supported” label is not sufficient billing evidence.

The guide’s price table is a July 29, 2026 snapshot, not a current quote. The useful integration decision here is to make the LangChain-to-CometAPI cost report retain enough information to apply a verified route price later—not to copy a historical discount into the collector.

I would use the following accounting rules:

Reported information Treatment in the cost report
Cache-read count Retain as a read category; attach a rate only after verifying that route’s billing definition.
Cache-creation count Retain separately from reads; do not price it as a discounted read.
Tier-prefixed cache key Preserve the original key and tier qualifier rather than silently merging it into an unqualified bucket.
Explicit zero Record zero for that reported field, without treating it as a claim about other fields.
Missing field Record unknown/not reported, not zero.
Aggregate input total Preserve it, but verify whether cache categories are included before combining totals.

This table is a proposed reporting policy, not a CometAPI response schema. In particular, adding cache creation and reads on top of aggregate input can double count if those categories are already included. Neither source establishes the overlap definition for your selected route.

A reconciliation check before publishing a savings number

Take a known request and compare its sanitized usage record with the LangChain metadata that reaches the cost collector. Keep credentials and prompt content out of this accounting sample. Retain the exact model identifier, route/provider and tier when exposed, original usage keys, and reported totals.

Then check two independent questions:

  1. Did the adapter preserve the accounting evidence? Verify that reported creation and read fields remain distinguishable, including tier qualifiers and missing-versus-zero status.
  2. Does the assigned price match the CometAPI charge? Match the request to available billing evidence and the current price for the selected model, route and tier. Keep any applicable storage charge separate; do not infer one from a token counter.

If field meaning, total overlap or the applicable price is unverified, leave that part of the estimate unresolved and visible. A partial priced subtotal should not be presented as the complete request cost—or as demonstrated savings.

The practical acceptance criterion is a cost report that can explain each priced category against the actual CometAPI route, while exposing what it cannot yet price. No live request or invoice was tested here, so this is a reconciliation procedure, not a measured savings result.

I would turn the reconciliation procedure into a small regression exercise before changing the collector. The useful test is not “does this request show savings?” but “can the collector lose evidence without the test failing?”

Start with one sanitized usage sample from the selected CometAPI route and its corresponding LangChain metadata. Then make explicitly synthetic variants for the collector test—not purported gateway responses:

  • Remove a reported creation field. The expected result should change from a known count to unknown, not to zero.
  • Set that same field to explicit zero. This should remain distinguishable from the missing-field case all the way through the report export.
  • Introduce an unfamiliar cache-related key. Preserve it for inspection and flag its classification as unresolved rather than silently discarding it or assigning the read rate. This tests forward compatibility; it does not establish that the route emits that key.

Keep prices out of this first test. A second, separate fixture can bind the preserved categories to verified route billing definitions and rates. That separation makes a failure actionable: a lost category is a collector defect, while an unpriced but preserved category is an unresolved accounting input.

For repeatability, I would record the adapter version alongside each fixture and rerun it when upgrading dependencies. These synthetic cases test information preservation only; even a passing suite would not prove a cache hit, service-tier support, or agreement with the CometAPI bill.