Use cumulative usage updates once in a CometAPI Claude stream

For a Claude integration on CometAPI, choose the usage parser by the endpoint contract—not simply by the model name. If the adapter is configured for CometAPI’s Anthropic Messages route, treat message_delta.usage token counts as cumulative snapshots, not increments to add together.

CometAPI’s Messages documentation specifies POST /v1/messages, configures the Anthropic SDK with base_url="https://api.cometapi.com", and shows client.messages.stream(...) with stream.get_final_message(). Make the adapter’s routing assertion check that exact base URL and /v1/messages destination before selecting the Messages parser. The page points OpenAI-compatible requests to a separate Chat Completions surface, so a move there must fail that assertion and require a separate parser check—not reuse based on the Claude model name.

The rule itself comes from Anthropic’s streaming contract: token counts in message_delta’s usage field are cumulative. Adding those snapshots repeatedly counts earlier tokens again. This is an upstream protocol guarantee, not a claim that a live CometAPI route has been tested here.

Before using this adapter’s output for usage reporting, I would make these acceptance checks:

  • Verify the configured destination is CometAPI’s /v1/messages, then assert that the selected response has the Anthropic Messages event shape (including message_delta usage and message_stop) before dispatching it to this parser. A Chat Completions response must take a separate parser path.
  • Feed the Messages adapter a synthetic fixture, not a claimed service response, with successive message_delta.usage.output_tokens values of 4, 9, and 12, followed by message_stop. The expected completed output-token count is 12, not 25. Replace the stored value for each reported field rather than adding snapshots; a missing field should not erase an earlier value.
  • On an authorized integration test against that selected CometAPI route, compare the adapter’s completed count with the completed Message’s usage.output_tokens when available. Keep an interrupted stream’s latest count marked provisional rather than presenting it as a completed total. These are proposed checks, not executed results.

This decision concerns output-token accounting, not invoice reconciliation. Keep input and cache usage fields separate, and do not translate the synthetic overcount into a savings claim. The useful boundary is narrow: CometAPI’s documented /v1/messages routing and event shape select the adapter; Anthropic supplies the cumulative-event semantics; the route-specific acceptance test checks that the integration preserves them.

I would add an attempt-boundary test before putting this adapter behind a shared interface for Claude and local inference. Choosing the correct protocol parser does not by itself prevent one request’s cumulative snapshot from leaking into the next request.

For the CometAPI Messages adapter, make the accumulator belong to one stream attempt, not to a conversation or a reusable client. A synthetic acceptance test could be:

  • Attempt A reports output_tokens: 9, then disconnects. Retain that observation as provisional under A.
  • A newly initiated attempt B reports output_tokens: 4, then output_tokens: 12, and ends with message_stop.
  • B’s completed output-token count must be 12, not 21; A’s observation must remain separately identifiable rather than being overwritten or presented as another increment in B.

This is a proposed client-state test, not a claim about automatic retries or CometAPI billing. It extends the documented cumulative-event rule by making its accounting boundary explicit.

For a local-inference comparison harness, I would preserve backend, protocol, attempt identity, and completion status alongside the reported count. That lets the harness expose a common reporting interface without asserting that another backend uses Claude’s event semantics—or that equal token counts represent equal work across tokenizers.