Skip to content

Anthropic API provider is not counting input and cache tokens from messageDelta events #4346

Description

@ShitizZip

What happened?

Anthropic API updated their spec at some point to include full usage details in messageDelta events, previously it used to be just output tokens. Now with the update, it includes input and cache token counts. These should be counted cumulatively with tokens from messageStart events as quoted on the referenced page:

The token counts shown in the usage field of the message_delta event are cumulative.

This results in some APIs following Anthropic's API spec breaking Cline's token and context window tracking, resulting in a degraded user experience. The fix for this would require upgrading the Anthropic SDK as it needs the new MessageDeltaUsage definitions that includes the new count types.

Reference: https://docs.anthropic.com/en/docs/build-with-claude/streaming

Steps to reproduce

Using Anthropic API, token counts may be off. This may or may not happen on the official API spec depending upon how they count and communicate tokens. I am able to reproduce it in my local environment through a custom proxy service that exposes Anthropic compatible API.

Relevant API REQUEST output

Provider/Model

Anthropic Models

Operating System

macOS 15.5

System Info

MacBook Pro: M1 Max 64GB

Cline Version

3.17.14-a

Additional context

I have a PR to fix this issue, creating this issue to track it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleAuto-applied to inactive issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions